An exception occurred processing JSP page

I have Apache Tomcat server default installation on local Windows 7 machine, works well for other projects.
But I have problems with this simple HTML and JSP project:

hobby.html

<html>
	<body>
		<form method="POST"	action="HobbyPage.jsp">
			Choose a hobby:<p>
			<select name="hobby" size="1">
				<option>horse skiing
				<option>extreme knitting
				<option>alpine scuba
				<option>speed dating
			</select>
			<br><br>
			<center>
				<input type="SUBMIT">
			</center>
		</form>
	</body>
</html>

Open in new window


HobbyPage.jsp

<%@ page import ="java.util.*"%>
<html>
<body>
	The friends who share your hobby of
	<%= request.getParameter("hobby")%>
	are: <br>
	<% ArrayList al = (ArrayList) request.getAttribute("names"); %>
	<% Iterator it = al.iterator();
	   while (it.hasNext()) {%>
	   <%=it.next()%>
	   <br>
	   <%}%>
</body>
</html>

Open in new window


When I open hobby.html form works OK, but when I select option and click submit button I get this error:

HTTP Status 500 - An exception occurred processing JSP page /HobbyPage.jsp at line 8

type Exception report

message An exception occurred processing JSP page /HobbyPage.jsp at line 8

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

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /HobbyPage.jsp at line 8

5: 	<%= request.getParameter("hobby")%>
6: 	are: <br>
7: 	<% ArrayList al = (ArrayList) request.getAttribute("names"); %>
8: 	<% Iterator it = al.iterator();
9: 	   while (it.hasNext()) {%>
10: 	   <%=it.next()%>
11: 	   <br>


Stacktrace:
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NullPointerException
	org.apache.jsp.HobbyPage_jsp._jspService(HobbyPage_jsp.java:76)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.54 logs.

Open in new window


What I am doing wrong?
KPaxAsked:
Who is Participating?
 
Jeffrey DakeConnect With a Mentor Senior Director of TechnologyCommented:
Oh I am sorry.  I gave you the open bracket for the if conditional, but forgot the closing one for the for loop.

	The friends who share your hobby of
	<%= request.getParameter("hobby")%>
	are: <br>
	<% String[] values = request.getParameterValues("names"); 
	if (values != null)
        {
            for(int i = 0; i < values.length ; i++){
        %>
        <%=values[i]%>
<%   }
         } %>

Open in new window

0
 
Jeffrey Dake Senior Director of TechnologyCommented:
You should go into your tomcat directory and find the work directory. Inside there you can find the compiled version of the map to see what exactly is on line 76. I think it is most likely your cast of request.getattribute to an array list. The getattribute is most likely returning null
0
 
KPaxAuthor Commented:
Line 76 is
Iterator it = al.iterator();

Open in new window

Yes, getattribute is most likely returning null, but why?
Shouldn't it return the value selected on HTML form?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
rrzCommented:
Shouldn't it return the value selected on HTML form?
No, the form has the request parameter "hobby". A request attribute is different. You have set it with something like
 request.setAttribute("names", yourList);

Open in new window

0
 
Jeffrey DakeConnect With a Mentor Senior Director of TechnologyCommented:
I think what you are really intending to do is call request.getParameterValues("names"), which will give you an array of Strings.

http://docs.oracle.com/javaee/5/api/javax/servlet/ServletRequest.html#getParameterValues
0
 
KPaxAuthor Commented:
rrz2015-02-06 at 17:59:19ID: 40594042
Shouldn't it return the value selected on HTML form?
No, the form has the request parameter "hobby". A request attribute is different. You have set it with something like
request.setAttribute("names", yourList);

It's from the book "Servlets and JSP, 2ed", they say this is the proper way for this to be done?

Jeffrey Dake2015-02-06 at 18:08:08ID: 40594054
I think what you are really intending to do is call request.getParameterValues("names"), which will give you an array of Strings.

http://docs.oracle.com/javaee/5/api/javax/servlet/ServletRequest.html#getParameterValues

If I try that then I got:
HTTP Status 500 - Unable to compile class for JSP:

type Exception report

message Unable to compile class for JSP:

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: 7 in the jsp file: /HobbyPage.jsp
Cannot cast from String[] to ArrayList
4: 	The friends who share your hobby of
5: 	<%= request.getParameter("hobby")%>
6: 	are: <br>
7: 	<% ArrayList al = (ArrayList) request.getParameterValues("names"); %>
8: 	<% Iterator it = al.iterator();
9: 	   while (it.hasNext()) {%>
10: 	   <%=it.next()%>


Stacktrace:
	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
	org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:476)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.54 logs.

Open in new window

0
 
Jeffrey DakeConnect With a Mentor Senior Director of TechnologyCommented:
Yeah you will need to get rid of the cast.  getParameterValues() returns a String[] not an ArrayList

Something like this.

 	The friends who share your hobby of
	<%= request.getParameter("hobby")%>
	are: <br>
	<% String[] values = request.getParameterValues("names"); 
	if (values != null)
        {
            for(int i = 0; i < values.length ; i++){
        %>
        <%=values[i]%>
<%   } %>

Open in new window

0
 
Jeffrey DakeConnect With a Mentor Senior Director of TechnologyCommented:
As for getAttribute and setAttribute.  Those are fields that you can set in the request and use later.  If you are processing form values or anything passed through a URL as a query string, those will come in as parameters, not attributes.
0
 
rrzConnect With a Mentor Commented:
It's from the book "Servlets and JSP, 2ed", they say this is the proper way for this to be done?
I don't have the  book.   But, somewhere they must have set the attribute.
As I posted, parameters and attributes are different things. See
http://www.xyzws.com/servletfaq/what-is-the-difference-between-the-request-attribute-and-request-parameter/1   
http://learnjava4enterprise.blogspot.com/2013/01/difference-between-attribute-and.html
0
 
KPaxAuthor Commented:
Jeffrey Dake2015-02-06 at 19:34:51ID: 40594262
Yeah you will need to get rid of the cast.  getParameterValues() returns a String[] not an ArrayList

Something like this.

 	The friends who share your hobby of
	<%= request.getParameter("hobby")%>
	are: <br>
	<% String[] values = request.getParameterValues("names"); 
	if (values != null)
        {
            for(int i = 0; i < values.length ; i++){
        %>
        <%=values[i]%>
<%   } %>

Open in new window


With that I got this:

HTTP Status 500 - Unable to compile class for JSP:
type Exception report
message Unable to compile class for JSP:
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: [37] in the generated java file: [C:\Program Files\Apache Software Foundation\Tomcat 7.0\work\Catalina\localhost\hobby\org\apache\jsp\HobbyPage_jsp.java]
Syntax error, insert "}" to complete ClassBody

An error occurred at line: [85] in the generated java file: [C:\Program Files\Apache Software Foundation\Tomcat 7.0\work\Catalina\localhost\hobby\org\apache\jsp\HobbyPage_jsp.java]
Syntax error, insert "}" to complete Block

An error occurred at line: [85] in the generated java file: [C:\Program Files\Apache Software Foundation\Tomcat 7.0\work\Catalina\localhost\hobby\org\apache\jsp\HobbyPage_jsp.java]
Syntax error, insert "else Statement" to complete IfStatement

An error occurred at line: [85] in the generated java file: [C:\Program Files\Apache Software Foundation\Tomcat 7.0\work\Catalina\localhost\hobby\org\apache\jsp\HobbyPage_jsp.java]
Syntax error, insert "}" to complete Block

An error occurred at line: [97] in the generated java file: [C:\Program Files\Apache Software Foundation\Tomcat 7.0\work\Catalina\localhost\hobby\org\apache\jsp\HobbyPage_jsp.java]
Syntax error on token "}", delete this token

Stacktrace:
	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
	org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:476)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.54 logs.

Open in new window

0
 
KPaxAuthor Commented:
I don't have the  book.   But, somewhere they must have set the attribute.
As I posted, parameters and attributes are different things. See
http://www.xyzws.com/servletfaq/what-is-the-difference-between-the-request-attribute-and-request-parameter/1   
http://learnjava4enterprise.blogspot.com/2013/01/difference-between-attribute-and.html

Yes I know they are not the same, that's where my confusion and that's why I asked to actually try to write and run that code. Strange enough, there's no errata for this piece of code. Anyway, thanks for the links.
0
All Courses

From novice to tech pro — start learning today.