Error in JSP Page : Unable to compile class for JSP

I have a login page which I enter my username and password and click submit. It goes tovalidate.jsp as shown below. When this is invoked i get a strange error as shown beow the code

<html>

<body>

<%@ page errorPage="errorpage.jsp" language="java" import="java.sql.*" %>

<%

Connection conn =null;

ResultSet rs =null;

try{

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

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

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

conn = DriverManager.getConnection("jdbc:odbc:bob","scott","tiger");

PreparedStatement statement= conn.prepareStatement("select * from users where username=? and password =?");

statement.setString(1, username);

statement.setString(2, password);

rs = statement.executeQuery();

if(rs!=null && rs.next()){

session.putValue("user",username);

if(username.equals("admin")){

response.sendRedirect("admin.htm");

}

else{

out.println("Logged IN");

response.sendRedirect("SearchCriteria.jsp");

}

}

else{

response.sendRedirect("invalidUser.htm");

}

}

catch(Exception e){

out.println("Error "+e.printStackTrace());

}

finally{

rs.close();

conn.close();

}

%>

</body>

</html>

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: 32 in the jsp file: /validate.jsp

The operator + is undefined for the argument type(s) String, void

29:

30: }

31: catch(Exception e){

32: out.println("Error "+e.printStackTrace());

33: }

34: finally{

35: rs.close();

Stacktrace:

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

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

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

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

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

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

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

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

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

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

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

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

[4100 byte] By [hemanthjavaa] at [2007-11-27 8:10:19]
# 1

The operator + is undefined for the argument type(s) String, void

29:

30: }

31: catch(Exception e){

32: out.println("Error "+e.printStackTrace());

33: }

The method e.printStackTrace returns void which can not be used with the String concatenation operator +.

Try

out.println("Error "+e.getMessage());

tolmanka at 2007-7-12 19:53:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
To do what you want to do, you will need to print the error message:e.getMessage() and if you want the Stack trace, you will need to loop through the array of StackTraceElement's after doing a e.getStackTrace() and prinitng out each element usign the .toString() method.
llemonsa at 2007-7-12 19:53:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
or just pull out the printStacktrace like so:29:30: }31: catch(Exception e){32: e.printStackTrace();33: }
tadchristiansena at 2007-7-12 19:53:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

I get the follwing error after making the changes. Is it because no username and password were found in the DB for the particular inputs.

The stack trace of the error is:

java.lang.NullPointerException java.lang.NullPointerException at org.apache.jsp.validate_jsp._jspService(validate_jsp.java:88) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Unknown Source)

hemanthjavaa at 2007-7-12 19:53:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

NullPointerException - something somewhere is null.

I think it is probably the username/password that you pass in as parameters.

If those are null (not provided) then you would get a null pointer exception from the line

if (userName.equals("admin"))

suggested fix:

if ("admin".equals(userName) )

evnafetsa at 2007-7-12 19:53:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...