So here is my first summery on the Head First Servlet & JSP. It is a very nice book in fact and much easier to memorize as the book instruction explained. But it is not easy to find the particular topics and specific details because they are distributed through out various chapters. I guess the authors have decided that way to make relevance with other chapters or for the repetition purpose.

So I started write down on paper what I think the most important keywords are and then decided to write a blog entry so I can search the content whenever I need to look up.

The first Three chapters are basically some introduction on how HTTP and Web is working. I believe they are basic and not worth to include here.

WARNING: This cram sheets are only for the quick review of what you already should know. You should first buy the book and read it if you do not have enough experience or knowledge.

Chapter 1 ~ Chapter 3 are basic concept.

(*) Container

  • Communication Support
  • Lifecycle management
  • Multithreading support
  • JSP Support

Servlet service() method will call doGet() / doPost()

Deployment Descriptor (DD) — XML

1
2
3
4
5
6
7
8
9
10
<web-app>
<servlet>
	<servlet-name>Servlet Name</servlet-name>
	<servlet-class>com.test.ServletClass</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>Servlet Name</servlet-name>
	<url-pattern>/test/*.do</url-pattern>
</servlet-mapping>
</web-app>

Chapter 4 request and response

Lifecycle of Servlet loaded by Container
Load class ==> Instantiate servlet(constructor runs) ==> init() ==> service() ==> destrory()

  • init() can be overridden
  • service() should not be overridden , but override doGet() or doPost()
  • do not try to access ServletConfig or ServletContext from the constructor since they are available only after init() call

Within Init(), below objects are loaded
ServletConfig : constant , defined in DD , they are per each servlet
ServletContext : constant , defined in DD , they are for the whole Application

HTTP Methods

  • GET: Asks to get the resource at the requested URL (idempotent : doing the same thing again w/o side effect)
  • POST : just like GET with extra info sent with request (Only POST is non-idempotent)
  • HEAD: Asks to return only Header part of the response (idempotent)
  • TRACE : asks to loopback of the request message
  • PUT: asks to put the body (idempotent)
  • DELETE : asks to delete
  • OPTIONS: asks for a list of the HTTP methods the server can support
  • CONNECT : says to connect for the purposes of tunneling

If method is not defined in the <form>element in HTML, the default method is GET. If you want POST, you will need to explicitly define ==> method=”POST”

Some Important methods of Request

String client = request.getHeader(“User-Agent”);
Cookie[] cookies = request.getCookies(); 
// note that there is no getCookie(“Cookie_name”)
HttpSession session = request.getSession();
String theMethod = request.getMethod();
InputStream input = request.getInputStream(); //or request.getReader();

getInputStream(): input stream from the request for the case when large input data — binary(getInputStream) or text(getReader) . This returns only the body of the request.

request.getRequestURL(); ==> http://localhost:8080/BeerV1/BeerServlet.do/extra
request.getRequestURI(); ==> /BeerV1/BeerServlet.do/extra
request.getContextPath(); ==> /BeerV1
request.getServletPath(); ==> /BeerServlet.do
request.getPathInfo(); ==> /extra

HttpServletResponse :
Set content type : response.setContentType("application/jar");
Text output: PrintWriter out = response.getWriter() ; out.println ("test123");
Binary output example :

1
2
3
4
5
6
7
8
ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream("/bookCode.jar"); 
OutputStream os = response.getOutputStream(); 
int read=0;
byte[] bytes = new byte[1024];
while ((read = is.read(bytes))!= -1) {
	os.write(bytes,0,read); 
}

Only Cookie and Header methods have add methods.
response.setHeader() add or replace header
response.addHeader() add header

response.setIntHeader() or response.addIntHeader()
response.setDateHeader() or response.addDateHeader()

Client Redirect: response.sendRedirect("http://otherwebsite"); //note that arg is string, not URL object.

Server side Forward :

RequestDispatcher view = request.getRequestDispatcher("test.jsp");
view.forward(request, response);

That’s all for today.


(*) Next: [SCWCD] Cram Sheet 2 – attribute and listeners

Share and Enjoy:
  • DZone
  • Twitter
  • Technorati
  • Reddit
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Diigo


One Response to “[SCWCD] Cram Sheet 1 – request and response”

  1.   Awesome Says:

    thanks!!!!!!!!!!

Leave a Reply