Solved

An exception occurred processing JSP page

Posted on 2015-02-06
11
1,569 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 10

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 27

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 10

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 450 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 10

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 450 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 10

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 450 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 27

Assisted Solution

by:rrz
rrz earned 50 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 10

Accepted Solution

by:
Jeffrey Dake earned 450 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add image on html mail 6 27
Asp in script 6 36
running on tomcat not jboss eap 7.0 3 18
Angular won't showing my array result in HTML 3 24
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn how to implement Singleton Design Pattern in Java.

840 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