• Request和Response学习笔记3


    获取请求参数通用方式

    获取请求参数通用方式

     String getParameter(String name)
     // 根据参数名称获取参数值
     
     String[] getParameterValues(String name)
     // 根据参数名称获取参数值的数组 
     
     Enumeration<String> getParameterNames()
     // 获取所有请求的参数名称
     
     Map<String,String[]> getParameterMap()
     // 获取所有参数的map集合
    
    无论get还是post请求方式都可以使用这些方法来获取请求参数
    

    根据参数获取参数值

    1. 创建一个html文件:login02.html

      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>用户登录</title>
      </head>
      <body>
          <form action="/RequestAndResponse_war_exploded/study/request/demo04" method="post">
              <input type="email" name="userEmail" placeholder="请输入您的邮箱"><br>
              <input type="password" name="userPassword" placeholder="请输入您的密码"><br>
              <input type="submit" value="post登录">
          </form>
          <br>
          <form action="/RequestAndResponse_war_exploded/study/request/demo04" method="get">
              <input type="email" name="userEmail" placeholder="请输入您的邮箱"><br>
              <input type="password" name="userPassword" placeholder="请输入您的密码"><br>
              <input type="submit" value="get登录">
          </form>
      </body>
      </html>
      
    2. 创建一个类,RequestDemo04.java

      package study.request;
      
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.IOException;
      
      @WebServlet("/study/request/demo04")
      public class RequestDemo04 extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                  throws ServletException, IOException {
              String userEmail = req.getParameter("userEmail");
              String userPassword = req.getParameter("userPassword");
              System.out.println("userEmail = " + userEmail + "
      " + "userPassword = " + userPassword);
          }
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                  throws ServletException, IOException {
              String userEmail = req.getParameter("userEmail");
              String userPassword = req.getParameter("userPassword");
              System.out.println("userEmail = " + userEmail + "
      " + "userPassword = " + userPassword);
          }
      }
      
    3. 上面的 doGet() 方法 和 doPost() 方法中的方法体内容一样,可以进行修改:

      public class RequestDemo04 extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                  throws ServletException, IOException {
              String userEmail = req.getParameter("userEmail");
              String userPassword = req.getParameter("userPassword");
              System.out.println("userEmail = " + userEmail + "
      " + "userPassword = " + userPassword);
          }
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                  throws ServletException, IOException {
              this.doGet(req, resp);
          }
      }
      
    4. 浏览器访问:http://localhost:8080/RequestAndResponse_war_exploded/login02.html

    根据参数名称获取参数值的数组

    1. 创建一个html文件:login03.html

      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>爱好</title>
      </head>
      <body>
          <form action="/RequestAndResponse_war_exploded/study/request/demo05" method="post">
              <input type="checkbox" name="hobby" value="study"> 学习
              <input type="checkbox" name="hobby" value="game"> 游戏 <br>
              <input type="submit" value="post提交">
          </form>
          <br>
          <form action="/RequestAndResponse_war_exploded/study/request/demo05" method="get">
              <input type="checkbox" name="hobby" value="study"> 学习
              <input type="checkbox" name="hobby" value="game"> 游戏 <br>
              <input type="submit" value="get提交">
          </form>
      </body>
      </html>
      
    2. 创建一个demo.java:RequestDemo05.java

      package study.request;
      
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.IOException;
      
      @WebServlet("/study/request/demo05")
      public class RequestDemo05 extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                  throws ServletException, IOException {
              // 根据参数名称获取参数值的数组
              String[] hobbies = req.getParameterValues("hobby");     // 参数名称为hobby
              for (String hobby : hobbies) {
                  System.out.println(hobby);
              }
          }
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                  throws ServletException, IOException {
              this.doGet(req, resp);
          }
      }
      
    3. 启动服务器,访问:http://localhost:8080/RequestAndResponse_war_exploded/login03.html

      这里两种提交方式都一样!!!

    获取所有请求的参数名称

    1. 创建一个类,RequestDemo06.java

      package study.request;
      
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.IOException;
      import java.util.Enumeration;
      
      @WebServlet("/study/request/demo06")
      public class RequestDemo06 extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                  throws ServletException, IOException {
              // 获取所有请求参数名称
              Enumeration<String> parameterNames = req.getParameterNames();
              while (parameterNames.hasMoreElements()) {
                  String parameterName = parameterNames.nextElement();
                  // 输出参数名称,已经根据参数名称输出对应的值
                  System.out.println(parameterName + ":" + req.getParameter(parameterName) + "
      ");
              }
          }
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                  throws ServletException, IOException {
              this.doGet(req, resp);
          }
      }
      
    2. 创建余个html文件:login04.html

      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>用户爱好</title>
      </head>
      <body>
      <form action="/RequestAndResponse_war_exploded/study/request/demo06" method="get">
          <label><input type="email" name="userEmail" placeholder="请输入您的邮箱"></label><br>
          <label><input type="password" name="userPassword" placeholder="请输入您的密码"></label><br>
          <label><input type="checkbox" name="hobby1" value="study"></label> 学习
          <label><input type="checkbox" name="hobby2" value="game"></label> 游戏 <br>
          <input type="submit" value="提交">
      </form>
      </body>
      </html>
      
    3. 启动服务器,访问:http://localhost:8080/RequestAndResponse_war_exploded/login04.html

      20200518212359

      提交,控制台输出:

      userEmail:stringbug@icloud.com
      
      userPassword:123456
      
      hobby1:study
      
      hobby2:game
      
      

      浏览器页面跳转到:http://localhost:8080/RequestAndResponse_war_exploded/study/request/demo06?userEmail=stringbug%40icloud.com&userPassword=123456&hobby1=study&hobby2=game

    获取所有参数的map集合

    1. 创建一个html文件:login05.html

      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>用户爱好</title>
      </head>
      <body>
      <form action="/RequestAndResponse_war_exploded/study/request/demo07" method="get">
          <label><input type="email" name="userEmail" placeholder="请输入您的邮箱"></label><br>
          <label><input type="password" name="userPassword" placeholder="请输入您的密码"></label><br>
          <label><input type="checkbox" name="hobby" value="study"></label> 学习
          <label><input type="checkbox" name="hobby" value="game"></label> 游戏 <br>
          <input type="submit" value="提交">
      </form>
      </body>
      </html>
      

      login05.html 与 login04.html 的差别是:将hobby1、hobby2均改为了hobby

      将路径中的demo06改为了demo07

    2. 创建一个类:RequestDemo07.java

      package study.request;
      
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.IOException;
      import java.util.Map;
      import java.util.Set;
      
      @WebServlet("/study/request/demo07")
      public class RequestDemo07 extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                  throws ServletException, IOException {
              // 获取所有请求参数的map集合
              Map<String, String[]> parameterMap = req.getParameterMap();
              // 获取所有的请求名称
              Set<String> parameterNames = parameterMap.keySet();
              for (String parameterName : parameterNames) {
                  // 输出每个请求名称
                  System.out.println("请求名称 -- " + parameterName);
                  // 根据请求名称获取对应的请求值
                  String[] parameterValues = req.getParameterValues(parameterName);
                  for (String parameterValue : parameterValues) {
                      // 输出每个请求值
                      System.out.println(parameterName + ":" + parameterValue);
                  }
              }
          }
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                  throws ServletException, IOException {
              this.doGet(req, resp);
          }
      }
      
    3. 启动服务器,访问:http://localhost:8080/RequestAndResponse_war_exploded/login05.html

      20200518214730
    4. 提交,控制台输出:

      请求名称 -- userEmail
      userEmail:stringbug@icloud.com
      请求名称 -- userPassword
      userPassword:123456
      请求名称 -- hobby
      hobby:study
      hobby:game
      

      浏览器页面跳转到:http://localhost:8080/RequestAndResponse_war_exploded/study/request/demo07?userEmail=stringbug%40icloud.com&userPassword=123456&hobby=study&hobby=game

    参考文献

    Good Good Write Bug, Day Day Up
  • 相关阅读:
    leetcode------Sum Root to Leaf Numbers
    TesserOCR训练
    【转载】C#.Net 创建网页快捷方式
    错误:没有为扩展名“.html”注册的生成提供程序。
    【转载】错误 CS0016: 未能写入输出文件“c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/.........dll”--“拒绝访问。 ”
    [转载]ASP.NET对路径"xxxxx"的访问被拒绝的解决方法小结
    [转载]ASP.NET对路径"xxxxx"的访问被拒绝的解决方法小结
    [转载]AFX_MANAGE_STATE关于资源切换
    OD鲜为人知的小技巧--搜索通配符(关键字)
    【转载】汇编跳转指令集
  • 原文地址:https://www.cnblogs.com/liyihua/p/14477444.html
Copyright © 2020-2023  润新知