JSP Problems

hi all
[13 byte] By [Sehama] at [2007-11-26 22:16:30]
# 1
hello, like you, i've got lots of JSP problems.. could you please help me?
suparenoa at 2007-7-10 11:09:49 > top of Java-index,Java Essentials,Java Programming...
# 2
well, I have posted that message because I have JSP problems myself,so can anyone help me please.
Sehama at 2007-7-10 11:09:49 > top of Java-index,Java Essentials,Java Programming...
# 3
Well I think everybody believes you posted your message by mistake and you will soon post a real question that can be answered ...
quittea at 2007-7-10 11:09:49 > top of Java-index,Java Essentials,Java Programming...
# 4

I am trying to read records from postgresql in JSP page and store the records in a vector. and store this vector in a session.

the values of this session will be dislpyed in another JSP page where session.getValue(vector) is called to display the DB records.

but every time I try to run the JSP pages I got erros.

can anyone help me please beacause its part of my final year project.

Here is the code for the two pages:

first JSP (LaptopsListings.jsp)

<%@ page import="java.sql.*" %>

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

<%@ page session= "true" import="java.util.*, seham.Laptops" %>

<html>

<head><title> Laptops page </tilte></html>

<body>

<%

Connection conn = null;

Statement stmt = null;

String query;

ResultSet rs= null;

HttpSessionsession;

try{

Class.forName("org.postgresql.Driver");

conn = DriverManager.getConnection("jdbc:postgresql:shopdb","postgres","07101981");

stmt = conn.createStatement();

query = "SELECT * FROM laptops";

Vector records = new Vector();

rs = stmt.executeQuery(query);

Laptops laptop ;

while( rs.next() ){

laptop = new Laptops( rs.getString( 1 ), rs.getString( 2 ),Integer.parseInt(rs.getString( 3 )),Integer.parseInt(rs.getString( 4 ));

records.add(laptop);

session.putValue("ShopLaptops", records);

rs.close();

stmt.close();

}

}catch(ClassNotFoundException cnf){

out.println(cnf.toString());

}catch(SQLException sql){

out.println(sql);

}

%>

<form action="http://localhost:8080/jsp-examples/admin/DisplayLaptopsListing.jsp" method="post">

<input type="submit" value="Show Laptops">

</form>

</body>

</html>

the second JSP(DisplayListingLaptops.jsp)

<%@ page import="java.sql.*" %>

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

<%@ page session= "true" import="java.util.*, seham.Laptops" %>

<%

Vector LaptopsList = (Vector)session.getValue("ShopLaptops");

if (LaptopsList != null && (LaptopsList.size()> 0)) {

%>

<HTML>

<HEAD>

<TITLE>Laptops List</TITLE>

</HEAD>

<BODY BGCOLOR= "#FDF5E6" >

<center>

<H1>Laptops For Sale</H1>

<table border="0" cellpadding="0" width="100%" bgcolor="#FDF5E6">

<%

for (int index = 0; index < LaptopsList.size(); index++){

Laptops laptop ;

laptop = (Laptops)LaptopsList.elementAt(index);

<tr>

<td><b><%= laptop.getLaptop_id() %></b></td>

<td><b><%= laptop.getLaptopDesc() %></b></td>

<td><b><%= laptop.getLaptopQuantity() %></b></td>

<td><b><%= laptop.getLaptopPrice() %></b></td>

<td>

<form name="BUY"

action="http://localhost:8080/myServlets/servlet/ShoppingCartController1"

method="POST">

<input type="submit" value="BUY">

<input type="hidden" name= "index" value='<%= index %>'>

<input type="hidden" name="action" value="BUY">

</form> </td>

</tr>

<%

}// end for

%>

</table>

</center>

<%

} //end if

%>

</body>

</html>

here is the errors that I got:

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 12 in the jsp file: /admin/LaptopsListing.jsp

Generated servlet error:

Syntax error, insert ")" to complete Expression

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 12 in the jsp file: /admin/LaptopsListing.jsp

Generated servlet error:

Syntax error, insert ")" to complete Expression

org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)

org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)

org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:414)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:297)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)

org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:305)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Sehama at 2007-7-10 11:09:49 > top of Java-index,Java Essentials,Java Programming...
# 5

I am trying to read records from postgresql in JSP page and store the records in a vector. and store this vector in a session. the values of this session will be dislpyed in another JSP page where session.getValue(vector) is called to display the DB records. but every time I try to run the JSP pages I got erros. can anyone help me please beacause its part of my final year project. Here is the code for the two pages:

first JSP (LaptopsListings.jsp) <%@ page import="java.sql.*" %> <%@ page import="java.io.*" %> <%@ page session= "true" import="java.util.*, seham.Laptops" %> <html> <head><title> Laptops page </tilte></html> <body> <% Connection conn = null; Statement stmt = null; String query ; ResultSet rs = null; HttpSession session; try{ Class.forName("org.postgresql.Driver"); conn = DriverManager.getConnection("jdbc:postgresql:shopdb","postgres","07101981"); stmt = conn.createStatement(); query = "SELECT * FROM laptops"; Vector records = new Vector(); rs = stmt.executeQuery(query); Laptops laptop ; while( rs.next() ){ laptop = new Laptops( rs.getString( 1 ), rs.getString( 2 ),Integer.parseInt(rs.getString( 3 )),Integer.parseInt(rs.getString( 4 )); records.add(laptop); session.putValue("ShopLaptops", records); rs.close(); stmt.close(); } }catch(ClassNotFoundException cnf){ out.println(cnf.toString()); }catch(SQLException sql){ out.println(sql); } %> <form action="http://localhost:8080/jsp-examples/admin/DisplayLaptopsListing.jsp" method="post"> <input type="submit" value="Show Laptops"> </form> </body> </html> the second JSP(DisplayListingLaptops.jsp) <%@ page import="java.sql.*" %> <%@ page import="java.io.*" %> <%@ page session= "true" import="java.util.*, seham.Laptops" %> <% Vector LaptopsList = (Vector)session.getValue("ShopLaptops"); if (LaptopsList != null && (LaptopsList.size()> 0)) { %> <HTML> <HEAD> <TITLE>Laptops List</TITLE> </HEAD> <BODY BGCOLOR= "#FDF5E6" > <center> <H1>Laptops For Sale</H1> <table border="0" cellpadding="0" width="100%" bgcolor="#FDF5E6"> <% for (int index = 0; index < LaptopsList.size(); index++){ Laptops laptop ; laptop = (Laptops)LaptopsList.elementAt(index); <tr> <td><b><%= laptop.getLaptop_id() %></b></td> <td><b><%= laptop.getLaptopDesc() %></b></td> <td><b><%= laptop.getLaptopQuantity() %></b></td> <td><b><%= laptop.getLaptopPrice() %></b></td> <td> <form name="BUY" action="http://localhost:8080/myServlets/servlet/ShoppingCartController1" method="POST"> <input type="submit" value="BUY"> <input type="hidden" name= "index" value='<%= index %>'> <input type="hidden" name="action" value="BUY"> </form> </td> </tr> <% }// end for %> </table> </center>

<% } //end if %> </body> </html> here is the errors that I got: org.apache.jasper.JasperException: Unable to compile class for JSP An error occurred at line: 12 in the jsp file: /admin/LaptopsListing.jsp Generated servlet error: Syntax error, insert ")" to complete Expression org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) root cause org.apache.jasper.JasperException: Unable to compile class for JSP An error occurred at line: 12 in the jsp file: /admin/LaptopsListing.jsp Generated servlet error: Syntax error, insert ")" to complete Expression org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84) org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328) org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:414) org.apache.jasper.compiler.Compiler.compile(Compiler.java:297) org.apache.jasper.compiler.Compiler.compile(Compiler.java:276) org.apache.jasper.compiler.Compiler.compile(Compiler.java:264) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:305) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

I would really appreiate your help.

Sehama at 2007-7-10 11:09:49 > top of Java-index,Java Essentials,Java Programming...
# 6

> Well I think everybody believes you posted your

> message by mistake and you will soon post a real

> question that can be answered ...

I'm losing belief actually (although he's trying hard)

Seham, please surround your code with [code] and [/code] tags, as explained in the [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url].

TimTheEnchantora at 2007-7-10 11:09:49 > top of Java-index,Java Essentials,Java Programming...
# 7

Please use code tags when posting code.

There are several mistakes concerning the HTML code, but this is a minor problem. Some things I found:

- In the session object, setValue() and getValue() have been deprecated and replaced by setAttribute() and getAttribute()

- There's a closing bracket ")" missing at the end of this line:

laptop = new Laptops( rs.getString( 1 ), rs.getString( 2 ),Integer.parseInt(rs.getString( 3 )),Integer.parseInt(rs.getString( 4 )));

- Afaics, you close both ResultSet and Statement inside the query loop; this should be done after the loop

<%@ page import="java.sql.*" %>

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

<%@ page session= "true" import="java.util.*, seham.Laptops" %>

<html>

<head><title> Laptops page </tilte>

</html>

<body>

<%

Connection conn = null;

Statement stmt = null;

String query ;

ResultSet rs = null;

HttpSession session;

try{

Class.forName("org.postgresql.Driver");

conn = DriverManager.getConnection("jdbc:postgresql:shopdb","postgres","07101981");

stmt = conn.createStatement();

query = "SELECT * FROM laptops";

Vector records = new Vector();

rs = stmt.executeQuery(query);

Laptops laptop ;

while( rs.next() ){

laptop = new Laptops( rs.getString( 1 ), rs.getString( 2 ),Integer.parseInt(rs.getString( 3 )),Integer.parseInt(rs.getString( 4 ));

records.add(laptop);

session.putValue("ShopLaptops", records);

rs.close();

stmt.close();

}

}

catch(ClassNotFoundException cnf){

out.println(cnf.toString());

}

catch(SQLException sql){

out.println(sql);

} %>

<form action="http://localhost:8080/jsp-examples/admin/DisplayLaptopsListing.jsp" method="post">

<input type="submit" value="Show Laptops">

</form>

</body>

</html>

the second JSP(DisplayListingLaptops.jsp)

<%@ page import="java.sql.*" %>

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

<%@ page session= "true" import="java.util.*, seham.Laptops" %>

<%

Vector LaptopsList = (Vector)session.getValue("ShopLaptops");

if (LaptopsList != null && (LaptopsList.size()> 0)) {

%>

<HTML>

<HEAD> <TITLE>Laptops List</TITLE> </HEAD>

<BODY BGCOLOR= "#FDF5E6" >

<center>

<H1>Laptops For Sale</H1>

<table border="0" cellpadding="0" width="100%" bgcolor="#FDF5E6">

<%

for (int index = 0; index < LaptopsList.size(); index++){

Laptops laptop ;

laptop = (Laptops)LaptopsList.elementAt(index);

<tr>

<td><b><%= laptop.getLaptop_id() %></b></td>

<td><b><%= laptop.getLaptopDesc() %></b></td>

<td><b><%= laptop.getLaptopQuantity() %></b></td>

<td><b><%= laptop.getLaptopPrice() %></b></td> <td>

<form name="BUY" action="http://localhost:8080/myServlets/servlet/ShoppingCartController1" method="POST">

<input type="submit" value="BUY">

<input type="hidden" name= "index" value='<%= index %>'>

<input type="hidden" name="action" value="BUY">

</form>

</td>

</tr>

<%

}// end for

%>

</table>

</center>

<%

} //end if

%>

</body>

</html>

quittea at 2007-7-10 11:09:49 > top of Java-index,Java Essentials,Java Programming...
# 8

Hi,

I have corrected the code as you have explained but I got another two errors:

I am so happy with your help

HTTP Status 500 -

--

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 12 in the jsp file: /admin/LaptopsListing.jsp

Generated servlet error:

Duplicate local variable session

An error occurred at line: 12 in the jsp file: /admin/LaptopsListing.jsp

Generated servlet error:

The constructor Laptops(String, String, int, int) is undefined

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 12 in the jsp file: /admin/LaptopsListing.jsp

Generated servlet error:

Duplicate local variable session

An error occurred at line: 12 in the jsp file: /admin/LaptopsListing.jsp

Generated servlet error:

The constructor Laptops(String, String, int, int) is undefined

org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)

org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)

org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:414)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:297)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)

org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:305)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.

--

Apache Tomcat/5.5.20

Sehama at 2007-7-10 11:09:49 > top of Java-index,Java Essentials,Java Programming...
# 9

you've declared a variable called 'session' in your JSP by the look of it. don't. there already is one. also, you're not using your Laptops class properly, I don't know how since I don't know what it looks like, but still.....

kudos on taking so long to actually get round to posting a question, by the way

georgemca at 2007-7-10 11:09:49 > top of Java-index,Java Essentials,Java Programming...
# 10
Right, I didn't see it. Delete the lineHttpSession session; because the session object is already pre-defined within a JSP.
quittea at 2007-7-10 11:09:49 > top of Java-index,Java Essentials,Java Programming...
# 11
> Right, I didn't see it. Delete the line> > HttpSession session; > > because the session object is already pre-defined> within a JSP.heh heh I didn't even look at his code. it's all there in the stack trace
georgemca at 2007-7-10 11:09:49 > top of Java-index,Java Essentials,Java Programming...
# 12
> heh heh I didn't even look at his code. it's all there in the stack traceI only read the start of the stacktrace, that's why I missed out the Laptop constructor issue :P
quittea at 2007-7-10 11:09:49 > top of Java-index,Java Essentials,Java Programming...
# 13
Hi,I have corrected all errors and I am getting non, but all I got in the browser when I run LaptopsListing.jsp, is only a blank page! is there still anything wrong with my code!?/Thanks a million for your grateful help.
Sehama at 2007-7-10 11:09:49 > top of Java-index,Java Essentials,Java Programming...
# 14

> > heh heh I didn't even look at his code. it's all

> there in the stack trace

>

> I only read the start of the stacktrace, that's why I

> missed out the Laptop constructor issue :P

at least you got that far! I think I'm going to write a "reading the stack trace for dummies" tutorial or something, so so many people here would benefit from getting that skill down earlier.....(not aimed at anyone in particular)

georgemca at 2007-7-10 11:09:49 > top of Java-index,Java Essentials,Java Programming...
# 15

Some problems within HTML code:

<html>

<head><title> Laptops page </tilte>

</html>

The closing tag should be "head", not "html".

And additionally, you should replace

<form action="http://localhost:8080/jsp-examples/admin/DisplayLaptopsListing.jsp" method="post">

by

<form action="<%= response.encodeURL("/jsp-examples/admin/DisplayLaptopsListing.jsp") %>" method="post">

so the session can be passed through.

quittea at 2007-7-21 18:37:16 > top of Java-index,Java Essentials,Java Programming...
# 16

> at least you got that far! I think I'm going to write

> a "reading the stack trace for dummies" tutorial or

> something, so so many people here would benefit from

> getting that skill down earlier.....(not aimed at anyone in particular)

Add a section like "The compiler isn't kidding", cause some people just don't seem to believe that the compiler is (almost) always right.

quittea at 2007-7-21 18:37:16 > top of Java-index,Java Essentials,Java Programming...
# 17
Hi quitte,I have tried that as well but I still have a blank page.what do you think is wrong with my page.Thanks indeed.
Sehama at 2007-7-21 18:37:16 > top of Java-index,Java Essentials,Java Programming...
# 18

Hi agan quitte,

I managed to got the DisplayingLaptopsListing.jsp to work and i have displayed all the records of the alptop table in that page. but when i press the button (buy) it suppose to go to the ShoppingCartServlet1 that receives this hidden parameter that called (action) but when i press on it i got the error:

java.lang.NullPointerException

ShoppingCartController1.doPost(ShoppingCartController1.java:47)

javax.servlet.http.HttpServlet.service(HttpServlet.java:709)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

I have tried to declare the Vector that inclueds the laptops records into the servlet and I still have tha same error.

also i have declared the Laptops.class in a package to be accesses by the the JSP page but when I want to call it from the Servlet, it gives me an error saying that Laptops class can not be accessed, make sure its in the right directory.

I have the Laptops.class in myServlets/WEB-INF/classes

here is the code of the ShoppingCartController1.java

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

public class ShoppingCartController1 extends HttpServlet {

//*******************

HttpSession thisSession;

Vector ShoppingCartVector = null;

Vector LaptopsList;

Laptops laptop;

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws IOException, ServletException{

thisSession = request.getSession();

laptopsList = (Vector)session.getAttribute("ShopLaptops");

}

//********************

public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{

String action = request.getParameter("action");

RequestDispatcher rd;

String url = "";

//*************************************************

if (action.equals("BUY") ) {

String sindex = request.getParameter("index");

int index = Integer.parseInt(sindex);

laptop = (Laptops)latopsList.get(index);

System.out.println( laptop.toString() );

if (ShoppingCartVector==null) {

ShoppingCartVector = new Vector();

ShoppingCartVector.add(laptop);

}else{

ShoppingCartVector.add(laptop);

}

thisSession.setAttribute("ShoppingCart",ShoppingCartVector);

rd = getServletContext().getRequestDispatcher("./admin/DisplayLaptopsListing.jsp");

rd.forward(request, response);

}

}

}

I would really appreciate your help.

Sehama at 2007-7-21 18:37:16 > top of Java-index,Java Essentials,Java Programming...
# 19
thisSession is null. it's only ever assigned in doGet, you're in doPost
georgemca at 2007-7-21 18:37:16 > top of Java-index,Java Essentials,Java Programming...
# 20

Ugh. Don't use instance variables in your servlet, as soon as you get two simultaneous requests from different users, their data is going to get all mixed up together. Put that data into the session if you need to store it between two requests. And always get the session from the current request. Getting it from the previous request is a bad idea because the previous request might have come from a different user.

And there is a Servlets forum. Posting servlet questions here is a bad idea because you are asking the wrong people.

DrClapa at 2007-7-21 18:37:16 > top of Java-index,Java Essentials,Java Programming...
# 21
Hi,how can I get the servlet forum that you are talking about.Thanks for relpying back.
Sehama at 2007-7-21 18:37:16 > top of Java-index,Java Essentials,Java Programming...
# 22
> Hi,> how can I get the servlet forum that you are talking> about.> > Thanks for relpying back.more or less the same way you got here. but I'm [url= http://forum.java.sun.com/forum.jspa?forumID=33]feeling generous today[/url]
georgemca at 2007-7-21 18:37:16 > top of Java-index,Java Essentials,Java Programming...