• 关于request请求的基本获取


     
    1.Request对象的作用是与客户端交互,收集客户端的Form、Cookies、超链接,或者收集服务器端的环境变量。
    request对象是从客户端向服务器发出请求,包括用户提交的信息以及客户端的一些信息。
    客户端可通过HTML表单或在网页地址后面提供参数的方法提交数据,然后通过request对象的相关方法来获取这些数据。
    request的各种方法主要用来处理客户端浏览器提交的请求中的各项参数和选项。

    2.requset的常用命令设置

    //1.获取请求方式
            String method = request.getMethod();

    //2.获得请求的资源
            String requestURI = request.getRequestURI();
            StringBuffer requestURL = request.getRequestURL();

    //3.获取web应用的名称
            String contextPath = request.getContextPath();

    //4.地址后的参数字符串
            String queryString = request.getQueryString();

    //5.获取客户端信息--获得IP地址
            String remoteAddr = request.getRemoteAddr();

    3.相应结果

    4.完整代码

    .html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="/WEB15/line" method="post">
           姓名:<input type="text" name="username"><br>
           密码:<input type="password" name="password"><br>
           <input type="submit" value="提交"><br>
        </form>
    </body>
    </html>

    Lineservlet

    package com.hdh.servlet;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class LineServlet extends HttpServlet {
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            // 1.获取请求方式
            String method = request.getMethod();
            System.out.println("method:" + method);
    
            // 2.获得请求的资源
            String requestURI = request.getRequestURI();
            StringBuffer requestURL = request.getRequestURL();
            System.out.println("uri:" + requestURI);
            System.out.println("url:" + requestURL);
    
            // 3.获取web应用的名称
            String contextPath = request.getContextPath();
            System.out.println("web应用名称:" + contextPath);
    
            // 4.地址后的参数字符串
            String queryString = request.getQueryString();
            System.out.println("queryString:" + queryString);
            
            //5.获取客户端信息--获得IP地址
            String remoteAddr = request.getRemoteAddr();
            System.out.println("remoteAddr:"+remoteAddr);
    
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request, response);
        }
    
    }

    5.注意事项

     <form action="/WEB15/line" method="post">

    action的提交路径为LineServlet 在web.xml中的mapping的访问路径(url-pattern);

     
  • 相关阅读:
    2.7 矩阵的秩
    HDU
    HDU
    HDU
    HDU
    HDU
    hdu 5179 beautiful number(数位dp)
    ACdream
    CodeForces
    <a>标签中 href="/" 和 hideFocus="true"
  • 原文地址:https://www.cnblogs.com/asndxj/p/9821982.html
Copyright © 2020-2023  润新知