title: error: No operations allowed after connection closed
body: <p>I know that it is a bad idea to put SQL code or any database related code in JSP. But I want to try this way please help.
  In the below code in retrieve.jsp I have to enter empid and datefrom and dateto then it will show the result on dispaly.jsp page as table with all the details and total working hours and overtime of the employee. </p>
  
  <p>retrieve.jsp</p>
  
  <pre><code>%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;%@page import="com.eis.bean.Provider"%&gt;
  &lt;%@page import="com.eis.bean.ConnectionProvider"%&gt;
  &lt;%@page import="java.sql.*" %&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;title&gt;JSP Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h4&gt;Enter Employee ID and the dates&lt;/h4&gt;
      &lt;form name="retrieve form" action="display1.jsp" method="POST"&gt;
          &lt;table border="0"&gt;
  
              &lt;tbody&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Employee ID&lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="Emp_id" required="required"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;From Date:&lt;/td&gt;
                      &lt;td&gt;&lt;input type="date" name="From" value="yyyy/MM/dd" required="required"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;To Date:&lt;/td&gt;
                      &lt;td&gt;&lt;input type="date" name="To" value="yyyy/MM/dd" required="required"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/tbody&gt;
          &lt;/table&gt;
          &lt;input type="reset" value="Clear" name="clear" /&gt;
          &lt;input type="submit" value="Submit" name="submit" /&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>display.jsp</p>
  
  <pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;%@page import="com.eis.bean.Provider"%&gt;
  &lt;%@page import="com.eis.bean.ConnectionProvider"%&gt;
  &lt;%@page import="java.sql.*" %&gt;
  &lt;!DOCTYPE html&gt;
  
  &lt;%
  String empid = request.getParameter("Emp_id");
  String from = request.getParameter("From");
  String to = request.getParameter("To");
  Connection conn= null;
  PreparedStatement ps = null; 
  ResultSet rs = null; 
  
  conn = ConnectionProvider.getConn(); 
  try {
  
      ps = conn.prepareStatement("SELECT * FROM timsheetdb.logintable WHERE Emp_id=? and LoginDate BETWEEN ? AND ?; ");
      ps.setString(1, empid); 
      ps.setString(2, from);
      ps.setString(3, to);
  
      out.print("&lt;table border=1&gt;");  
      out.print("&lt;caption&gt;TimeSheet &lt;/caption&gt;");  
      rs = ps.executeQuery();  
      ResultSetMetaData rsmd = rs.getMetaData(); 
      int total=rsmd.getColumnCount();  
       if (rs.next()) {
   %&gt;&lt;jsp:include page="retrieve.jsp"/&gt;
  &lt;%
  
      out.print("&lt;center&gt;&lt;h1&gt;Timsheet&lt;/h1&gt;&lt;/center&gt;");
      out.print("&lt;b&gt;Employee ID&lt;/b&gt; " + empid + "");
      out.print("&lt;br&gt;" + "&lt;b&gt;From Date:&lt;/b&gt; " + from);
      out.print("&lt;br&gt;" + "&lt;b&gt;To Date:&lt;/b&gt; " + to);
      out.print("&lt;tr&gt;");  
          for(int i=1;i&lt;=total;i++)  
          {  
          out.print("&lt;th&gt;"+rsmd.getColumnName(i)+"&lt;/th&gt;");  
          }  
          out.print("&lt;/tr&gt;");
   while(rs.next())  
          {  
          out.print("&lt;tr&gt;&lt;td&gt;"+rs.getString(1)+"&lt;/td&gt;&lt;td&gt;"+rs.getString(2)+"&lt;/td&gt;&lt;td&gt;"+rs.getString(3)+"&lt;/td&gt;&lt;td&gt;"+rs.getString(4)+"&lt;/td&gt;&lt;td&gt;"+rs.getString(5)+"&lt;/td&gt;&lt;td&gt;"+rs.getString(6)+"&lt;/td&gt;&lt;td&gt;"+rs.getString(7)+"&lt;/td&gt;&lt;td&gt;"+rs.getString(8)+"&lt;/td&gt;&lt;td&gt;"+rs.getString(9)+"&lt;/td&gt;&lt;td&gt;"+rs.getString(10)+"&lt;/td&gt;&lt;/tr&gt;");  
  
          }  
          out.print("&lt;/table&gt;");  
  } 
  else
       { 
  out.println("Please check the Employee ID and Dates "); 
  %&gt;
  &lt;jsp:include page="retrieve.jsp"/&gt;
  &lt;%}
  } catch (Exception ex) {
      System.out.println(ex);
  }   
  finally {  
           if (conn != null) {  
              try {  
                  conn.close();  
              } catch (SQLException e) {  
                  e.printStackTrace();  
              }  
          } 
  
          if (ps != null) {  
              try {  
                  ps.close();  
              } catch (SQLException e) {  
                  e.printStackTrace();  
              }  
          }  
          if (rs != null) {  
              try {  
                  rs.close();  
              } catch (SQLException e) {  
                  e.printStackTrace();  
              }  
          }  
  
      }  
  %&gt;
  </code></pre>
  
  <p>ConnectionProvider.class</p>
  
  <pre><code>import java.sql.*; 
  
  public class ConnectionProvider {  
    private static Connection conn=null;  
    static{  
      try{  
        Class.forName("com.mysql.jdbc.Driver");
        conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/timsheetdb","root","lion");  
      }catch(Exception e){}  
    }  
  
    public static Connection getConn(){  
      return conn;  
    }  
  
  }
  </code></pre>
  
  <p>Below is the error:</p>
  
  <pre><code>com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
  at com.mysql.jdbc.Util.handleNewInstance(Util.java:400)
  at com.mysql.jdbc.Util.getInstance(Util.java:383)
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:958)
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
  at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1236)
  at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1231)
  at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4140)
  at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4109)
  at org.apache.jsp.display_jsp._jspService(display_jsp.java:113)
  at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
  at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
  at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
  at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
  at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
  at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2503)
  at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2492)
  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)
  </code></pre>