I am trying to set a cookie with user attribute values that I get from an Identity server. I am getting the user attributes but unable to set cookie. Can you someone tell me what is the mistake that I am doing?.
Here is my Java Code:
public class CircleArea extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Server name is: " + request.getServerName());
System.out.println("Requesting cookies...");
Cookie[] cookies = request.getCookies();
String targetResourceURL = request.getRequestURL().toString();
if (cookies == null) {
System.out.println("Cookies are not present");
System.out.println("Requesting reference value...");
String refValue = request.getParameter("REF");
if (refValue == null) {
System.out.println("Reference value is null...");
System.out.println("Redirecting to URL: " + Configurations.PF_BASE_URL + ":"
+ Configurations.PF_PRIMARY_SSL_PORT + Configurations.PF_START_SSO_ENDPOINT + "?"
+ "PartnerIdpId=" + Configurations.PARTNER_ENTITY_ID + "&TargetResource=" + targetResourceURL);
response.sendRedirect(Configurations.PF_BASE_URL + ":" + Configurations.PF_PRIMARY_SSL_PORT
+ Configurations.PF_START_SSO_ENDPOINT + "?" + "PartnerIdpId="
+ Configurations.PARTNER_ENTITY_ID + "&TargetResource=" + targetResourceURL);
} else {
try {
System.out.println("Reference Value is: " + refValue);
System.out.println("Getting user attributes...");
Map<String, String> userAttrs = UserAttributes.pickUpAttributesFromReferenceIdAdapter(refValue);
for(Map.Entry<String, String> userAttr : userAttrs.entrySet()){
System.out.println("Key: " + userAttr.getKey() + " Value: " + userAttr.getValue());
}
System.out.println("Creating cookie with user attribute displayname Value...");
System.out.println("Cookie name is : " + targetResourceURL.substring(38));
Cookie cookie = new Cookie(targetResourceURL.substring(38), userAttrs.get("displayName"));
System.out.println("Setting cookie path to :" + targetResourceURL);
cookie.setPath(targetResourceURL);
System.out.println("Setting cookie age...");
System.out.println("Cookie will be deleted atonce when the browser is closed");
cookie.setMaxAge(-1);
response.addCookie(cookie);
RequestDispatcher dispatch = request.getRequestDispatcher("/area.jsp");
dispatch.forward(request, response);
} catch (Throwable any) {
any.printStackTrace();
}
}
}
}
public double calculateArea() {
Random rnd = new Random();
int radius = rnd.nextInt(15);
double area = Math.pow((double) radius, (double) 2) * Math.PI;
return area;
}
}
Here is my area.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" session="false"%>
<%# page import="com.pegright.seleniumproject.controllers.CircleArea"%>
<%# page import="javax.servlet.http.Cookie;"%>
<!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">
<title>Area</title>
</head>
<body>
<%
CircleArea area = new CircleArea();
Cookie[] cookie = request.getCookies();
for (int i = 0; i < cookie.length; i++) {
out.println("Welcome " + cookie[i].getValue());
}
out.println("Area of a random circle: " + area.calculateArea());
%>
</body>
</html>
I am getting the following error when I am accessing the application in browser:
Oct 07, 2015 5:50:40 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.area_jsp._jspService(area_jsp.java:76)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
at com.pegright.seleniumproject.controllers.CircleArea.service(CircleArea.java:54)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
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)
org.apache.jasper.JasperException: java.lang.NullPointerException
at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
at com.pegright.seleniumproject.controllers.CircleArea.service(CircleArea.java:54)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
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)
Caused by: java.lang.NullPointerException
at org.apache.jsp.area_jsp._jspService(area_jsp.java:76)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
... 33 more
I think I know the root cause. Cookies created in the Servlet via response.addCookie won't be available yet on a request dispatch.
You can use response.sendRedirect instead.
Remove these lines:
RequestDispatcher dispatch = request.getRequestDispatcher("/area.jsp");
dispatch.forward(request, response);
and replace with:
response.sendRedirect("area.jsp");
Related
I am developing a little webapp meant to be run on Tomcat. The webapp works fine on my Windows machine (Windows 7, Java 1.8, Tomcat 7) but fails to run on my raspberry pi 3 (Raspian with the lastest update, Java 1.8, Tomcat 7). Starting Tomcat is fine until one try to load the login page. Then I got the following output :
Error when loading localhost:8080/Timbreuse/connection
java.lang.NullPointerException
at ch.cpnv.timbreuse.servlets.Connection.doGet(Connection.java:39)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
at ch.cpnv.timbreuse.filters.ConnectedFilter.doFilter(ConnectedFilter.java:39)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
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)
Message : null
java.lang.NullPointerException
at ch.cpnv.timbreuse.servlets.Connection.doGet(Connection.java:39)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
at ch.cpnv.timbreuse.filters.ConnectedFilter.doFilter(ConnectedFilter.java:39)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
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)
Connection.java:39
public static final String VIEW_CONNECTION = "/WEB-INF/connection.jsp";
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
this.getServletContext().getRequestDispatcher(VIEW_CONNECTION).forward(request, response);
}
Folder architecture when the .war is deployed on the Pi
./student
./student/info.jsp
./display
./display/welcomeDisplay.jsp
./display/showDisplay.jsp
./WEB-INF
./WEB-INF/header.jsp
./WEB-INF/changePassword.jsp
./WEB-INF/classes
./WEB-INF/classes/ch
./WEB-INF/classes/ch/cpnv
./WEB-INF/classes/ch/cpnv/timbreuse
./WEB-INF/classes/ch/cpnv/timbreuse/automation
./WEB-INF/classes/ch/cpnv/timbreuse/automation/Automation.java
./WEB-INF/classes/ch/cpnv/timbreuse/automation/Automation.class
./WEB-INF/classes/ch/cpnv/timbreuse/automation/EndOfDay$EndOfDayTask.class
./WEB-INF/classes/ch/cpnv/timbreuse/automation/EndOfDay.class
./WEB-INF/classes/ch/cpnv/timbreuse/automation/EndOfDay.java
./WEB-INF/classes/ch/cpnv/timbreuse/forms
./WEB-INF/classes/ch/cpnv/timbreuse/forms/AddTimeStudentsForm.class
./WEB-INF/classes/ch/cpnv/timbreuse/forms/CreateStudentForm.java
./WEB-INF/classes/ch/cpnv/timbreuse/forms/ChangePasssowordForm.java
./WEB-INF/classes/ch/cpnv/timbreuse/forms/CreateStudentForm.class
./WEB-INF/classes/ch/cpnv/timbreuse/forms/StudentResearchForm.class
./WEB-INF/classes/ch/cpnv/timbreuse/forms/AddTimeStudentsForm.java
./WEB-INF/classes/ch/cpnv/timbreuse/forms/CreateAdminForm.class
./WEB-INF/classes/ch/cpnv/timbreuse/forms/DeleteStudentForm.class
./WEB-INF/classes/ch/cpnv/timbreuse/forms/ConnectionForm.java
./WEB-INF/classes/ch/cpnv/timbreuse/forms/DeleteStudentForm.java
./WEB-INF/classes/ch/cpnv/timbreuse/forms/DeleteTeacherForm.class
./WEB-INF/classes/ch/cpnv/timbreuse/forms/ChangePasssowordForm.class
./WEB-INF/classes/ch/cpnv/timbreuse/forms/StudentResearchForm.java
./WEB-INF/classes/ch/cpnv/timbreuse/forms/DeleteAdminForm.java
./WEB-INF/classes/ch/cpnv/timbreuse/forms/CreateTeacherForm.class
./WEB-INF/classes/ch/cpnv/timbreuse/forms/DeleteAdminForm.class
./WEB-INF/classes/ch/cpnv/timbreuse/forms/ConnectionForm.class
./WEB-INF/classes/ch/cpnv/timbreuse/forms/SetHolydaysForm.java
./WEB-INF/classes/ch/cpnv/timbreuse/forms/CreateAdminForm.java
./WEB-INF/classes/ch/cpnv/timbreuse/forms/CreateTeacherForm.java
./WEB-INF/classes/ch/cpnv/timbreuse/forms/SetHolydaysForm.class
./WEB-INF/classes/ch/cpnv/timbreuse/forms/DeleteTeacherForm.java
./WEB-INF/classes/ch/cpnv/timbreuse/beans
./WEB-INF/classes/ch/cpnv/timbreuse/beans/Teacher.class
./WEB-INF/classes/ch/cpnv/timbreuse/beans/Teacher.java
./WEB-INF/classes/ch/cpnv/timbreuse/beans/Student.class
./WEB-INF/classes/ch/cpnv/timbreuse/beans/Student.java
./WEB-INF/classes/ch/cpnv/timbreuse/beans/Log.class
./WEB-INF/classes/ch/cpnv/timbreuse/beans/Log.java
./WEB-INF/classes/ch/cpnv/timbreuse/beans/User.class
./WEB-INF/classes/ch/cpnv/timbreuse/beans/Holyday.java
./WEB-INF/classes/ch/cpnv/timbreuse/beans/Holyday.class
./WEB-INF/classes/ch/cpnv/timbreuse/beans/User.java
./WEB-INF/classes/ch/cpnv/timbreuse/config
./WEB-INF/classes/ch/cpnv/timbreuse/config/DAOFactoryInitialisation.class
./WEB-INF/classes/ch/cpnv/timbreuse/config/DAOFactoryInitialisation.java
./WEB-INF/classes/ch/cpnv/timbreuse/dao
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOUtility.class
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOImplUser.class
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOImplTeacher.java
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOUser.class
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOLog.class
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOStudent.java
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOUtility.java
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOTeacher.java
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOException.java
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOImplStudent.class
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOImplTeacher.class
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOImplHolyday.class
./WEB-INF/classes/ch/cpnv/timbreuse/dao/dao.properties
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOImplUser.java
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOImplHolyday.java
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOFactory.java
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOImplLog.class
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOImplAdmin.class
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOImplStudent.java
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOHolyday.class
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOFactory.class
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOTeacher.class
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOImplAdmin.java
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOException.class
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOUser.java
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOHolyday.java
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOLog.java
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOStudent.class
./WEB-INF/classes/ch/cpnv/timbreuse/dao/DAOImplLog.java
./WEB-INF/classes/ch/cpnv/timbreuse/servlets
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/SetTimetable.class
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/AdminPanel.java
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/Display.java
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/Connection.java
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/Logout.class
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/SetTimetable.java
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/ChangePassword.java
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/Logout.java
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/SetHolydays.class
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/AdminPanel.class
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/StudentInfo.java
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/ChangePassword.class
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/EndDay.class
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/ManageStudents.java
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/ManageStudents.class
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/Display.class
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/Connection.class
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/EndDay.java
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/StudentInfo.class
./WEB-INF/classes/ch/cpnv/timbreuse/servlets/SetHolydays.java
./WEB-INF/classes/ch/cpnv/timbreuse/mathTime
./WEB-INF/classes/ch/cpnv/timbreuse/mathTime/Math.class
./WEB-INF/classes/ch/cpnv/timbreuse/mathTime/Math.java
./WEB-INF/classes/ch/cpnv/timbreuse/mathTime/Date.class
./WEB-INF/classes/ch/cpnv/timbreuse/mathTime/SecondsPastMidnight.class
./WEB-INF/classes/ch/cpnv/timbreuse/mathTime/SecondsPastMidnight.java
./WEB-INF/classes/ch/cpnv/timbreuse/mathTime/Date.java
./WEB-INF/classes/ch/cpnv/timbreuse/filters
./WEB-INF/classes/ch/cpnv/timbreuse/filters/ConnectedFilter.class
./WEB-INF/classes/ch/cpnv/timbreuse/filters/ConnectionFilter.class
./WEB-INF/classes/ch/cpnv/timbreuse/filters/StudentFilter.class
./WEB-INF/classes/ch/cpnv/timbreuse/filters/TeacherFilter.java
./WEB-INF/classes/ch/cpnv/timbreuse/filters/ConnectionFilter.java
./WEB-INF/classes/ch/cpnv/timbreuse/filters/AdminFilter.java
./WEB-INF/classes/ch/cpnv/timbreuse/filters/AdminFilter.class
./WEB-INF/classes/ch/cpnv/timbreuse/filters/ConnectedFilter.java
./WEB-INF/classes/ch/cpnv/timbreuse/filters/TeacherFilter.class
./WEB-INF/classes/ch/cpnv/timbreuse/filters/StudentFilter.java
./WEB-INF/footer.jsp
./WEB-INF/.gitignore
./WEB-INF/lib
./WEB-INF/lib/bonecp-0.8.1-20131105.191813-1.jar
./WEB-INF/lib/jasypt-1.9.0.jar
./WEB-INF/lib/jstl-1.2.jar
./WEB-INF/lib/java-json.jar
./WEB-INF/lib/slf4j-api-1.6.6.jar
./WEB-INF/lib/joda-time-2.1.jar
./WEB-INF/lib/mysql-connector-java-5.1.38-bin.jar
./WEB-INF/lib/guava-13.0.1.jar
./WEB-INF/web.xml
./WEB-INF/connection.jsp
./connecting.jsp
./form.css
./META-INF
./META-INF/MANIFEST.MF
./teacher
./teacher/setTimeTable.jsp
./teacher/manageStudents.jsp
./teacher/setHolydays.jsp
./admin
./admin/adminPanel.jsp
How can I prevent that ?
The only objects that can be null are request and response, or dispatcher. If you can't place a breakpoint there, I would check tomcat's messages/logs before the exception
I have a controller which handles a long polling mechanism by using Springs DeferredResult. At times when I call the controller method multiple times I get the below exception :
java.lang.IllegalStateException: getOutputStream() has already been called for this response
So there is a call views/getLongPollingGraphData which passes some ids and returns with a json response for one or none id. When I load the page multiple times which makes this URL call some times I get the above exception.
The issue that I face is I am using the onCompletion callback on DeferredResult to remove the DeferredResult from a map when it either times out or the response is set on it. But in the case of this exception the onCompletion is not called and it stays on the map. I have read different So posts about this exception but I could not relate to any one of them.
Below is the entire stack trace of the exception and parts of my controller method.
Nov 07, 2016 9:52:06 AM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:648)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:213)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:104)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:186)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:126)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
at org.apache.jsp.WEB_002dINF.jsp.ClientAbortException_jsp._jspService(ClientAbortException_jsp.java:136)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:604)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:543)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:229)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:659)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:625)
at org.apache.catalina.core.AsyncContextImpl$1.run(AsyncContextImpl.java:239)
at org.apache.catalina.core.AsyncContextImpl.doInternalDispatch(AsyncContextImpl.java:382)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:215)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.asyncDispatch(CoyoteAdapter.java:299)
at org.apache.coyote.http11.AbstractHttp11Processor.asyncDispatch(AbstractHttp11Processor.java:1652)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Nov 07, 2016 9:52:06 AM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet main-dispatcher threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:648)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:213)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:104)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:186)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:126)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
at org.apache.jsp.WEB_002dINF.jsp.ClientAbortException_jsp._jspService(ClientAbortException_jsp.java:136)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:604)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:543)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:229)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:659)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:625)
at org.apache.catalina.core.AsyncContextImpl$1.run(AsyncContextImpl.java:239)
at org.apache.catalina.core.AsyncContextImpl.doInternalDispatch(AsyncContextImpl.java:382)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:215)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.asyncDispatch(CoyoteAdapter.java:299)
at org.apache.coyote.http11.AbstractHttp11Processor.asyncDispatch(AbstractHttp11Processor.java:1652)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Nov 07, 2016 9:52:06 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [main-dispatcher] in context with path [/test-web] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:648)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:213)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:104)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:186)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:126)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
at org.apache.jsp.WEB_002dINF.jsp.ClientAbortException_jsp._jspService(ClientAbortException_jsp.java:136)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:604)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:543)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:229)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:659)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:625)
at org.apache.catalina.core.AsyncContextImpl$1.run(AsyncContextImpl.java:239)
at org.apache.catalina.core.AsyncContextImpl.doInternalDispatch(AsyncContextImpl.java:382)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:215)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.asyncDispatch(CoyoteAdapter.java:299)
at org.apache.coyote.http11.AbstractHttp11Processor.asyncDispatch(AbstractHttp11Processor.java:1652)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Controller method:
#RequestMapping(value = "views/getLongPollingGraphData", method = RequestMethod.GET, headers = "Accept=application/json")
#ResponseBody
public DeferredResult<WebServiceResponse> getLongGraphData(
HttpServletRequest request,
#RequestParam(value = "ids") String ids)
throws Exception
{
//perform operations
//set time out this DeferredResult, after 5 seconds
final DeferredResult<WebServiceResponse> deferredResult = new DeferredResult<WebServiceResponse>(5000L, awrDefault);
//listOfViews is a list containing ids
mMapOfDeferredResultAndViews.put(deferredResult, listOfViews);
//more operations
deferredResult.onCompletion(new Runnable() {
#Override
public void run()
{
mMapOfDeferredResultAndViews.remove(deferredResult);
}
});
return deferredResult;
}
Edit:
I think that a org.apache.catalina.connector.ClientAbortException is thrown at some point when the page is continuously refreshed. I use org.springframework.web.servlet.handler.SimpleMappingExceptionResolver to handle it in a jsp page. Below is the code:
main-dispatcher-servlet.xml
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">Exception</prop>
<prop key="org.apache.catalina.connector.ClientAbortException">ClientAbortException</prop>
</props>
</property>
</bean>
ClientAbortException.jsp
<%#page import="org.slf4j.LoggerFactory"%>
<%#page import="org.slf4j.Logger"%>
<%#page import="com.test.model.messages.Messages"%>
<%#page import="java.util.Enumeration"%>
<%#page import="org.codehaus.jackson.map.ObjectMapper"%>
<%#page import="com.test.model.exceptions.TestException"%>
<%#page import="com.test.web.exchange.WebServiceResponse"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
Logger LOGGER = LoggerFactory.getLogger(this.getClass());
LOGGER.debug("Exception caught in JSP: ", (Exception) request.getAttribute("exception"));
boolean bReturnJson = false;
Enumeration<?> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = (String) headerNames.nextElement();
if (headerName.equals("accept")) {
if (request.getHeader(headerName).equals(
"application/json; charset=utf-8")) {
bReturnJson = true;
}
break;
}
}
if (bReturnJson) {
WebServiceResponse awsr = new WebServiceResponse();
TestException exception = new TestException(
Messages.ERROR.toString(),
(Exception) request.getAttribute("exception"));
awsr.setException(exception);
ObjectMapper mapper = new ObjectMapper();
out.write(mapper.writeValueAsString(awsr));
} else {
((Exception) request.getAttribute("exception")).printStackTrace();
%>
HTML error!
<%
}
%>
The problem is obvious:
http://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html#getInputStream--
Throws:
IllegalStateException - if the getReader() method has already been called for this request
To find out, which Filter (or something similar) calls the getReader() method before your JSP servlet does, set a break point to IllegalStateException. Call your JSP. In your debugger's call stack, find out the specific implementation class of the interface ServletRequest (this is container specific). Set a break point to getReader(), then call your JSP again and you got the root cause.
If you really have to call getReader() or getInputStream() multiple times, a HttpServletRequestWrapper might help you, see e.g.:
How to read InputStream multiple times
https://gist.github.com/calo81/2071634
Without your whole project code no-one will be able to find out the cause for this Exception.
I at the time of running spark job in my project:
public static List<String> getAllSavedModelFoldersJob(String saveModelsFolderFilePath) {
JavaSparkContext ctx = null;
List<String> list = new ArrayList<String>();
try {
ctx = new JavaSparkContext(
new SparkConf().setAppName("Save Models into File").setMaster("local"));
list = getAllSavedModelFolders(ctx, saveModelsFolderFilePath).collect();
} catch (Exception e) {
e.printStackTrace();
return list;
} finally { // ctx.stop(); } return list; }
}
But getting below error,
java.lang.ClassNotFoundException: org.apache.spark.launcher.LauncherProtocol$Message
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at org.apache.spark.scheduler.local.LocalBackend.(LocalBackend.scala:107)
at org.apache.spark.SparkContext$.org$apache$spark$SparkContext$$createTaskScheduler(SparkContext.scala:2603)
at org.apache.spark.SparkContext.(SparkContext.scala:522)
at org.apache.spark.api.java.JavaSparkContext.(JavaSparkContext.scala:59)
at com.nuevora.core.spark.SaveModelsJob.getAllSavedModelFoldersJob(SaveModelsJob.java:257)
at com.nuevora.controllers.FormsValidatorServlet.service(FormsValidatorServlet.java:546)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
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)
I am using spark version is spark-1.6,hadoop2.6 and java1.8.
I wrote this Code to check and create a cookie.It loops through the array returned by the request object.But it throws exception.It's thrown at the beginning of the for loop.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CookieLogin extends HttpServlet {
PrintWriter out;
boolean flag;
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException{
Cookie[] cookies = req.getCookies();
out = res.getWriter();
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equals("session_started")){
flag=true;
out.println("Your session started on "+cookies[i].getValue());
}
}
if(!flag){
String time = new java.util.Date().toString();
res.addCookie(new Cookie("session_started",time));
out.println("just started");
}
}
}
Here's the Stack Trace from eclipse.
java.lang.NullPointerException
at CookieLogin.doPost(CookieLogin.java:11)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
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)
req.getCookies() returned null,so cookies.length throws NullPointerException,
you should check wheather the req.getCookies(); is null;
The NullPointerException is because getCookies() method will return null if there are no cookies. And so it throws a NullPointerException.
Solution:Check for null
for(int i=0;cookies!=null&&i<cookies.length;i++)
I have created a webapp using servlets. I am establishing a connection to a MySQL database using a DataSource object and get the following stacktrace when I load the webpage:
java.sql.SQLException: No database selected
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1084)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2832)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2781)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1569)
at uk.co.morleys.TestimonialService.countTestimonials(TestimonialService.java:106)
at uk.co.morleys.TestimonialService.getPaginationDetails(TestimonialService.java:122)
at uk.co.morleys.HomeController.doGet(HomeController.java:77)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
This is the TestimonialService.java file:
public class TestimonialService {
ResultSet rs;
DataSource ds;
public TestimonialService(){
rs = null;
ds = DataSourceFactory.getMySQLDataSource();
}
public int countTestimonials() throws SQLException{
int count = 0;
try(Connection con = ds.getConnection()){
Statement stmt = con.createStatement();
rs = stmt.executeQuery("SELECT COUNT(*) FROM morleys_testimonial WHERE isActive=1");
while(rs.next()){
count = rs.getInt(1);
}
}catch(SQLException e){
e.printStackTrace();
}
return count;
}
}
This is the DataSourceFactory.java where I establish a connection to the database:
public class DataSourceFactory {
public static DataSource getMySQLDataSource() {
MysqlDataSource mysqlDS = null;
try {
mysqlDS = new MysqlDataSource();
mysqlDS.setURL("jdbc:mysql://hostname/");
mysqlDS.setDatabaseName("dbname");
mysqlDS.setUser("user");
mysqlDS.setPassword("password");
} catch (Exception e) {
throw new IllegalStateException(e);
}
return mysqlDS;
}
}
Question Am I not selecting the database with setDatabaseName()? If not how do I do this?
Why dont you simply try this
mysqlDS.setURL("jdbc:mysql://hostname/dbname");
instead of
mysqlDS.setDatabaseName("dbname");
Are you providing port number along with the hostname? If not try giving portnumber as well.
Additionally make sure you have Mysql JDBC Driver
You can follow any of these tutorial for creating MysqlDataSource:
http://www.journaldev.com/2509/jdbc-datasource-example-oracle-mysql-and-apache-dbcp-tutorial
or
http://www.herongyang.com/JDBC/MySQL-JDBC-Driver-DataSource.html