HttpServletRequest和ServletRequest的区别以及HttpServletRequest对象方法的用法

HttpServletRequest和ServletRequest都是接口

HttpServletRequest继承自ServletRequest 

HttpServletRequest比ServletRequest多了一些针对于Http协议的方法。 例如:

getHeader(), getMethod() , getSession() 


  1. @Override    
  2. public void doFilter(ServletRequest request, ServletResponse response,    
  3.         FilterChain filterChain) throws IOException, ServletException {    
  4.     // 过滤用户请求,判断是否登录    
  5.     HttpServletRequest httpServletRequest = (HttpServletRequest)request;    
  6.     HttpServletResponse httpServletResponse = (HttpServletResponse)response;    
  7.     httpServletResponse .setContentType(”text/html;charset=utf-8”);    
  8.     httpServletRequest.setCharacterEncoding(”utf-8”);    
  9.     httpServletResponse.setCharacterEncoding(”utf-8”);    
  10.         
  11.     String username = (String)httpServletRequest.getSession().getAttribute(”username”);    
  12.     if (username == null) {    
  13.         String path = httpServletRequest.getContextPath();    
  14.         httpServletResponse.sendRedirect(path+”/index.jsp”);    
  15.     }    
  16.     filterChain.doFilter(httpServletRequest, httpServletResponse);    
  17. }    
    @Override  
    public void doFilter(ServletRequest request, ServletResponse response,  
            FilterChain filterChain) throws IOException, ServletException {  
        // 过滤用户请求,判断是否登录  
        HttpServletRequest httpServletRequest = (HttpServletRequest)request;  
        HttpServletResponse httpServletResponse = (HttpServletResponse)response;  
        httpServletResponse .setContentType("text/html;charset=utf-8");  
        httpServletRequest.setCharacterEncoding("utf-8");  
        httpServletResponse.setCharacterEncoding("utf-8");  

        String username = (String)httpServletRequest.getSession().getAttribute("username");  
        if (username == null) {  
            String path = httpServletRequest.getContextPath();  
            httpServletResponse.sendRedirect(path+"/index.jsp");  
        }  
        filterChain.doFilter(httpServletRequest, httpServletResponse);  
    }  

1. 获得客户机信息
    getRequestURL方法返回客户端发出请求时的完整URL。
    getRequestURI方法返回请求行中的资源名部分。
    getQueryString 方法返回请求行中的参数部分。
    getRemoteAddr方法返回发出请求的客户机的IP地址
    getRemoteHost方法返回发出请求的客户机的完整主机名
    getRemotePort方法返回客户机所使用的网络端口号
    getLocalAddr方法返回WEB服务器的IP地址。
    getLocalName方法返回WEB服务器的主机名
    getMethod得到客户机请求方式
 2.获得客户机请求头

    getHeader(string name)方法
    getHeaders(String name)方法
    getHeaderNames方法 

 3. 获得客户机请求参数(客户端提交的数据)
    getParameter(name)方法
    getParameterValues(String name)方法
    getParameterNames方法
    getParameterMap方法


例子程序:

  1. public void doGet(HttpServletRequest request, HttpServletResponse response)    
  2.         throws ServletException, IOException {    
  3.         System.out.println(”getRequestURL: ”+request.getRequestURL());    
  4.         System.out.println(”getRequestURI: ”+request.getRequestURI());    
  5.         System.out.println(”getQueryString: ”+request.getQueryString());    
  6.         System.out.println(”getRemoteAddr: ”+request.getRemoteAddr());    
  7.         System.out.println(”getRemoteHost: ”+request.getRemoteHost());    
  8.         System.out.println(”getRemotePort: ”+request.getRemotePort());    
  9.         System.out.println(”getRemoteUser: ”+request.getRemoteUser());    
  10.         System.out.println(”getLocalAddr: ”+request.getLocalAddr());    
  11.         System.out.println(”getLocalName: ”+request.getLocalName());    
  12.         System.out.println(”getLocalPort: ”+request.getLocalPort());    
  13.         System.out.println(”getMethod: ”+request.getMethod());    
  14.         System.out.println(”——-request.getParamterMap()——-“);    
  15.         //得到请求的参数Map,注意map的value是String数组类型    
  16.         Map map = request.getParameterMap();    
  17.         Set<String> keySet = map.keySet();    
  18.         for (String key : keySet) {    
  19.         String[] values = (String[]) map.get(key);    
  20.         for (String value : values) {    
  21.             System.out.println(key+”=”+value);    
  22.         }    
  23.      }    
  24.         System.out.println(”——–request.getHeader()——–”);    
  25.         //得到请求头的name集合    
  26.         Enumeration<String> em = request.getHeaderNames();    
  27.         while (em.hasMoreElements()) {    
  28.         String name = (String) em.nextElement();    
  29.         String value = request.getHeader(name);    
  30.         System.out.println(name+”=”+value);    
  31.     }    
  32.             
  33. }    
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
            System.out.println("getRequestURL: "+request.getRequestURL());  
            System.out.println("getRequestURI: "+request.getRequestURI());  
            System.out.println("getQueryString: "+request.getQueryString());  
            System.out.println("getRemoteAddr: "+request.getRemoteAddr());  
            System.out.println("getRemoteHost: "+request.getRemoteHost());  
            System.out.println("getRemotePort: "+request.getRemotePort());  
            System.out.println("getRemoteUser: "+request.getRemoteUser());  
            System.out.println("getLocalAddr: "+request.getLocalAddr());  
            System.out.println("getLocalName: "+request.getLocalName());  
            System.out.println("getLocalPort: "+request.getLocalPort());  
            System.out.println("getMethod: "+request.getMethod());  
            System.out.println("-------request.getParamterMap()-------");  
            //得到请求的参数Map,注意map的value是String数组类型  
            Map map = request.getParameterMap();  
            Set<String> keySet = map.keySet();  
            for (String key : keySet) {  
            String[] values = (String[]) map.get(key);  
            for (String value : values) {  
                System.out.println(key+"="+value);  
            }  
         }  
            System.out.println("--------request.getHeader()--------");  
            //得到请求头的name集合  
            Enumeration<String> em = request.getHeaderNames();  
            while (em.hasMoreElements()) {  
            String name = (String) em.nextElement();  
            String value = request.getHeader(name);  
            System.out.println(name+"="+value);  
        }  

    }  

浏览器上地址栏:http://localhost:8080/RequestAndResponse/requestmethod?name=sunjob&password=123456&password=haha

控制台输出:

  1. getRequestURL: http://localhost:8080/RequestAndResponse/requestmethod    
  2. getRequestURI: /RequestAndResponse/requestmethod    
  3. getQueryString: name=sunjob&password=123456&password=haha    
  4. getRemoteAddr: 127.0.0.1    
  5. getRemoteHost: 127.0.0.1    
  6. getRemotePort: 2374    
  7. getRemoteUser: null    
  8. getLocalAddr: 127.0.0.1    
  9. getLocalName: localhost    
  10. getLocalPort: 8080    
  11. getMethod: GET    
  12. ——-request.getParamterMap()——-    
  13. name=sunjob    
  14. password=123456    
  15. password=haha    
  16. ——–request.getHeader()——–    
  17. host=localhost:8080    
  18. user-agent=Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20100101 Firefox/17.0    
  19. accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8    
  20. accept-language=zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3    
  21. accept-encoding=gzip, deflate    
  22. connection=keep-alive    
  23. cache-control=max-age=0    
    getRequestURL: http://localhost:8080/RequestAndResponse/requestmethod  
    getRequestURI: /RequestAndResponse/requestmethod  
    getQueryString: name=sunjob&password=123456&password=haha  
    getRemoteAddr: 127.0.0.1  
    getRemoteHost: 127.0.0.1  
    getRemotePort: 2374  
    getRemoteUser: null  
    getLocalAddr: 127.0.0.1  
    getLocalName: localhost  
    getLocalPort: 8080  
    getMethod: GET  
    -------request.getParamterMap()-------  
    name=sunjob  
    password=123456  
    password=haha  
    --------request.getHeader()--------  
    host=localhost:8080  
    user-agent=Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20100101 Firefox/17.0  
    accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8  
    accept-language=zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3  
    accept-encoding=gzip, deflate  
    connection=keep-alive  
    cache-control=max-age=0  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值