ServletRequest的四个主要方法

 1 package com.yunqing.servlet;
 2 
 3 import javax.servlet.*;
 4 import java.io.IOException;
 5 import java.util.Arrays;
 6 import java.util.Enumeration;
 7 import java.util.Map;
 8 
 9 public class LoginServlet implements Servlet {
10 
11     @Override
12     public void init(ServletConfig servletConfig) throws ServletException {
13 
14     }
15 
16     @Override
17     public ServletConfig getServletConfig() {
18         return null;
19     }
20 
21     @Override
22     public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
23         //1.
24         System.out.println(servletRequest.getParameter("user") + "," + servletRequest.getParameter("password"));
25         //2.
26         String[] arr = servletRequest.getParameterValues("interesting");
27         for (String s : arr) {
28             System.out.println("--------" + s);
29         }
30         //3.
31         Enumeration<String> names = servletRequest.getParameterNames();
32         while (names.hasMoreElements()) {
33             String name = names.nextElement();
34             String value = servletRequest.getParameter(name);
35             System.out.println(name + "," +value);
36         }
37         //4.
38         Map<String,String[]> map = servletRequest.getParameterMap();
39         for (Map.Entry<String, String[]> en : map.entrySet()) {
40             System.out.println(en.getKey() + ":" + Arrays.asList(en.getValue()));
41         }
42     }
43 
44     @Override
45     public String getServletInfo() {
46         return null;
47     }
48 
49     @Override
50     public void destroy() {
51 
52     }
53 }

前台页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>login</title>
</head>
<body>
    <form action="LoginServlet" method="post">
        user:<input type="text" name="user">
        password:<input type="password" name="password">
        <br><br>
        interesting:
        <input type="checkbox" name="interesting" value="reading">Read
        <input type="checkbox" name="interesting" value="game">game
        <input type="checkbox" name="interesting" value="football">football
        <input type="checkbox" name="interesting" value="learn">learn
        <input type="checkbox" name="interesting" value="other">other
        <input type="submit" value="submit">
    </form>

</body>
</html>

结果图:

 

posted @ 2017-12-07 14:03  云晴  阅读(1128)  评论(0编辑  收藏  举报