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

时间:2012-10-28 14:58:14

标签: html eclipse tomcat servlets

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

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

enter image description here

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


}

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>

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>

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

enter image description here

enter image description here

似乎工作正常。

我的问题

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

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

我所期待的是,一旦提交了input.htm,它将调用serlvet并且网页地址应为:

http://localhost:8080/ServeletTest/myservletURL

但是显示页面地址仍然是,不会改变:

带有HTTP状态404错误的

http://localhost:8080/ServeletTest/myservlet

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

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

2 个答案:

答案 0 :(得分:2)

如果您将网址格式更改为myservletURL,则还需要更新表单操作以定位此新网址。

答案 1 :(得分:1)

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

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

更改此urlpattern {“/ MyServlet”}