404错误servlet jsp [重复]

loading...


2

这个问题在这里已有答案:

我目前正在使用eclipse开发一个简单的Web App项目 .

我使用centos server和apache-tomcat-7.0.47 .

我有一个 index.jsp 文件:

<form action="MyServlet">
   <input type="submit" value="Send" />
</form>

我的servlet文件 MyServlet.java

package com.srk.pkg;

import java.io.IOException;
import java.io.PrintWriter; 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
  * Servlet implementation class MyServlet
*/
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

    /**
      * @see HttpServlet#HttpServlet()
    */
    public MyServlet() {
       super();
    }

   /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.print("Hello Everybody..");
   }

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

和我的 web.xml file:

<?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>Example1</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.srk.pkg.MyServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
  </servlet-mapping>
</web-app>

我遇到的问题是:

我从eclipse运行web应用程序,一切正常 .

http://localhost:8080/Example1/index.jsp

http://localhost:8080/Example1/MyServlet?

但是当我尝试从我的centos服务器访问它时,我得到一个

HTTP Status 404 - /Example1/WebContent/MyServlet

类型状态报告

message /Example1/WebContent/MyServlet

description请求的资源不可用 .

谁能帮我??

我使用的链接:

.....:8080/Example1/WebContent/index.jsp

....:8080/Example1/WebContent/MyServlet?

Package explorer

1回答

  • 0

    你为什么试图访问

    /Example1/WebContent/MyServlet
    

    项目中 WebContent 目录中的所有内容最终都将位于生成的 war 文件的根目录下 . 所有不在 WEB-INF 的东西都可以访问,所以你可以这样做

    /Example1/index.jsp
    

    否则,你必须通过 Servlet . 您没有匹配的网址映射

    /Example1/WebContent/MyServlet
    

    因此,这给你一个404 .

    你有一个网址映射

    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/MyServlet</url-pattern>
    </servlet-mapping>
    

    你可以访问

    /Example1/MyServlet
    

    请注意,在浏览器中提交此表单

    <form action="MyServlet">
       <input type="submit" value="Send" />
    </form>
    

    取决于您当前的网址 . 如果您之前提出过请求

    /Example1/index.jsp
    

    然后提交表格将发送请求

    /Example1/MyServlet
    

    如果你在

    /Example1/some/random/path
    

    然后提交将发送请求

    /Example1/some/random/MyServlet
    

    如果您想让您的请求始终转到相同的URL,您应该使您的路径绝对

    <form action="${pageContext.request.contextPath}/MyServlet">
       <input type="submit" value="Send" />
    </form>
    

loading...

评论

暂时没有评论!