zoukankan      html  css  js  c++  java
  • 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一致么??

  • 相关阅读:
    Linux Vim编辑器
    Linux sed 流编辑器
    Shell 编程 (变量和条件测试)
    Linux 下 Bash配置文件读取
    Linux 用户、权限
    Linux 指令(一)文件/目录操作
    Ubuntu 下安装 Swoole
    Mysql IN语句查询
    Mysql 查询优化
    Mysql 获取表属性
  • 原文地址:https://www.cnblogs.com/yunxiblog/p/5189801.html
Copyright © 2011-2022 走看看