• 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>

    结果图:

  • 相关阅读:
    20145229吴姗珊《信息安全系统设计基础》第3周学习总结
    20145229《信息安全系统设计基础》第2周学习总结
    20145207 myeclipse测试
    20145207 ms11_050漏洞攻击
    20145207 ms08_067攻击实验
    20145207《Java程序设计》实验一(Java开发环境的熟悉)实验报告
    20145207《Java程序设计》第7周学习总结
    20145207李祉昂《网络对抗技术》恶意代码分析
    20145207《Java程序设计》第6周学习总结
    20145207《网络对抗》免杀原理与实践
  • 原文地址:https://www.cnblogs.com/yunqing/p/7998791.html
Copyright © 2020-2023  润新知