无法访问JSP中的servlet页面

pinkon5k  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(238)

我创建了一个名为ORMS1的项目,在其中我有以下目录stuture.

这是www.example.com文件的代码myfile.java(内部动作包)

package action;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Servlet implementation class myfile
 */
@WebServlet(name="up", urlPatterns= {"/myfile"} )
public class myfile extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public myfile() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

这是我的web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="5.0">
  <display-name>ORMS_1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>default.htm</welcome-file>
  </welcome-file-list>
   <servlet>
    <description></description>
    <display-name>fileupload</display-name>
    <servlet-name>fileupload</servlet-name>
    <servlet-class>fileupload</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>fileupload</servlet-name>
    <url-pattern>/fileupload</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>myfile</display-name>
    <servlet-name>myfile</servlet-name>
    <servlet-class>action.myfile</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>myfile</servlet-name>
    <url-pattern>/myfile</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>myfileupload</display-name>
    <servlet-name>myfileupload</servlet-name>
    <servlet-class>sample1.myfileupload</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>myfileupload</servlet-name>
    <url-pattern>/myfileupload</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>upload</display-name>
    <servlet-name>upload</servlet-name>
    <servlet-class>pkg.upload</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>upload</servlet-name>
    <url-pattern>/try</url-pattern>
  </servlet-mapping>
  
</web-app>

问题是,每当我访问任何jsppage,那么它将通过错误为什么会发生??

当我重新加载这个,然后它会显示以下错误页面

当我创建任何新的servlet或访问默认的Servlet,然后我不能访问servlet,我不知道是什么原因。

3ks5zfa0

3ks5zfa01#

fileapload不存在的问题。
您应该从web.xml中删除这些行

<servlet-name>fileupload</servlet-name>
    <servlet-class>fileupload</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>fileupload</servlet-name>
    <url-pattern>/fileupload</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>myfile</display-name>
    <servlet-name>myfile</servlet-name>
    <servlet-class>action.myfile</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>myfile</servlet-name>
    <url-pattern>/myfile</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>myfileupload</display-name>
    <servlet-name>myfileupload</servlet-name>
    <servlet-class>sample1.myfileupload</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>myfileupload</servlet-name>
    <url-pattern>/myfileupload</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>upload</display-name>
    <servlet-name>upload</servlet-name>
    <servlet-class>pkg.upload</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>upload</servlet-name>
    <url-pattern>/try</url-pattern>
  </servlet-mapping>

然后向现有servlet添加Map

/**
 * Servlet implementation class myfile
 */
@WebServlet("/fileupload")
public class myfile extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public myfile() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

相关问题