通过Servlet获取表单中的数据及其应用

通过Servlet获取表单中的数据及其应用

通过Servlet获取表单中的数据及其应用_第1张图片
样例跳转:样例八<–点这跳转

paramsForm.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/5/1
  Time: 0:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
  <head>
    <title>数据页面</title>
  </head>
  <body>
  <form method="post" action="ThreeParams">
      <p>数据1<label>
          <input type="text" name="gr1">
      </label></p>
      <br>
      <p>数据2<label>
          <input type="text" name="gr2">
      </label></p>
      <br>
      <p>数据3<label>
          <input type="text" name="gr3">
      </label></p>
      <br>
      <p>
          <input type="submit" value="提交">
          <input type="reset" value="清除">
      </p>
  </form>
  </body>
</html>

ThreeParams.java

package lib;

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

public class ThreeParams extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException{
        response.setContentType("text/html:charset=UTF-8");
        PrintWriter out=response.getWriter();
        out.println(request.getParameter("gr1"));
        out.println(request.getParameter("gr2"));
        out.println(request.getParameter("gr3"));
        out.close();
    }
    protected void doGet(HttpServletRequest request,HttpServletResponse response)
        throws IOException{
        processRequest(request,response);
    }
    protected void doPost(HttpServletRequest request,HttpServletResponse response)
            throws IOException{
        processRequest(request,response);
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>ThreeParams</servlet-name>
        <servlet-class>lib.ThreeParams</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ThreeParams</servlet-name>
        <url-pattern>/ThreeParams</url-pattern>
    </servlet-mapping>
</web-app>

通过Servlet获取表单中的数据及其应用_第2张图片
通过Servlet获取表单中的数据及其应用_第3张图片

你可能感兴趣的:(JavaWeb)