如何设置 Servlet Path 使 HTML 正确调用 servlet 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13109898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:36:59  来源:igfitidea点击:

How to set up the Servlet Path make HTML correctly invoke servlet file?

htmleclipsetomcatservlets

提问by Michael Lai

My goal: to access a .htm file, and pass the user input to the invoked servlet and display the content.

我的目标:访问 .htm 文件,并将用户输入传递给调用的 servlet 并显示内容。

What i did: I used eclipse Juno to create a dynamic project:ServeletTest. The structure of the project is as followed:

我做了什么:我使用 Eclipse Juno 创建了一个动态项目:ServeletTest。该项目的结构如下:

enter image description here

在此处输入图片说明

The servlet file is MyServlet.java and the related code is:

servlet 文件为 MyServlet.java,相关代码为:

package ylai.Servlet.test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
/**
 * Servlet implementation class MyServlet
 */
@WebServlet(description = "test servlet", urlPatterns = { "/MyServlet" })
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyServlet() {
        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
        String info = request.getParameter("info") ;    
        PrintWriter out = response.getWriter() ;
        out.println("<html>") ;
        out.println("<head><title>Hello Servlet</title></head>") ;
        out.println("<body>") ;
        out.println("<h1>" + info + "</h1>") ;
        out.println("</body>") ;
        out.println("</html>") ;
        out.close() ;

    }

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


}

The html file is input.htm . And the detail code is:

html 文件是 input.htm 。详细代码是:

<html>
<head><title>This is html file</title></head>
<body>
<form action="myservlet" method="post">
    Type something:<input type="text" name="info">
    <input type="submit" value="submit">
</form>
</body>
</html>

And the web.xml is defined as :

web.xml 定义为:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>    
    <servlet>
        <servlet-name>myservlet</servlet-name>
        <servlet-class>ylai.Servlet.test.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myservlet</servlet-name>
        <url-pattern>/myservlet</url-pattern>
    </servlet-mapping>  
    </web-app>

When i run the input.htm using Built-in Tomcat within the Eclipse, it works fine, and the input content in the input.htm can be displayed by MyServlet.java. The screen shot is as followed:

当我在Eclipse中使用内置Tomcat运行input.htm时,它工作正常,并且可以通过MyServlet.java显示input.htm中的输入内容。屏幕截图如下:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

It seems works fine.

它似乎工作正常。

My Question:

我的问题

If i want to modify the value of in the web.xml as

如果我想将 web.xml 中的值修改为

 <servlet-mapping>
        <servlet-name>myservlet</servlet-name>
        <url-pattern>/myservletURL</url-pattern>
    </servlet-mapping>  

What i expected was once the input.htm is submitted, it will invoked the serlvet and the web page address should be:

我期望的是一旦input.htm被提交,它就会调用serlvet,网页地址应该是:

http://localhost:8080/ServeletTest/myservletURL

But the display page address is still, does not change :

但是显示页面地址依旧,没有变化:

http://localhost:8080/ServeletTest/myservletwith HTTP Status 404 error.

http://localhost:8080/ServeletTest/myservlet带有 HTTP 状态 404 错误。

It looks weird!!! The mechanism should be: When i submit the input.htm page, it will invoke servlet by servlet-name in the web.xml. In this case, servlet-name is myservlet. Tomcat will use servlet-name to find the actual location of servlet file: MyServlet.java and execute it. The redirect page address would be depends on what you define in . In this case, it should /ServeletTest/myservletURL But right now. Servlet file can not be invoked and the page address is not what i expect.

看起来很奇怪!!!机制应该是:当我提交 input.htm 页面时,它会通过 web.xml 中的 servlet-name 调用 servlet。在这种情况下,servlet-name 是 myservlet。Tomcat 将使用 servlet-name 查找 servlet 文件的实际位置:MyServlet.java 并执行它。重定向页面地址将取决于您在 . 在这种情况下,它应该是 /ServeletTest/myservletURL 但是现在。无法调用 Servlet 文件,页面地址不是我所期望的。

Do i have wrong understand on the servlet invoke mechanism or others?

我对 servlet 调用机制或其他机制有错误理解吗?

回答by user553180

If you changed the url-patternto myservletURL, you will also need to update the form actionto target this new url.

如果您将url-pattern更改为 myservletURL,您还需要更新表单操作以定位这个新的 url。

回答by sunleo

LifeCycleServlet--@WebServlet("/LifeCycleServlet")
MyServlet--@WebServlet(description = "test servlet", urlPatterns = { "/MyServlet" })

delete these lines because here you have mentioned url as MyServlet

删除这些行,因为在这里你提到了 url 作为 MyServlet

or

或者

Change this urlpattern { "/MyServlet" } also

还要更改此 urlpattern { "/MyServlet" }