Reputation: 551
Something truly bizarre is happening with my Tomcat7 deployment. Either that, or I'm way too tired. Here's the skinny...
I have a very simple Tomcat7 webapp that I'm developing through Eclipse(Juno). One of my JSP files includes a form. When the form is submitted, a servlet queries the database and returns two parameters back to the same jsp via session.setAttribute.
Everything works fine, but if I start mucking around with the servlet, I suddenly get an HTTP 500 errors on the JSP page. The only way I've been able to resolve it is to go through a bizarre ritual of removing all Java-related content from the JSP, then slowly adding it in and refreshing. I can't tell if this is a Tomcat issue, Servlet issue, Firefox issue, or some combination of the above.
For clarity, here are some code snippets:
Jsp Code:
<html>
...
<body>
<script type='text/javascript'>
var searchResults = '<%= ((String[][])session.getAttribute("searchResults")).length %>';
var additionalResults = '<%= session.getAttribute("additionalResults") %>';
...
</script>
...
</body>
</html>
Tomcat Error:
org.apache.jasper.JasperException: An exception occurred processing JSP page /test.jsp at line 32
30: <body>
31: <script type='text/javascript'>
32: var searchResults = '<%= ((String[][])session.getAttribute("searchResults")).length %>';
33: var additionalResults = '<%= session.getAttribute("additionalResults") %>';
34:
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:722)
root cause
java.lang.NullPointerException
org.apache.jsp.test_jsp._jspService(test_jsp.java:93)
I know for a FACT that the database is returning proper values, so line 32 should not be throwing an exception or a NullPointer. Line 93 is simply:
<div id="wrapper">
I even get the Tomcat error when line 32 is commented out?!?!
To try to debug, I simplified the contents of the script tag to be:
alert('<%= session.getAttribute("searchResults") %>');
And edited my servlet to read:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String redirectedPage = "/test.jsp";
request.getSession().setAttribute("searchResults", "Bob");
RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher(redirectedPage);
reqDispatcher.forward(request,response);
}
}
But the resultant popup shows 'null'. I've also tried restarting MySQL and Tomcat many times, in various order.
So I'm at a complete loss as to what's going on. Any input would be greatly appreciated.
Upvotes: 1
Views: 6835
Reputation: 551
I forgot to post the answer to this. It turned out I was closing the JDBC connection, so the ResultSet object was inaccessible by the time it came back to the jsp. I've since updated that code and moved the results out of the ResultSet object and into parameters that I pass back via the HTTPRequest object.
Upvotes: 1
Reputation: 1046
use this for your jsp code
if('<%= session.getAttribute("searchResults") %>' !="null")
{
var str = '<%= (String)session.getAttribute("searchResults") %>'
searchResults = str.length;
alert(searchResults)
}
Upvotes: 0