request

 

HttpServletRequest对象代表客户端的请求。当客户端通过HTTP协议访问服务器时,HTTP头中的所有信息都会封装在这个对象中。开发人员通过这个对象的方法,可以获得客户这些信息。

 

Request是一个域对象

 

一般请求包含

  • 请求方式  通过getMethod()方法
  • 请求协议  一般都是http
  • 请求资源  getRequestURL() 和getRequestURI()

 

http://www.sina.com/news/1.html

整体是URL 标识互联网上的资源

/news/1.html 是URI ,标识任意资源

 

常见用法

  • getRequestURI,获取请求资源
  • 获取请求头和请求数据
  • 中文乱码
  • 实现转发
  • Include

 

 

 

Request常用方法:

 

             

 

 

1. getRequestURI,获取请求资源

 

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println(request.getRequestURI());
        System.out.println(request.getRequestURL());    
        
        /*
        输出:
        /day1121/servlet/RequestDemo1
        http://localhost:8080/day1121/servlet/RequestDemo1
}

 

getRequestURI是获取请求资源,一般作用:

 

    通过得到用户请求资源之后,可以:

  • 1.权限管理

        开发过程中,写一个过滤器,拦截用户所有请求,然后用这个方法得到用户请求的资源, 再看看这个资源访问需要什么权限,再看看该请求用户有没有这个权限

  • 2.统计页面访问次数

 

2. 获取请求头和请求数据

 

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //获取指定头
        
String head = request.getHeader("Accept-Encoding"); //获取指定头的所有数据 Enumeration heads = request.getHeaders("Accept-Encoding"); //获取客户机带过来的所有的头名称 heads = request.getHeaderNames(); //获取请求数据值
//获取特定的值 String value = request.getParameter("username"); System.out.print(value); //获取特定的值的所有数据 String[] es = request.getParameterValues("username"); //获取所有的值 Enumeration e = request.getParameterNames(); }

 

客户端向服务器传送数据有两种方式:

  • 1.通过超链接
  • 2.通过表单 

 

<!DOCTYPE html>
<html>
  <head>
    <title>带数据给服务器</title>
    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
   
<!--    两种方式:
   1.通过超链接-->

    <a href="/day1121/servlet/RequestDemo2?username=QQQ">点击</a>
  <-- 2.通过表单 -->
    <form action="/day1121/servlet/RequestDemo2" method="post"> 
用户名 <input type="text" name="username">
<input type="submit" value="提交">
</form>
</body>
</html>

 

3. 中文乱码

 

request处理中文乱码,对于get和post请求是不一致的。

 

<!DOCTYPE html>
<html>
  <head>
    <title>中文乱码</title>
    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
    提交数据方式
     1.表单提交   
post提交
<form action="/day1121/servlet/RequestDemo3" method="post"> 用户名 <input type="text" name="username"> <input type="submit" value="提交"> </form> </body> get提交 get请求的数据其实也是显示在地址栏中的 <body> <form action="/day1121/servlet/RequestDemo3" method="get"> 用户名 <input type="text" name="username"> <input type="submit" value="提交"> </form> 超链接提交的中文 超链接是get请求 <a href="/day1121/servlet/RequestDemo2?username=中国">点击</a> </body> </html>

 

在servlet里的处理

客户端上的汉字以何种方式编码传到服务器,不是固定的,就看当前页面以何种编码方式打开

public class RequestDemo3 extends HttpServlet {

    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        

        //get需要手工处理

//得到的username字符串实际上已经是servlet经过iso8859编码而成的 String username = request.getParameter("username"); //先把字符串还原到编码之前,再通过UTF-8编码 username = new String(username.getBytes("iso8859-1"),"UTF-8"); System.out.println(username); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String username = request.getParameter("username"); System.out.println(username); } }

 

4.实现转发

 

不仅可以通过response实现转发,也可以通过request实现转发

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String data = "aaa";
        
        request.setAttribute("data", data); 
        
        //request也可以实现转化
        request.getRequestDispatcher("/message.jsp").forward(request, response);
}

 

转发(forward)几个细节问题:

 

  • forward实现转发地址栏不会变化
  • 转发过程中不会创建新的request和response对象
  • 客户端只发送一次请求,服务器需要多个资源响应

 

  • 1. forward方法用于将请求转发到RequestDispatch对象封装的资源
  • 2. 如果在调用forward方法之前,servlet程序中写入的内容已经被真正地传送到客户端了,forward将会抛出IllegalStateException异常

      比如下面这个程序

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

         String data = "aaa";
        
        
        
        if(true){
             request.getRequestDispatcher("/MyJsp.jsp").forward(request, response);
             //return;
        }
        
        //如果下面这句话也执行了,就一定会报错!
        //改进的方法是在每一个forward方法后面加上一句话 return;
        request.getRequestDispatcher("/index.jsp").forward(request, response);
        //return;
    }

 

  •  3. 如果在调用forward方法之前向servlet引擎的缓存的缓冲区(response)中写入内容,只要写入的内容还没有真正地输入到客户端,forward方法被执行的时候都会清空缓冲区里的内容,但是,已写入到HttpServletResponse对象中的响应头字段保持有效

 

 

5.Include

 

       RequestDispatch地址不仅有forward方法,还有Include方法

 

     //include
    //加入网头和网脚
    //不过实际开发中是不会在servlet里面处理数据输出的,要在jsp里面处理,这个以后再说
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.getRequestDispatcher("/Public/head.jsp").include(request, response);
        response.getWriter().write("aaaa");
        request.getRequestDispatcher("/Public/foot.jsp").include(request, response);
    }

 

posted on 2014-11-29 12:42  飞鸟快跑  阅读(338)  评论(0编辑  收藏  举报