Solved

An exception occurred processing JSP page

Posted on 2015-02-06
11
1,220 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 10

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 450 total points
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 10

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 450 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction The Google Maps API offers various ways to draw dynamic and static maps.  Using a combination of PHP and JavaScript, you can draw active JavaScript maps that allow pan-and-zoom in the client browser window.  You can also draw "static" …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now