Click to See Complete Forum and Search --> : Miserable Kirilisa's very easy Java/JSP/JSTL questions


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>

chazzy
01-31-2008, 09:42 PM
1. No, they're the same as long as you put the java code inside of a scriptlet.
2. EL is "expression language." See here: http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.html
3. Vector is an implementation of the List interface (part of the java collections framework). Its methods are all syncrhonized. ArrayList is similar to Vector, also implementing List, but is not synchronized. You will probably want to use a List if you need to use the collections framework, arrays otherwise. In general, most people pick lists.
4. The primitive equivalent to String is char[]. Individual char's are wrapped as Character, but char[] is wrapped into String.
5. I don't know what you mean. Probably use a List though.
6. You probably want to reduce the amount of code in the JSP as much as possible, to keep it more object-oriented.
7. See 6.
8. See 6. Don't put SQL in to JSP, use proper objects.

kirilisa
01-31-2008, 10:12 PM
Hey,

Thanks for the answers.

Problem is, I didn't write this massive application, and it is ALL written with SQL mixed with JSP mixed with HTML, I look at it and could cry it's so messy, but that's how he did it. I just have to fix all the broken stuff and implement new stuff someday. Hopefully I can clean it up some as I go, but I don't have the time to concentrate on completely rewriting it (it is huge!) and either way I can't do much until I gain a lot more familiarity with the language :-/

For 5), I just meant, that since you usually initialize an Array as String[] = new String[5], and then are limited to 5 elements in that array, what happens if you want to create an array, but don't know ahead of time that there will be 5 elements in it? ArrayList is resizeable I gather, but an array as defined by String[] = new String[5] is not. Thus you'd have to use ArrayList if you didn't know the number of elements straight up?

Regarding 6, 7 & 8, I know and appreciate that it is abominable coding practice to have all the SQL etc. right in there (I reiterate, I didn't write this beast) but I am trying to understand how and why things work. e.g. 6, how is it possible to do [see red comment] whether or not it is best to do it that way? Likewise for 7) I am wondering if it is possible, although I would hope to not end up doing it that way.

As for 8), the issue isn't where the SQL is gotten from, whether it's in the JSP or not, it's the question of trying to test if the value of a certain HTML select field is in a list/array of items, so that one knows whether or not that HTML option should be selected or not. It really doesn't have anything to do with the SQL or how it is gotten, but rather with types etc. Either way one has to interface with HTMl somehow, whether that is via the JSTL tags or dumping it through a servlet, and the main issue there was I didn't understand how JSTL was dealing with the types.

Again, thanks for your answers so far. If you have any more input to give to clarify what I just wrote, I'd be very appreciative. Java is as I said completely foreign to me, I've only been looking at it for a few days, and trying to debug this huge messy often broken application when I am unfamiliar with Java (or any language like it) is proving difficult for me. I am still trying to get a kind of basic understanding of the whys and hows at this point.

Thanks!