kirilisa
01-31-2008, 07:30 PM
Hello, I'm trying to debug (and ultimately add to, ha ha) a very massive (and rather messy) web application written in JSP. Sadly, I know zilch about Java/JSP/JSTL. While I've managed to fix a bunch of minor errors in the application, implementing new stuff is very difficult as I don't even know the syntax for most things. I have therefore a list of what should be ridiculously easy questions which I can't find coherant info on after Googling etc. and I'd be most appreciative if someone would give me their input.
**Kirilisa's embarassingly idiotic list of questions***
1) Is there any difference, syntactically, of coding in JSP and in Java?
Obviously JSP you're writing straight into the page and it's being interpreted etc, but syntax-wise are they identical?
2) What exactly is referred to by EL? JSTL refers to the tag library, <sql:query and the like, plus whatever proprietary tags you make (right?), but what exactly does the EL refer to? Simply the ${} expressions which are used by the JSTL tags? And if so, wouldn't that make JSTL & EL so intimately linked as to barely be deserving of considering them separately, or can you ever use one without the other? (Just curious)
3) What is the difference between Vector, ArrayList, and an array as defined by String[] = new String[50]? I gather Vector is slow, ArrayList is medium, and String[] is fast?
4) Is there no string class vs. string primitive type? For instance, there is Integer (class) and int (primitive type) and while I always see arrays as such int[] arr = {whatever} and String[] arr = {whatever} I was wondering why/if there is not a String vs. string.
5) Is it possible to make an array without allocating it? Is this what ArrayList is for? If you're not sure how many elements your array is going to need, what is the general practice?
6) Is it possible to create an array directly in JSTL? I realize you can do
<c:set var="myobj" value="<%=new java.util.HashMap()%>" />
<c:set target="${myobj}" property="name" value="myname" />
<c:set var="myobj" value="<%=new java.util.ArrayList()%>" />
<!-- but how do I add an item to this arraylist since <c:set requires a property and it won't take nothing, 0, or "0"? -->
7) Is there any way to pass a variable, array or hash from JSTL to JSP? I know that an item can be gotten and printed as below, but how do you store it? When I try it gives me incompatible types error.
<c:set var="x" value="12"/>
<%
out.print(pageContext.getAttribute("x")); // this works
Integer x = pageContext.getAttribute("x"); // this fails with incompatible type error; ditto when I try to assign it to a String
%>
And here is the question that has me most miserable...
8) Does JSTL have any notion of types? I read a few places that it doesn't, but if so, what is this page on about? http://www.informit.com/articles/article.aspx?p=30946&seqNum=5 As far as I can tell you can't use toString from JSTL and anyway it doesn't exactly return you the object converted to a string, but more a representation of the object as a string, isn't that right?
I have some JSP code here which calls 2 SQL queries: one a whole list of departments, one a subset of those departments that are assigned to a certain piece of news. I am trying simply to figure out which departments are in the subset list so I can make them automatically selected in a HTML select box in a form. However, what I've written fails, apparantly because for some reason departmentId is sometimes a string and sometimes an integer. See comments in code. Can anyone tell me what I am doing wrong?
Thank you!!!
<!-- get list of all departments from DB -->
<sql:query var="q" sql="SELECT departmentId, name FROM SystemDepartment ORDER BY name" />
<!-- get list of selected departments from DB -->
<sql:query var="du">
SELECT nu.departmentId, sd.name
FROM SystemNewsUser nu LEFT JOIN SystemDepartment sd ON nu.departmentId = sd.departmentId
WHERE nu.newsId = ?
<sql:param value="${param.id}" />
</sql:query>
<!-- create associative array to store deptid/deptname key/value pairs -->
<c:set var="depts" value="<%=new java.util.HashMap()%>" />
<!-- store all the queried department info in said associative array -->
<c:forEach var="x" items="${du.rows}">
<!-- why does this end up with the property being type string? -->
<c:set target="${depts}" property="${x.departmentId}" value="${x.name}" />
</c:forEach>
<!-- display the multiple section field with all departments -->
<select multiple="multiple" name="departmentId" size="4" style="width:200px">
<c:forEach var="r" items="${q.rows}">
<!-- how does this decide that the variable is type integer, unlike above? -->
<c:set var="id" value="${r.departmentId}"/>
<c:set var="sel" value="" />
<option>${depts[id]}</option>
<!-- this test always fails because id is an integer (I assume) and the keys in the depts hashmap are strings -->
<c:if test="${not empty depts[id]}">
<c:set var="sel" value="selected" />
</c:if>
<option value="${r.departmentId}" ${sel}>${r.name}</option>
</c:forEach>
</select>
**Kirilisa's embarassingly idiotic list of questions***
1) Is there any difference, syntactically, of coding in JSP and in Java?
Obviously JSP you're writing straight into the page and it's being interpreted etc, but syntax-wise are they identical?
2) What exactly is referred to by EL? JSTL refers to the tag library, <sql:query and the like, plus whatever proprietary tags you make (right?), but what exactly does the EL refer to? Simply the ${} expressions which are used by the JSTL tags? And if so, wouldn't that make JSTL & EL so intimately linked as to barely be deserving of considering them separately, or can you ever use one without the other? (Just curious)
3) What is the difference between Vector, ArrayList, and an array as defined by String[] = new String[50]? I gather Vector is slow, ArrayList is medium, and String[] is fast?
4) Is there no string class vs. string primitive type? For instance, there is Integer (class) and int (primitive type) and while I always see arrays as such int[] arr = {whatever} and String[] arr = {whatever} I was wondering why/if there is not a String vs. string.
5) Is it possible to make an array without allocating it? Is this what ArrayList is for? If you're not sure how many elements your array is going to need, what is the general practice?
6) Is it possible to create an array directly in JSTL? I realize you can do
<c:set var="myobj" value="<%=new java.util.HashMap()%>" />
<c:set target="${myobj}" property="name" value="myname" />
<c:set var="myobj" value="<%=new java.util.ArrayList()%>" />
<!-- but how do I add an item to this arraylist since <c:set requires a property and it won't take nothing, 0, or "0"? -->
7) Is there any way to pass a variable, array or hash from JSTL to JSP? I know that an item can be gotten and printed as below, but how do you store it? When I try it gives me incompatible types error.
<c:set var="x" value="12"/>
<%
out.print(pageContext.getAttribute("x")); // this works
Integer x = pageContext.getAttribute("x"); // this fails with incompatible type error; ditto when I try to assign it to a String
%>
And here is the question that has me most miserable...
8) Does JSTL have any notion of types? I read a few places that it doesn't, but if so, what is this page on about? http://www.informit.com/articles/article.aspx?p=30946&seqNum=5 As far as I can tell you can't use toString from JSTL and anyway it doesn't exactly return you the object converted to a string, but more a representation of the object as a string, isn't that right?
I have some JSP code here which calls 2 SQL queries: one a whole list of departments, one a subset of those departments that are assigned to a certain piece of news. I am trying simply to figure out which departments are in the subset list so I can make them automatically selected in a HTML select box in a form. However, what I've written fails, apparantly because for some reason departmentId is sometimes a string and sometimes an integer. See comments in code. Can anyone tell me what I am doing wrong?
Thank you!!!
<!-- get list of all departments from DB -->
<sql:query var="q" sql="SELECT departmentId, name FROM SystemDepartment ORDER BY name" />
<!-- get list of selected departments from DB -->
<sql:query var="du">
SELECT nu.departmentId, sd.name
FROM SystemNewsUser nu LEFT JOIN SystemDepartment sd ON nu.departmentId = sd.departmentId
WHERE nu.newsId = ?
<sql:param value="${param.id}" />
</sql:query>
<!-- create associative array to store deptid/deptname key/value pairs -->
<c:set var="depts" value="<%=new java.util.HashMap()%>" />
<!-- store all the queried department info in said associative array -->
<c:forEach var="x" items="${du.rows}">
<!-- why does this end up with the property being type string? -->
<c:set target="${depts}" property="${x.departmentId}" value="${x.name}" />
</c:forEach>
<!-- display the multiple section field with all departments -->
<select multiple="multiple" name="departmentId" size="4" style="width:200px">
<c:forEach var="r" items="${q.rows}">
<!-- how does this decide that the variable is type integer, unlike above? -->
<c:set var="id" value="${r.departmentId}"/>
<c:set var="sel" value="" />
<option>${depts[id]}</option>
<!-- this test always fails because id is an integer (I assume) and the keys in the depts hashmap are strings -->
<c:if test="${not empty depts[id]}">
<c:set var="sel" value="selected" />
</c:if>
<option value="${r.departmentId}" ${sel}>${r.name}</option>
</c:forEach>
</select>