• 入门servlet:request获取请求行数据


     1 /**
     2  * 演示Request对象获取请求行数据
     3  */
     4 @WebServlet("/test")
     5 public class RequestDemo1 extends HttpServlet {
     6     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     7 
     8     }
     9 
    10     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    11         //一、获取请求行
    12         //1.获取请求方式:GET
    13         String method = request.getMethod();
    14         System.out.println(method);
    15         //2.(*)获取虚拟目录:/request
    16         String contextPath = request.getContextPath();
    17         System.out.println(contextPath);
    18         //3.获取Servlet路径:/demo
    19         String servletPath = request.getServletPath();
    20         System.out.println(servletPath);
    21         //4.获取get方式请求参数:name=zhangshan
    22         String queryString = request.getQueryString();
    23         System.out.println(queryString);
    24         //5.(*)获取请求URI:/request/demo
    25             //* String getRequestURI(): /request/demo
    26         String requestURI = request.getRequestURI();
    27         System.out.println(requestURI);
    28         //* StringBuffer getRequestURI():http://localhost/request/demo
    29         StringBuffer requestURL = request.getRequestURL();
    30         System.out.println(requestURL);
    31         //6.获取协议及版本:HTTP/1.1
    32         String protocol = request.getProtocol();
    33         System.out.println(protocol);
    34         //7.获取客户机的IP地址:
    35             //*String getRemoteAddr()
    36         String remoteUser = request.getRemoteUser();
    37         System.out.println(remoteUser);
    38     }
    39 }
  • 相关阅读:
    第二阶段站立会议(3)
    第二阶段站立会议(2)
    第二阶段站立会议(1)
    返回一个最大联通子数组的和
    场景调研
    课程改进意见
    百度搜索引擎——评价
    求1的个数
    《校园封神榜》个人工作总结——第十天
    《校园封神榜》个人工作总结——第九天
  • 原文地址:https://www.cnblogs.com/flypig666/p/11480339.html
Copyright © 2020-2023  润新知