Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

An exception occurred processing JSP page

Posted on 2015-02-06
11
Medium Priority
?
2,426 Views
Last Modified: 2015-02-06
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?
0
Comment
Question by:KPax
  • 5
  • 4
  • 2
11 Comments
 
LVL 11

Expert Comment

by:Jeffrey Dake
ID: 40593851
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
 

Author Comment

by:KPax
ID: 40593859
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
 
LVL 28

Expert Comment

by:rrz
ID: 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);

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 1800 total points
ID: 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
0
 

Author Comment

by:KPax
ID: 40594223
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
 
LVL 11

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 1800 total points
ID: 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

0
 
LVL 11

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 1800 total points
ID: 40594272
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
 
LVL 28

Assisted Solution

by:rrz
rrz earned 200 total points
ID: 40594275
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
 

Author Comment

by:KPax
ID: 40594292
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
 
LVL 11

Accepted Solution

by:
Jeffrey Dake earned 1800 total points
ID: 40594306
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
 

Author Comment

by:KPax
ID: 40594320
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question