Get Started. It's Free
or sign up with your email address
Rocket clouds
CHAPTER 2 by Mind Map: CHAPTER 2

1. Handling the Client Request: Form Data

1.1. The Role of Form Data

1.1.1. Example URL at online travel agent – http://host/path?user=Marty+Hall&origin=bwi&dest=lax – Names come from HTML author; values from end user

1.1.2. Handling Client Request form data: passed through html form. An html form is nothing but various controls are inherited to develop an application.

1.2. GET & POST

1.2.1. GET requests include all required data in the URL. not secured because data is exposed in URL bar. can be bookmarked.

1.2.2. POST requests supply additional data from the client (browser) to the server in the message body. secured because data is not exposed in URL bar. cannot be bookmarked.


1.3.1. used to extend the capabilities of servers that host applications accessed by means of a request-response programming model.

1.4. Creating Form Data:HTML Forms

1.4.1. - Use <form>...</form> tag.

1.4.2. Every form must call a servlet by using the following: <form name="name of the form" action="either absolute or relative address" method="get or post"> ......... ......... </form>

1.5. Reading Form Data In Servlets

1.5.1. request.getParameter("name") Returns URL-decoded value of first occurrence of name in query string

1.5.2. request.getParameterValues("name") Returns an array of the URL-decoded values of all occurrences of name in query string

1.6. doGet

1.6.1. That is what the Web server will call when the browser sends a GET request for your servlet. Example public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{ ……. }

1.6.2. HttpServletRequest req HttpServletResponse res throws ServletException, IOException HttpServletRequest req An object representing the request made by the browser. throws ServletException, IOException Servlets have their own exceptions, which they may throw. In addition, it’s possible to cause an IOException to be thrown in writing HTML to the response object. HttpServletResponse res An object representing the response the servlet is building to send back to the browser.

2. Handling the Client Request: HTTP Request Headers

2.1. Client HTTP Request

2.1.1. When a browser requests for a web page, it sends lot of information to the web server which cannot be read directly because this information travel as a part of header of HTTP request.

2.2. Reading Request Headers from Servlets

2.2.1. Call the getHeader method of HttpServletRequest, which returns a String if the specified header was supplied on this request, null otherwise.

2.3. Important Header Information

2.3.1. Accept - This header specifies the MIME types that the browser or other clients can handle. Values of image/png or image/jpeg are the two most common possibilities.

2.3.2. Accept-Charset - This header specifies the character sets the browser can use to display the information. For example ISO-8859-1.

2.3.3. Accept-Encoding This header specifies the types of encodings that the browser knows how to handle. Values of gzip or compress are the two most common possibilities.

2.3.4. Accept-Language - This header specifies the client's preferred languages in case the servlet can produce results in more than one language. For example en, en-us, ru, etc

2.3.5. Authorization -This header is used by clients to identify themselves when accessing password-protected Web pages.

2.3.6. Connection - This header indicates whether the client can handle persistent HTTP connections. Persistent connections permit the client or other browser to retrieve multiple files with a single request. A value of Keep-Alive means that persistent connections should be used.

2.4. Methods to read HTTP Header

2.4.1. Cookie[] getCookies()

2.4.2. Enumeration getAttributeNames()

2.4.3. Enumeration getHeaderNames()

2.4.4. Enumeration getParameterNames()

2.4.5. HttpSession getSession()

2.4.6. HttpSession getSession(boolean create)

2.4.7. Locale getLocale()

3. Generating the Server Response: HTTP Status Codes

3.1. The format of the HTTP response

3.1.1. An initial status line + CRLF ( Carriage Return + Line Feed i.e. New Line )

3.1.2. Zero or more header lines + CRLF

3.1.3. A blank line, i.e., a CRLF

3.1.4. An optional message body like file, query data or query output.

3.2. Why Status code is important?

3.2.1. Servlets can perform a variety of important tasks by manipulating the status line and the response headers.

3.3. Status Codes Categories

3.3.1. 100-199 Codes in the 100s are informational, indicating that the client should respond with some other action.

3.3.2. 200-299 Values in the 200s signify that the request was successful.

3.3.3. 300-399 Values in the 300s are used for files that have moved and usually include a Location header indicating the new address.

3.3.4. 400-499 Values in the 400s indicate an error by the client.

3.3.5. 500-599 Codes in the 500s signify an error by the server.

3.4. Setting a Status Code

3.4.1. A servlet can use setStatus() to set a response status code: public void HttpServletResponse.setStatus(int sc) public void HttpServletResponse.setStatus(int sc, String sm)