html Servlet web.xml(转)

在浏览器输入:http://127.0.0.1:8080/test/test.html
点击提交按钮,Tomcat后台输出:
control: aaa's value is : bbb
页面显示结果:
page: aaa's value is : bbb

文件目录结构:
test
│  test.html
│  
└—WEB-INF
    │  web.xml
    └—classes
            SubmitServle.java
            SubmitServle.class

源代码(test.html):
<form action="submit.do">
<input type="textField" name="aaa" value="bbb" />
<input type="submit" />
</form>

源代码(web.xml):
<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
    version="2.4">
    <servlet>
      <servlet-name>submitServle</servlet-name>
      <servlet-class>SubmitServle</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>submitServle</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

源代码(SubmitServle.java):

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SubmitServle extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String aaa = request.getParameter("aaa");
        System.out.println("control: aaa's value is : " + aaa);
        response.getOutputStream().println("page: aaa's value is : " + aaa);;
    }
}

另:test.html中的提交按钮被点击时,会将表单中的字段aaa以及它的值提交到submit.do去(提交的这一过程也可以用javaScript来写)。服务器接到这个请求,将解析web.xml文件中的内容,将转交给符合这一请求(*.do)的servlet--SubmitServlet处理。处理调用service方法,可以从request中取得参数aaa的值,然后打印出来。

url pattern 应该和 form faction一致么??

原文地址:https://www.cnblogs.com/yunxiblog/p/5189801.html