[轉貼]Understanding Servlet request

Understanding Servlet request

ServletRequest Is one of the most important Interface of Servlet API. This tutorial explains the basics of ServletRequest interface and the most widely used methods defined in it.

What is servlet request

Servlets are designed to receive client request and generate responce to send to client. Though servlets are protocol independent, they are most widely used wih HTTP protocol to generate the dynamic response. The servlet request interface represents the request sent by the client (Browser in our case) . Whenever a web container receives a request from the browser, it creates a servlet request, and identifies which servlet needs to be invoked based on the URL mapping, calls the service method of the servlet and passes the servlet request object as parameter. The servlet request object contains information like request parameters, request headers etc. which is used by Servlet when generating the response.

Javax.servlet.ServletRequest

ServletRequest is an interface defined in javax.servlet package. Servlet request represents a client request and contains information required by a Servlet to generate the response.

Javax.servlet.HttpServletRequest

HttpServletRequest interface extends ServletRequest interface and provides methods to get the information related to HTTP protocol request. When writing servlets, you will encounter HttpServletRequest only, Because service method of HttpServlet takes HttpServletRequest as parameter.

Don't get confused between javax.servlet.ServletRequest and javax.servlet.http.HttpServletRequest, as HttpServletRequest interface extends ServletRequest, all the methods defined in ServletRequest is available to HttpServletRequest also.

Now, let's see some of the most commnly used methods of servlet request.

Methods for request parameters

One of the most common use of servlet request is to obtain the request paramaters. When browser sends a GET or POST request, form parameters (form input elements) and URL query string parameters are available as request parameters.

public String getParameter(String name)

getParameter() method is used to obtain the value of a parameter by name.

For example, When user submits a form which has a input element of type text with name username as shown below. getParameter(“username”) will return what user has entered into the text box.

<input type=”text” name=”username”/>

Getting all selected values of a multiselect list box.

public String[] getParameterValues(String name)

geParameterValues() returns an array of String containing all values of given parameter name. It is mainly used to obtain values of a Multi select list box.

Get all parameter names

 java.util.Enumeration getParameterNames() 

getParameterNames() method retuns an enumeration of all of the request parameter names.

The getParameterMap() method.

public Java.util.Map getParameterMap() method.

getParameterMap() method returns a Map of all request parameters. The parameter name is key and the parameter value is the value of the map.

Methods for request attributes

Servlets,JSP and filters can store information as request attributes. The life time of objects stored as request attributes is that of the request itself. A servlet can use request attributes to pass the data to another servlet, or JSP page. When a request is forwareded to another servlet or JSP, the request attributes stored by previous servlet would be available to the servlet or JSP page to which the request is forwarded.

The setAttribute() method

public void String setAttribute(String name, Object value)

The setAttribute() method is used to set a named attribute

The getAttribute() method

public Object getAttribute(String name) 

The getAttribute() method returns the value of a named attribute, or Null if the attribute does not exist.

The getAttributeNames() method

public Enumeration getAttributeNames()

getAttributeNames() method returns an Enumeration of all request attribute names.

The removeAttribute() method

public void removeAttribute(String name)

The removeAttribute() method is used to remove a named attribute.

Methods for request Headers

The getHeader() method

public String getHeader(String name) 

The getHeader() method returns the value of a named HTTP header.

The getHeaders() method

public Enumeration getHeaders()

The getHeaders() method returns an Enumeration of all HTTP request header values sent by the browser.

The getHeaderNames() method

public Enumeration getHeaderNames() 

The getHeaderNames() method returns an Enumeration of all request header names.

Get the Cookies sent by the browser

Cookies are used to store the information on client (Browser).

The getCookies() method

public Cookie[] getCookies()

getCookies() method returns an array of javax.servlet.http.Cookie.

留言

這個網誌中的熱門文章

WPF - 深入 Style

C# – M$ Chart Control 自定 ToolTip 的顯示

Vue.js - 基礎介紹教學