java.lang.ClassNotFoundException: at jdbc.DatabaseAccess.doGet(DatabaseAccess.java:25) - java

I am trying simple login authentication using servlets and jdbc connection to MySQL database. After executing it on tomcat server I am getting following error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at jdbc.DatabaseAccess.doGet(DatabaseAccess.java:25)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
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:950)
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:620)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
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)
May 04, 2015 6:06:29 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/servelt] has started
May 04, 2015 6:06:29 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/servelt] is completed
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at jdbc.DatabaseAccess.doGet(DatabaseAccess.java:25)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
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:950)
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:620)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
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)
I am sharing the content of all files:
DatabaseAccess.java
package jdbc;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.sql.*;
#WebServlet("/DatabaseAccess")
public class DatabaseAccess extends HttpServlet
{
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String title = "Database connect";
String docType ="<!doctype html>\n";
out.println(docType +"<html>\n" +"<head><title>" + title + "</title></head>\n" +"<body>\n" +
"<h1 align=\"center\">" + title + "</h1>\n");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next()){
int id = rs.getInt("id");
int marks = rs.getInt("marks");
String name = rs.getString("name");
out.println("ID: " + id + "<br>");
out.println(", name: " + name + "<br>");
out.println(", marks: " + marks + "<br>");
}
out.println("</body></html>");
rs.close();
stmt.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
web.Xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>servelt</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<servlet>
<servlet-name>DatabaseAccess</servlet-name>
<servlet-class>jdbc.DatabaseAccess</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DatabaseAccess</servlet-name>
<url-pattern>/DatabaseAccess</url-pattern>
</servlet-mapping>
</web-app>
Please help me to get out of this issue.

Add your mysql connector jar to WEB-INF\lib folder to be loaded by tomcat

Exception is for java.lang.ClassNotFoundException: add the respective jar in your lib folder or classpath

Your source code is correct.The exception shows that,mysql connector is not in your libraries.You must add mysql jdbc connector to your application classpath and WEB-INF/lib.If you add only classpath and run any method individually,exception will not occur but from servlet or jsp if you call this method you will get error.For solution you must add mysql jdbc connector jar also to WEB-INF/lib

Related

Null pointer exception. How my connection object is pointing to null

The stacktrace
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1952)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1795)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at Store.<init>(Store.java:12)
at RegisterServlet.doGet(RegisterServlet.java:38)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
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:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:165)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1201)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:654)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:319)
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)
Sep 01, 2020 9:06:08 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [RegisterServlet] in context with path [/Registration] threw exception
java.lang.NullPointerException
at Store.insertuser(Store.java:31)
at RegisterServlet.doGet(RegisterServlet.java:40)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
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:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:165)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1201)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:654)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:319)
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)
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Store {
Connection connection;
Store() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String username = "sa";
String password = "123456abcd";
String url = "jdbc:sqlserver://localhost;databaseName=Register";
connection = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean insertuser(String string1, String string2, String string3, String string4, String string5) throws SQLException{
String sql = "insert into dbo.Usertable values(?,?,?,?,?)";
PreparedStatement pstmt = connection.prepareStatement(sql);//this is the place where connection is null
pstmt.setString(1, string1);
pstmt.setString(2, string2);
pstmt.setString(3, string3);
pstmt.setString(4, string4);
pstmt.setString(5, string5);
int i = pstmt.executeUpdate();
if (i > 0) {
return true;
}
return false;
}
}
i have a class store. I connect jdbc in init. I insert values in usertable in a method insertuser. My connection object is pointing to null. My usertable has five varchar values in a database called Register. Why my connection object is pointing to null.I have jdbc jar connected to my classpath.
If the Store constructor encounters any exception, it will print a stacktrace and return a partially constructed Store object with a null in the connection field.
That would explain what you are seeing. But we can't tell you the cause of the exception you are catching without seeing the (complete) stacktrace. Typical explanations for this kind of thing include:
Database driver JAR not the classpath (correctly)
Incorrect database driver JAR
Incorrect database credentials
Incorrect database host or port information
Network problems
Things you could fix:
As commenters pointed out, the exception handling in your constructor is broken. If you get an exception while setting up the connection, the constructor should terminate with an exception. The caller needs to deal with it, or allow it to propagate.
If you use DriverManager.getConnection you shouldn't need to use Class.forName to load the JDBC driver. The DriverManager infrastructure will find the driver for itself, based on what the jdbc URL says.
You need to add the jar file containing the class com.microsoft.sqlserver.jdbc.SQLServerDriver to your war file's WEB-INF/lib folder.

HTTP Status 500 - Error instantiating servlet class com.sar.pkg.MyServlet

MyServlet.java
class MyServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
//private static final long serialVersionUID =102831973239L;
static String task;
public MyServlet(){
}
String tm;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
task = request.getParameter("task");
tm= request.getParameter("reminder_time");
try {
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date time=new Date();
formatter.format(time);
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =DriverManager.getConnection(
"jdbc:mysql://localhost:3306/reminder","sarthak","sar31thak");
String INSERT_RECORD = "insert into ToDo values(?, ?)";
PreparedStatement pstmt = null;
pstmt = con.prepareStatement(INSERT_RECORD);
pstmt.setString(1, task);
pstmt.setDate(2, (java.sql.Date) time);
int rs= pstmt.executeUpdate();
if(rs!=0){
response.sendRedirect("success.html");
return;
}
else{
response.sendRedirect("error.html");
}
con.close();
}
catch(Exception e){
System.out.println("Got an EXCEPTION");}
}
catch (Exception e) {
System.out.println("Got an ERROR");
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Alert</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>MyServlet</display-name>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.sar.pkg.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
</web-app>
Which causes the following error
HTTP Status 500 - Error instantiating servlet class com.sar.pkg.MyServlet
type Exception report
message Error instantiating servlet class com.sar.pkg.MyServlet
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Error instantiating servlet class com.sar.pkg.MyServlet
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
root cause
java.lang.IllegalAccessException: Class org.apache.catalina.core.DefaultInstanceManager can not access a member of class com.sar.pkg.MyServlet with modifiers "public"
sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
java.lang.Class.newInstance(Unknown Source)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.47 logs.
I have tried to remove this error by using different serialVersionID and cleaned the project before running it. Still stuck on this. PLease help. Thanks in advance.
Your Servlet class com.sar.pkg.MyServlet is not public. Your definition
class MyServlet extends HttpServlet{
should be
public class MyServlet extends HttpServlet{

Null Pointer Exception when deploying a webapp on Tomcat7

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

Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException

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.

Unable to set cookie using Java

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");

Categories

Resources