servletRequestAndResponse

来源:互联网 发布:如何拍好淘宝产品照片 编辑:程序博客网 时间:2024/05/29 19:02

一、       Introduction

Servlet的主要工作是对请求的处理和响应处理结果。具体是通过get或者post方法中的request和response对象完成。B/S架构中,客户端浏览器通过get或者post方法向服务端发起请求前提是遵循http协议,http协议中主要是关于请求和响应的协议。Servlet的request和response对象正是依据http的协议设计和执行。Request对象用于处理用户的请求,response用于向用户响应数据。

二、       Request

Servlet的service方法中doGet和doPost方法,将HttpServletReqeust接口的对象,即request作为参数传入使用。其中常用的方法有:

获得请求的方式(get还是post)、获得请求资源、获得协议类型、获得IP地址、获得端口号、获得请求头信息

1.       Method

 String

getAuthType()
          Returns the name of the authentication scheme used to protect the servlet.

 String

getContextPath()
          Returns the portion of the request URI that indicates the context of the request.

 Cookie[]

getCookies()
          Returns an array containing all of the Cookie objects the client sent with this request.

 long

getDateHeader(String name)
          Returns the value of the specified request header as a long value that represents a Date object.

 String

getHeader(String name)
          Returns the value of the specified request header as a String.

 Enumeration

getHeaderNames()
          Returns an enumeration of all the header names this request contains.

 Enumeration

getHeaders(String name)
          Returns all the values of the specified request header as an Enumeration of String objects.

 int

getIntHeader(String name)
          Returns the value of the specified request header as an int.

 String

getMethod()
          Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.

 String

getPathInfo()
          Returns any extra path information associated with the URL the client sent when it made this request.

 String

getPathTranslated()
          Returns any extra path information after the servlet name but before the query string, and translates it to a real path.

 String

getQueryString()
          Returns the query string that is contained in the request URL after the path.

 String

getRemoteUser()
          Returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated.

 String

getRequestedSessionId()
          Returns the session ID specified by the client.

 String

getRequestURI()
          Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.

 StringBuffer

getRequestURL()
          Reconstructs the URL the client used to make the request.

 String

getServletPath()
          Returns the part of this request's URL that calls the servlet.

 HttpSession

getSession()
          Returns the current session associated with this request, or if the request does not have a session, creates one.

 HttpSession

getSession(boolean create)
          Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.

 Principal

getUserPrincipal()
          Returns a java.security.Principal object containing the name of the current authenticated user.

 boolean

isRequestedSessionIdFromCookie()
          Checks whether the requested session ID came in as a cookie.

 boolean

isRequestedSessionIdFromUrl()
          Deprecated. As of Version 2.1 of the Java Servlet API, useisRequestedSessionIdFromURL() instead.

 boolean

isRequestedSessionIdFromURL()
          Checks whether the requested session ID came in as part of the request URL.

 boolean

isRequestedSessionIdValid()
          Checks whether the requested session ID is still valid.

 boolean

isUserInRole(String role)
          Returns a boolean indicating whether the authenticated user is included in the specified logical "role".

2.       关于获得请求行信息

   protected voiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException, IOException {

      String method=request.getMethod();

      String uri=request.getRequestURI();

      String protocol=request.getProtocol();

      String ip=request.getRemoteAddr();

      int port=request.getRemotePort();

      System.out.println(method+"--"+uri+"--"+ip+"--"+port+"--"+protocol);

   }

3.       关于获得请求头信息

在请求头信息中,有一个referer参数,这个表示请求源的引用路径。引用路径,说白了就是请求通过什么路径连接到服务器的。如果不是通过点击或者form表单形式的请求,也就是说通过地址栏输入地址的方式的请求,那么这个referer的值为空。这个参数可以用于防盗链设置。方法即判断请求源的引用路径是否不为空且等于特定全路径。

   protected voiddoGet(HttpServletRequest request, HttpServletResponseresponse)throwsServletException, IOException {

      //获得请求头信息

      //获得请求主机和端口号

      String host=request.getHeader("Host");

      //获得用户代理

      String userAgent=request.getHeader("User-Agent");

      //获得cookies

      String cookie=request.getHeader("Cookie");

      System.out.println(host+"--"+userAgent+"--"+cookie);

      //获得请求源的url引用

      String referer=request.getHeader("referer");

      System.out.println(referer);

   }

4.       关于请求乱码

1)       Get方式请求的乱码

先采用来源的编码来编码,再采用使用方的编码来解码。

如:浏览器采用的utf-8编码,而生产环境采用gbk编码

Stringusername=request.getParameter(“username”);

这时,得到的username是将utf-8编码的数据,采用gbk解码了,所以,需要先采用gbk编码,再采用utf-8解码,就可以得到信息原来的编码的内容。

username=newString(username.getBytes(“ISO-8859-1”) “utf-8”);

2)       Post方式请求的乱码

请在获取请求参数之前,设置,比如网页为utf-8编码环境下:

request.setCharacterEncoding(“utf-8”);

3)       顺便介绍响应乱码的处理

response.setContentType("text/html;charset=UTF-8");

5.       获取请求参数

通过form表单,获取text类型的请求时,使用getParameter()方法。如果需要获得checkbox类型的参数,由于是多个参数的情况,需要使用getParameterValues()方法,而getParameterMap()方法一般用于BeanUtils类中获取form表单的全部参数的映射对并放入javabean。

例子:

   protected voiddoGet(HttpServletRequest request, HttpServletResponseresponse)throwsServletException, IOException {

      String username=request.getParameter("username");

      String[] hobbies=request.getParameterValues("hobby");

      System.out.println(username);

      for(Stringhobby:hobbies){

         System.out.println(hobby);

      }

   }

6.       请求转发

如果客户端发送到servlet的请求,需要根据情况,将请求流转交给另一个servlet处理,就产生了请求转发的需求。同一个请求流的转发可以多次进行,最后的servlet将处理结果响应给客户端。由于一次请求的生命周期,是请求发出和响应完成,所以,在请求转发过程中,需要保持请求流的唯一性和延续性,相当于,在请求的生命周期中,request可以作为一个容器,存放请求的数据,响应完成,则request容器清空。每次产生请求,则request容器再次存放相应的请求数据。

 void

removeAttribute(String name)
          Removes an attribute from this request.

 void

setAttribute(String name,Object o)
          Stores an attribute in this request.

 Object

getAttribute(String name)
          Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.

 Enumeration

getAttributeNames()
          Returns an Enumeration containing the names of the attributes available to this request.

 

例子:

   protected voiddoGet(HttpServletRequest request, HttpServletResponseresponse)throwsServletException, IOException {

      request.setAttribute("request","来自第一个servlet的转发请求");

      //由于请求是在项目工程内执行,所有转发路径不需要写项目地址名

      RequestDispatcher requestDispatcher=request.getRequestDispatcher("/Request2");

      //forward根据新地址转寄,就是将这个servletrequest对象和response对象转寄给目标servlet

      requestDispatcher.forward(request,response);

   }

   protected voiddoGet(HttpServletRequest request, HttpServletResponseresponse)throwsServletException, IOException {

      //使用request,获得request中的属性内容

      String requestContent=(String)request.getAttribute("request");

      System.out.println(requestContent);

      //将数据响应给客户端

      response.setContentType("text/html;charset=utf-8");

      response.getWriter().write("来自servlet2的响应");

   }

关于请求转发语句的链式简写方式:

request.getRequestDispatcher("/Request2").forward(request,response);

三、       Response

Servlet的service方法中doGet和doPost方法,将HttpServletResponse接口的对象,即response作为参数传入使用。主要用于实现向客户端发送响应的功能。

 void

addCookie(Cookie cookie)
          Adds the specified cookie to the response.

 void

addDateHeader(String name, long date)
          Adds a response header with the given name and date-value.

 void

addHeader(String name,String value)
          Adds a response header with the given name and value.

 void

addIntHeader(String name, int value)
          Adds a response header with the given name and integer value.

 boolean

containsHeader(String name)
          Returns a boolean indicating whether the named response header has already been set.

 String

encodeRedirectUrl(String url)
          Deprecated. As of version 2.1, use encodeRedirectURL(String url) instead

 String

encodeRedirectURL(String url)
          Encodes the specified URL for use in the sendRedirect method or, if encoding is not needed, returns the URL unchanged.

 String

encodeUrl(String url)
          Deprecated. As of version 2.1, use encodeURL(String url) instead

 String

encodeURL(String url)
          Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged.

 void

sendError(int sc)
          Sends an error response to the client using the specified status code and clearing the buffer.

 void

sendError(int sc,String msg)
          Sends an error response to the client using the specified status.

 void

sendRedirect(String location)
          Sends a temporary redirect response to the client using the specified redirect location URL.

 void

setDateHeader(String name, long date)
          Sets a response header with the given name and date-value.

 void

setHeader(String name,String value)
          Sets a response header with the given name and value.

 void

setIntHeader(String name, int value)
          Sets a response header with the given name and integer value.

 void

setStatus(int sc)
          Sets the status code for this response.

 void

setStatus(int sc,String sm)
          Deprecated. As of version 2.1, due to ambiguous meaning of the message parameter. To set a status code use setStatus(int), to send an error with a description use sendError(int, String). Sets the status code and message for this response.

1.       请求的重定向

对于用户请求,如果本servlet不能满足这个请求或者不方便满足这个请求,可以采用重定向,即直接响应给用户,要求用户再次发起请求到指定的servlet或者视图页面。

一般,重定向的实现步骤:1,响应给用户302状态码;2,设置新的被请求路径。

例子:

   protected voiddoGet(HttpServletRequest request, HttpServletResponseresponse)throwsServletException, IOException {

      //设置需要用户重定向请求的状态码

      response.setStatus(302);

      //设置响应头头信息,指定用户重新请求的地址路径。注意:由于重定向可以指向任何内容,包括本项目外内容,所有路径中需要包括具体的项目路径名

      response.setHeader("location","/servlet/Response2");

   }

另外,上述步骤可以合并。关于合并重定向语句的简写方式:

response.sendRedirect("/servlet/Response2");

2.       设置定时刷新

使用响应头中的refresh参数,设置定时跳转到重定向的路径。

//定时刷新的时间单位为秒,实际时间受到网络速度影响。

response.setHeader("refresh","5,/servlet/Response2");

3.       响应乱码的处理

见上述请求乱码的处理。

原创粉丝点击