What is the proper syntax for embeding jave scripting into a jsp. I have a book which tells me to put the java between <% for(int i =0; i<arr.length; i++) out.print(arr[i]); %> Which does not work I get unable to compile error occured in jsp.
Note that as of JSP 2.0, scriptlets are considered passe. If using JSP 2.0, you should be focusing on using the EL, JSTL, and proper application structure in order to avoid scriplets.
Really they're pretty much unnecessary as of JSP 1.2. But sometimes they can make sense, when a single line of scriptlet would require half a page of JSTL (for example). This however is not such a case.
But as to the original question, where do you define "arr"?
42
Donna Bachner
Ranch Hand
Joined: Oct 08, 2004
Posts: 109
posted
0
the arr is coming from the servlet. I am changing to jstl's but I have a simple question how do you close a jstl line of codefor example <c:param= " j=0"/> How do I say I want j to be an int and equal to 0?
What do I need in my JSP to print out book[]. I'm confused I don't see any options to get the variable. my IDE would not let me do a scriplet
Jeroen T Wenting
Ranch Hand
Joined: Apr 21, 2006
Posts: 1847
posted
0
I'm not quite sure, but c:forEach might work with arrays. If not, turn the array into a List or Set inside the servlet and set that in the request instead of the array, and use that with c:forEach.
Carol Enderlin
drifter
Ranch Hand
Joined: Oct 10, 2000
Posts: 1348
posted
0
You are not showing us enough code or details with your questions to help us help you.
1. Please indicate what version of jsp you are using. If you don't know or can't figure out what version of jsp, then what are you running your jsps on, Tomcat 5.5 or WebLogic 8.1 or ?
2. The JSTL <c:foreach/> tag works on arrays. I recommend downloading the very readable specification (need to know which version to download, see the JspFaq). You could also google for an example use of c:foreach, but you really need something to use as an ongoing reference.
3. When you use request.setAttribute("book", book); in your servlet you should be able to access "book" with jstl in your jsp. An extra step is required with scriptlets and we are recommending you not go the scriptlet route.
4. Regarding your question about closing a line of jstl, use of c:param, and int = 0 what are you trying to do?
Originally posted by Donna Bachner: the arr is coming from the servlet.
If this means that you have a servlet that contains a declaration for a "arr" variable, that has nothing to do with the JSP. The JSP is a separate unit and if it wants to use a variable named "arr" in a scriptlet, it has to declare one.
This is a perfect example of how being incomplete just confuses the people who are trying to help you.
This phrase could mean many things. Is "arr" a method variable as Paul surmised? Or is it a scoped variable that you created in the servlet? If so, how and in what scope?
Please click on the "Smart Questions" link below and learn how to pose your questions such that we can help you. Otherwise. you're just wasting the time of the people trying to help you, as well as your own.
Donna Bachner
Ranch Hand
Joined: Oct 08, 2004
Posts: 109
posted
0
Okay, here is the information I know. My IDE is netbeans 5.5, I'm using JSP 2.0, I am using the derby database, with tomcat 5.5. This is long Here is my servlet: *************************************************************************** public class run extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher view = request.getRequestDispatcher("archResults.jsp"); view.forward(request,response); ************************************************************************* my jsp: this is where I have no clue how to get my variables in the textarea.
here is part of my bean I do this routine for each variable: this part works I can get the variables from the database to the screen it is just not layed out the way I want it. ***************************************************************************
public ArrayList<String> getISBN() throws Exception{
statement = con.createStatement(); isbn = statement.executeQuery("select ISBN from BOOKS_BOOKS WHERE SUBJECT = 'Architechture'");
while(isbn.next()){
isbnList.add(isbn.getString("ISBN"));
}
} catch(Exception excep){
throw new Exception(excep);
}//end catch
Carol Enderlin
drifter
Ranch Hand
Joined: Oct 10, 2000
Posts: 1348
posted
0
Not sure what you want to do with the array of books. If you want to iterate over the array and list each one, you do not need an int variable to index into the array...c:foreach will handle all that.
Before trying anything this complicated you need to get your server set up properly.
Start with a simple JSP that contains ${3+4}
Unitl that displays 7, you're not ready.
Did you follow all the steps outlined in the JSP FAQ to get the JSTL and EL (as well as your web.xml) configured correctly?
Donna Bachner
Ranch Hand
Joined: Oct 08, 2004
Posts: 109
posted
0
yes my server is set up properly. I have used it before on other projects. When I put the print statement into the bean the program works fine except I can't make the output look differently. I can take information from the jsp to the bean. I just can't go the other way around. I will try something simpler and see if that works.