Solved

An exception occurred processing JSP page

Posted on 2015-02-06
11
1,873 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

729 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