如何设置Servlet路径使HTML正确地调用servlet文件? [英] How to set up the Servlet Path make HTML correctly invoke servlet file?

查看:107
本文介绍了如何设置Servlet路径使HTML正确地调用servlet文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

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);
    }


}

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

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

<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>

web.xml定义为:

And the web.xml is defined as :

<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>

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

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:

似乎工作正常。

我的问题

My Question:

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

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

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

我预计一旦输入.htm被提交,它将调用serlvet和网页地址应该是:

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

http://localhost:8080/ServeletTest/myservletURL

但显示页面地址仍然不变:

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

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

http://localhost:8080/ServeletTest/myservlet with HTTP Status 404 error.

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

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.

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

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

推荐答案

如果您将 url-pattern 更改为myservletURL,您还需要更新 strong>来定位这个新网址。

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

这篇关于如何设置Servlet路径使HTML正确地调用servlet文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆