GeeCON Prague 2014*
The moose likes JSP and the fly likes scriplets in jsp's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "scriplets in jsp Watch "scriplets in jsp New topic
Author

scriplets in jsp's

Donna Bachner
Ranch Hand

Joined: Oct 08, 2004
Posts: 109
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.


Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
TellTheDetails

There must be more to your code. Where does the variable arr come from?

Is there more to the error?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
True, I should have mentioned that.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
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
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?
Donna Bachner
Ranch Hand

Joined: Oct 08, 2004
Posts: 109
this is what I have in my Servlet:

book[j] = isbnArr[j] +" , "+titleArr[j] +" , "+authorArr[j]+" , " +conditionArr[j] +" , "+priceArr[j]+"/n";


}

request.setAttribute("book", book);


RequestDispatcher view = request.getRequestDispatcher("archResults.jsp");
view.forward(request,response);

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
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: 1364
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?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

the arr is coming from the servlet.


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
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 {

PrintWriter out = response.getWriter();

try{

archBean DB = new archBean();
ArrayList isbn = DB.getISBN();

ArrayList title = DB.getTITLE();


ArrayList author = DB.getAUTHOR();

ArrayList condition = DB.getCONDITION();

ArrayList price = DB.getPRICE();
isbn.trimToSize();
int i = isbn.size();


Object isbnArr[] = new Object[i];
isbnArr = isbn.toArray();
Object titleArr[] = new Object[i];
titleArr = title.toArray();
Object authorArr[] = new Object[i];
authorArr = author.toArray();
Object conditionArr[] = new Object[i];
conditionArr = condition.toArray();
Object priceArr[] = new Object[i];
priceArr = price.toArray();
String book[] = new String[i];

for(int j = 0; j <i; j++)
{
book[j] = isbnArr[j] +" , "+titleArr[j] +" , "+authorArr[j]+" , " +conditionArr[j] +" , "+priceArr[j]+"/n";


}

request.setAttribute("book", book);


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.

*************************************************************************
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<table width="100%" border="3" cellspacing="5" cellpadding="5" bordercolor="#0000FF"background-color:#FFFF99;>
<tr>
<td>
<a href="index.jsp" title="index">HOME</a></td>
<td><a href="buy.jsp" title="Buy books">BUY BOOKS</a></td>
<td><a href="Sell.jsp" title="sell books">SELL BOOKS</a></td>
<td>BROWSE BOOKS</td>
<td><a href="help.jsp" title="help">HELP</a></td>
<td><a href="logon.jsp" title="login">LOGIN</a></td>
<td><a href="http://www.pitt.edu" title="logout">LOGOUT</a></td>
</tr>
</table>
<center><h1>Results</h1></center>
<c:set var="j" value="0" scope="page"/>


<c:forEach items="book[j]" var="getBook"/>


<textarea name="res" rows="10" cols="255">




</textarea>
</body>
</html>
***************************************************************************

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 statement;
ResultSet isbn;
ArrayList<String> isbnList = new ArrayList<String>();

try{

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: 1364
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.

Here's an example from chapter 6 of the JSTL 1.1 Specification JSTL 1.1 Specification

So, assuming that in a servlet that forwards to this jsp they did the following similar to your array of books with attribute "book":

request.setAttribute("customers",arrayOfCustomers);



If that's not what you need to do, what do you need to do?
Donna Bachner
Ranch Hand

Joined: Oct 08, 2004
Posts: 109
that is exactly what I need to do. I am using the int variable so I can number the books. I'll see if it works. ;)
Donna Bachner
Ranch Hand

Joined: Oct 08, 2004
Posts: 109
Hi
I tried:
<table>
<c:forEach var=�books� items=�${book}�>
<tr><td>${books}</td></tr>
</c:forEach>
</table>

and I get $(books) in the table. could the book array not be going to the jsp properly? Or do I need to include something at the top of the jsp?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Originally posted by Donna Bachner:

and I get $(books) in the table.


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
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

If you are seeing ${books} your server is not set up properly to evaluate EL.
[ December 04, 2006: Message edited by: Bear Bibeault ]
Donna Bachner
Ranch Hand

Joined: Oct 08, 2004
Posts: 109
I found the problem a typo in my context file. Thanks for all your help.
 
GeeCON Prague 2014
 
subject: scriplets in jsp's