zoukankan      html  css  js  c++  java
  • servlet HTTP获取请求数据

      <body>
        This is my HTML page. <br>
            <h3>get </h3>
        <form  action="/day1/requestdemo2" method="GET">
            用户名:<input type"text" name="name"/><br/>
            密码:<input type="password" name="password"/><br/>
            <input type="submit" value="提交">
      
        </form>
        <h3>Post </h3>
        <form  action="/day1/requestdemo2" method="POST">
            用户名:<input type"text" name="name"/><br/>
            密码:<input type="password" name="password"/><br/>
            <input type="submit" value="提交">
      
        </form>
      </body>

    不推荐的办法:

    public class requestdemo2 extends HttpServlet {
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
    System.out.println("get:");
    String value=request.getQueryString();
    System.out.println(value);
        }
    
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            System.out.println("post:");
    InputStream in=  request.getInputStream();
    byte[] buf=new byte[1024];
    int len=0;
    while((len=in.read())!=-1){
        String str= new String(buf,0,len);
        System.out.println(str);
    }
    
        }

    推荐方式:

    doget(),dopost()通用,获取数据方式

        String name=    request.getParameter("name");
        String password=    request.getParameter("password");
            System.out.println(name+":"+password);
            
            System.out.println("================================================");
            Enumeration<String > enums=request.getParameterNames();
            while(enums.hasMoreElements()){
                String paraname=enums.nextElement();
                String paravalue=request.getParameter(paraname);
                System.out.println(paraname+"="+paravalue);
            }

    传递的请求参数如何获取              

                               GET方式: 参数放在URI后面

                               POST方式: 参数放在实体内容中

                              获取GET方式参数:

                                                request.getQueryString();

                              获取POST方式参数:

                                                request.getInputStream();

                              问题:但是以上两种不通用,而且获取到的参数还需要进一步地解析。

                              所以可以使用统一方便的获取参数的方式:

                                      

                                      核心的API:

                                       request.getParameter("参数名");  根据参数名获取参数值(注意,只能获取一个值的参数)

                                       request.getParameterValue("参数名“);根据参数名获取参数值(可以获取多个值的参数)

                                       request.getParameterNames();   获取所有参数名称列表  

  • 相关阅读:
    AtCoder Regular Contest 093
    AtCoder Regular Contest 094
    G. Gangsters in Central City
    HGOI 20190711 题解
    HGOI20190710 题解
    HGOI 20190709 题解
    HGOI 20190708 题解
    HGOI20190707 题解
    HGOI20190706 题解
    HGOI 20190705 题解
  • 原文地址:https://www.cnblogs.com/yimian/p/7016591.html
Copyright © 2011-2022 走看看