gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:57:08 24 4
gpt4 key购买 nike

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(未知来源)

注意 Apache Tomcat/7.0.47 日志中提供了根本原因的完整堆栈跟踪。

我尝试通过使用不同的 serialVersionID 来消除这个错误,并在运行它之前清理了项目。仍然坚持这一点。请帮忙。提前致谢。

最佳答案

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

    class MyServlet extends HttpServlet{  

应该是

    public class MyServlet extends HttpServlet{  

关于java - HTTP 状态 500 - 实例化 servlet 类 com.sar.pkg.MyServlet 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55051687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com