HTTP状态500-实例化servlet类com.sar.pkg.MyServlet时出错

时间:2019-03-07 19:44:00

标签: java apache tomcat web-applications

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>

哪个会导致以下错误

HTTP状态500-实例化servlet类com.sar.pkg.MyServlet时出错

类型异常报告

消息错误,实例化servlet类com.sar.pkg.MyServlet

说明服务器遇到内部错误,导致服务器无法满足此请求。

例外 javax.servlet.ServletException:实例化servlet类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(未知来源)     java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知源)     java.lang.Thread.run(未知来源)

根本原因 java.lang.IllegalAccessException:类org.apache.catalina.core.DefaultInstanceManager无法使用修饰符“ public”访问com.sar.pkg.MyServlet类的成员     sun.reflect.Reflection.ensureMemberAccess(未知来源)     java.lang.Class.newInstance(未知来源)     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(未知来源)     java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知源)     java.lang.Thread.run(未知来源)

note可以在Apache Tomcat / 7.0.47日志中找到根本原因的完整堆栈跟踪。

我尝试通过使用不同的serialVersionID消除此错误,并在运行该项目之前将其清除。仍然停留在此。请帮忙。预先感谢。

1 个答案:

答案 0 :(得分:1)

您的Servlet类com.sar.pkg.MyServlet是不公开的。您的定义

    class MyServlet extends HttpServlet{  

应该是

    public class MyServlet extends HttpServlet{