<%-- Document : form Created on : 2020-4-12, 11:07:16 Author : Administrator --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title> 收集参数的表单页 </title> </head> <body> <form id="form1" action="aa"> 用户名:<br/> <input type="text" name="name"><hr/> 性别:<br/> 男:<input type="radio" name="gender" value="男"> 女:<input type="radio" name="gender" value="女"><hr/> 喜欢的颜色:<br/> 红:<input type="checkbox" name="color" value="红"> 绿:<input type="checkbox" name="color" value="绿"> 蓝:<input type="checkbox" name="color" value="蓝"><hr/> 来自的国家:<br/> <select name="country"> <option value="中国">中国</option> <option value="美国">美国</option> <option value="俄罗斯">俄罗斯</option> </select><hr/> <input type="submit" value="提交"> <input type="reset" value="重置"> </form> </body> </html>
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author Administrator */ public class FirstServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置解码方式 request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); // 获取name的请求参数值 String name = request.getParameter("name"); // 获取gender的请求参数值 String gender = request.getParameter("gender"); // 获取color的请求参数值 String[] color = request.getParameterValues("color"); // 获取country的请求参数值 String national = request.getParameter("country"); // 获取页面输出流 PrintStream out = new PrintStream(response.getOutputStream()); //输出HTML页面标签 out.println("<html>"); out.println("<head>"); out.println("<title>Servlet测试</title>"); out.println("</head>"); out.println("<body>"); // 输出请求参数的值:name out.println("您的名字:" + name + "<hr/>"); // 输出请求参数的值:gender out.println("您的性别:" + gender + "<hr/>"); // 输出请求参数的值:color out.println("您喜欢的颜色:"); for (String c : color) { out.println(c + " "); } out.println("<hr/>"); out.println("您喜欢的颜色:"); // 输出请求参数的值:national out.println("您来自的国家:" + national + "<hr/>"); out.println("</body>"); out.println("</html>"); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app metadata-complete="true" version="3.1" 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_3_1.xsd"> <!-- 配置Servlet的名字 --> <servlet> <!-- 指定Servlet的名字, 相当于指定@WebServlet的name属性 --> <servlet-name>firstServlet</servlet-name> <!-- 指定Servlet的实现类 --> <servlet-class>FirstServlet</servlet-class> </servlet> <!-- 配置Servlet的URL --> <servlet-mapping> <!-- 指定Servlet的名字 --> <servlet-name>firstServlet</servlet-name> <!-- 指定Servlet映射的URL地址, 相当于指定@WebServlet的urlPatterns属性--> <url-pattern>/aa</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>