I have experienced this strange error, and now I will explain when this happens:
basically what I have to do is having an HTML form (or JSP, doesn't really matter) with a simple textfield and two buttons, one for submitting to the .jsp page and the other one is just to reset the textfield.
Everything is working fine, except when I try to write in the textfield something with spaces (example: "abc 123").
If I simply write "abc123" it doesn't give any error 500 though.
I hope some of you can help me, here are the sources:
<html>
<head>`<html>
<head>
<meta charset="ISO-8859-1">
<title>HTML Form</title>
</head>
<body>
<form name = "intoll" method = "post" action = "send.jsp">
Insert text: <input type = "text" name = "food" id = "food">
<br/>
<input type = "submit" name = "send" value = "Send">
<input type = "reset" name = "Reset">
</form>
</body>
send.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="com.athirat.intolleranze.foods.*" %>
<%
String food = request.getParameter("food").trim();
if (food != null && food != "") {
food = food.toLowerCase();
out.print(food);
NetMaster.init(food);
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<%
try {
out.print(NetMaster.showContent());
} catch (Exception e) {
throw new Exception("String error");
}
%>
</body>
</html>
Thanks for helping me.
EDIT:
Stacktrace
giu 16, 2015 3:06:12 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [jsp] in context with path [/intoll] threw exception [An exception occurred processing JSP page /send.jsp at line 22
19: try {
20: out.print(NetMaster.showContent());
21: } catch (Exception e) {
22: throw new Exception("String error");
23: }
24: %>
25: </body>
Stacktrace:] with root cause
java.lang.Exception: Errore nella stringa
at org.apache.jsp.send_jsp._jspService(send_jsp.java:113)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
giu 16, 2015 3:23:56 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [jsp] in context with path [/intoll] threw exception [An exception occurred processing JSP page /send.jsp at line 22
19: try {
20: out.print(NetMaster.showContent());
21: } catch (Exception e) {
22: throw new Exception("String error");
23: }
24: %>
25: </body>
Stacktrace:] with root cause
java.lang.Exception: Errore nella stringa
at org.apache.jsp.send_jsp._jspService(send_jsp.java:113)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
This is the method showContent with it's called methods:
public static String showContent() throws Exception {
if (isFood())
return getContent();
else
return "LOG Creato: " + generateLogFile();
}
private static String generateLogFile() throws Exception {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String logname = "foodLog_" + sdf.format(cal.getTime());
String logdir = ps.getProperty("logfolder");
File f = new File ("." + logdir + logname + ".log");
FileOutputStream fout = new FileOutputStream(f);
DataOutputStream dout = new DataOutputStream(fout);
System.out.println(logname);
System.out.println(logdir);
System.out.println(f.getParent());
dout.writeBytes(food + " non esistente, si prega di aggiungerlo");
fout.close();
return logname;
}
private static String getContent() {
String result = "{[";
#SuppressWarnings("resource")
Scanner sc = new Scanner(inputReader).useDelimiter("\\A");
result += sc.hasNext() ? sc.next() : "";
return result;
}
I solved:
showContent() throws an Exception, and I only need to catch that.
The error was caused because I used to throw a new exception in the try-catch of showContent(), something I couldn't do for the reason above.
Thanks everybody.
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
Create a servlet that uses cookies to store the number how many times user has visited?-That's the question im trying to solve
this is my html
<html>
<head>
</head>
<body>
<form action="NewServlet">
<input type="text" name="text1">
<input type="submit"/>
</form>
</body>
</html>
this is NewServlet.java
public class NewServlet extends HttpServlet {
public void service(HttpServletRequest req,HttpServletResponse res)throws
IOException,ServletException
{
res.setContentType("text/html");
Cookie[] cook=req.getCookies();
String s=req.getParameter("text1");
int a=0;
if(cook.length>0)
{
for(Cookie cookie :cook)
{
if(cookie.getName().equals(s))
{
int b=Integer.parseInt(cookie.getValue());
b=b+1;
cookie.setValue(String.valueOf(b));
a++;
}
}
}
if(a>0)
{
res.sendRedirect("NewServlet1");
}
else
{
Cookie cok=new Cookie(s,"1");
res.addCookie(cok);
req.setAttribute("user", s);
RequestDispatcher r=req.getRequestDispatcher("NewServlet1");
r.forward(req,res);
}
}
}
and this is NewServlet1.java
public class NewServlet1 extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html;charset=UTF-8");
Cookie[] cookie=req.getCookies();
String s=String.valueOf(req.getAttribute("user"));
for(Cookie cok:cookie)
{
if(cok.getName().equals(s))
{
int a=Integer.parseInt(cok.getValue());
res.getWriter().println("number of times visited"+a);
}
}
}
}
what i am doing is checking if cookie exist or not and if no then creating the cookie on NewServlet.java .But i am getting a Null-Pointer Exception (Error 500)
on clicking the submit button
stacktrace
at First.NewServlet.service(NewServlet.java:28)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Change if(cook.length>0) to if(cook !=null && cook.length > 0). This way if cook is null then it will not check the .length and hence avoid the NPE
I am building a simple Train booking system, now i am trying to call a servlet once i open a jsp to select data from the database then return back to the same servlet, but i keep getting errors.
this is my trip.jsp
<body>
<% if ((request.getAttribute("trips") == null)) {
%>
<jsp:include page="/trips" flush="true" />
<% } %>
<table>
<tr>
<th>Trip Number</th>
<th>Train Number</th>
<th>Source</th>
<th>Destination</th>
<th>Departure Time</th>
<th>Arrival Time</th>
<th>Date</th>
<th>Reserved Seats</th>
<th>Price</th>
</tr>
<%
//ArrayList list = new ArrayList();
List trip = new ArrayList();
Iterator viewTrip;
if(request.getAttribute("trips")!=null && request.getAttribute("trips")!=""){
List allTrips = (ArrayList)request.getAttribute("trips");
Iterator itr = allTrips.iterator();
while(itr.hasNext()){
trip = (ArrayList)itr.next();
viewTrip = trip.iterator();
%>
<tr>
<%
while(viewTrip.hasNext()){
%>
<td>
<%=viewTrip.next()%>
</td>
<%
}
%>
</tr>
<%
}
}
%>
</table>
</body>
and this is my servlet. trips.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
Connection con = DB.getActiveConnection();
Statement stmt = con.createStatement();
ResultSet rs = null;
ArrayList trip = new ArrayList();
ArrayList data = null;
try {
String query = "select * from trips";
rs = stmt.executeQuery(query);
while(rs.next()){
data = new ArrayList();
data.add(rs.getString(9)); // trip number
data.add(rs.getInt(1)); // train number
data.add(rs.getString(2)); // source
data.add(rs.getString(3)); // destination
data.add(rs.getTime(4)); //departure time
data.add(rs.getTime(5));// arrival time
data.add(rs.getDate(6));// date
data.add(rs.getInt(7));// reserved seats
data.add(rs.getDouble(8));//price
trip.add(data);
}
request.setAttribute("trips", trip);
con.close();
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/updateTrip.jsp");
requestDispatcher.include(request, response);
//out.close();
//return;
}
catch (Exception e) {
e.printStackTrace();
}
}
and this is the error i get
25-Dec-2016 15:47:01.822 SEVERE [http-nio-8084-exec-4]
org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [jsp] in context with path [/Train] threw exception [java.lang.IllegalStateException: Exception occurred when flushing data] with root cause
java.io.IOException: Stream closed
at org.apache.jasper.runtime.JspWriterImpl.ensureOpen(JspWriterImpl.java:200)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:105)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:184)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:120)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
at org.apache.jsp.updateTrip_jsp._jspService(updateTrip_jsp.java:199)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
You are using try with resources, which automatically closes the stream while exiting the try block.
try (PrintWriter out = response.getWriter()) {
....
}
trip.jsp passes request and response objects to the servlet trips.java, where you get the PrintWriter of the response (and for some reason, not using it anywhere) in a try with resource, which will close the underlying stream at the end of the try block. Now, when your include is finished and control comes back to the trip.jsp and try to write the jsp content, it'll fail as the stream is already closed.
Just change your code as:
PrintWriter out = response.getWriter();
try {
....
} catch (IOException e) {
....
}
or if you don't want to handle the exception here, just remove the try altogether.
I am trying to display a list on the jsp (on its loading) fetched from controller. The controller is called by ajax, and the list is also received by ajax function as a Json response, but it gives me following error:
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:478)
at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotWritable(DefaultHandlerExceptionResolver.java:344)
at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException(DefaultHandlerExceptionResolver.java:131)
at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:136)
at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1120)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:944)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:279)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
However, if the list is returned empty then the success of ajax function is called, so the problem revolves around returning the arraylist as a response.
Below is my jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js> </script>
</head>
<script>
$(document).ready(function() {
var path = window.location.href;
;
alert(path);
$.ajax({
type : "GET",
url : "displaySurvey.htm",
dataType: "json",
data : {
name : window.location.href
},
success : function(response) {
alert("success");
displayTheContent(response);
},
});
});
function displayTheContent(response) {
alert("displayResponse");
$("#displayDiv").empty();
for (var i = 0, l = response.length; i < l; i++) {
var user = response[i];
$("#displayDiv").append(
"<li>" + user.question + " " + user.option1 + "</li>");
}
}
</script>
<body>
<div id="displayDiv"></div>
</body>
</html>
Controller:
#Controller
#RequestMapping("/displaySurvey.htm")
public class DisplaySurveyController extends javax.servlet.http.HttpServlet {
#RequestMapping(method = RequestMethod.GET)
public #ResponseBody ArrayList<SurveyQuestions> submit(#RequestParam("name") String request)
throws AdException {
String surveyId = request.substring(request.lastIndexOf("=") + 1);
try {
SurveyDAO surveyDao = new SurveyDAO();
ArrayList<SurveyQuestions> questionList = (ArrayList<SurveyQuestions>) surveyDao.getSurveyQuestions(surveyId);
return questionList;
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception: " + e.getMessage());
throw new AdException("Could not list the categories", e);
}
}
}
Can anyone help me with this please.
Here's the code I'm working with:
title = request.getParameter("title");
author = request.getParameter("author");
search(title, author);
ArrayList<Book>books = new ArrayList();
while(conn.rs.next()){
Book book = new Book(conn.rs.getString(1), conn.rs.getString(2), conn.rs.getString(3));
books.add(book);
}
System.out.println(books.size());
for(int i = 0; i < books.size(); i++){
System.out.println(books.get(i).getBookID());
}
request.setAttribute("Book", books);
RequestDispatcher rd = request.getRequestDispatcher("/jspSearch.jsp");
rd.include(request, response);
JSP:
<c:forEach var="Book" items="${Book}">
<tr>
<td>${Book.bookID}</td>
<td>${Book.title}</td>
<td>${Book.author}</td>
</tr>
</c:forEach>
I am getting a numberFormatException on
rd.forward(request, response);
and
processRequest(request, response);
but I'm not doing anything with parsing a string to a number? I've been stuck on this for most of the day and I'm not doing anything with numbers so
Nov 30, 2015 10:11:57 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [showAllBooks] in context with path [/a3Austin] threw exception [java.lang.NumberFormatException: For input string: "bookID"] with root cause
java.lang.NumberFormatException: For input string: "bookID"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at javax.el.ListELResolver.coerce(ListELResolver.java:157)
at javax.el.ListELResolver.getValue(ListELResolver.java:70)
at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:110)
at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:943)
at org.apache.jsp.jspShowAll_jsp._jspService(jspShowAll_jsp.java:129)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:720)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:466)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:391)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
at web.showAllBooks.processRequest(showAllBooks.java:48)
at web.showAllBooks.doGet(showAllBooks.java:57)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
out.write("\t\t\t\t<td>");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${Books.bookID}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("</td>\r\n");
How can I set property and get it on a jsp page?
my Hello.class,
package Foo;
public class Hello
{
private String message;
public String sayHello ()
{
return "Hello World!";
}
public String getMessage() {
return this.message;
}
public void setMessage(String x) {
this.message = x;
}
}
My index.jsp,
<html>
<head>
<title>Custom Class</title>
</head>
<body>
<%# page import="Foo.Hello" %>
<%
Hello hello = new Hello();
hello.setMessage("Hello There!");
out.print(hello.getMessage());
out.print(hello.sayHello());
%>
</body>
</html>
Error,
org.apache.jasper.JasperException: An exception occurred processing JSP page /custom-class/index.jsp at line 12
9: <%
10: Hello hello = new Hello();
11:
12: hello.setMessage("Hello There!");
13: out.print(hello.getMessage());
14: out.print(hello.sayHello());
15: %>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:461)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
javax.servlet.ServletException: java.lang.NoSuchMethodError: Foo.Hello.setMessage(Ljava/lang/String;)V
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:908)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:837)
org.apache.jsp.custom_002dclass.index_jsp._jspService(index_jsp.java:122)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NoSuchMethodError: Foo.Hello.setMessage(Ljava/lang/String;)V
org.apache.jsp.custom_002dclass.index_jsp._jspService(index_jsp.java:103)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
I'm new to Java and JSP. What have done incorrectly?
I don't find any problem in your code it is working fine i think you have to clean your project and redeploy it on your server and also clean and restart your server then it will surely work.
For people who encounter this problem in the future:
I just had the same problem and I removed tomcat server from "IntelliJ IDEA > Preferences > Application Servers" and I added it again. and it works fine now.