实例化servlet类[org.ran.FristServlet]

问题描述 投票:0回答:1

[您好,我使用apache Tomcat编写代码,但是我一直都在上面。我已经检查了Web.xml路径

enter image description here

以及我没有发现任何错误的web.xml的内容

enter image description here

下面是我的代码和错误消息

index.jsp

enter image description here

FirstServlet

public class FirstServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RequestDispatcher rd = req.getRequestDispatcher("index.jsp");
        rd.forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        int ph = Integer.parseInt(req.getParameter("ph"));

        HttpSession session = req.getSession();

        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        if (uid != null && ph < 0) {

            try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306?user=root&password=Eagle&useSSL=false");

                String sql = "select invno,phone from invnt.allinvoice where invno=? and phone=? ";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, uid);
                pstmt.setInt(2, ph);
                rs = pstmt.executeQuery();

                String nid="";
                int nph=0;

                while (rs.next()) {
                    nid=rs.getString(1);
                    nph=rs.getInt(2);
                }
                if(uid==nid && ph==nph) {
                    RequestDispatcher rd = req.getRequestDispatcher("index.jsp");
                    rd.forward(req, resp);
                }else{
                    RequestDispatcher rd = req.getRequestDispatcher("index.jsp");
                    rd.forward(req, resp);
                }

            } catch (ClassNotFoundException | SQLException e) {

                e.printStackTrace();
            }

        } else {
            RequestDispatcher rd = req.getRequestDispatcher("index.jsp");
            rd.forward(req, resp);
        }
    }

错误日志

javax.servlet.ServletException: Error instantiating servlet class [org.ran.FristServlet]
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)

Root Cause

java.lang.ClassNotFoundException: org.ran.FristServlet
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1363)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1186)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61
java jsp tomcat servlets
1个回答
0
投票

首先从您的web.xml中删除所有servlet及其servletmapping标签,因为您可以简单地使用注释,而不用编写该部署描述符代码。此外,您的项目结构也会受到干扰将您的servlet放在src / main / java中,然后尝试运行它。

也如下注释您的班级。

@WebServlet(urlPatterns = "/fs", loadOnStartup = 1)
public class FirstServlet extends HttpServlet {
© www.soinside.com 2019 - 2024. All rights reserved.