Home: www.vipan.com Vipan Singla e-mail: vipan@vipan.com
Coding Java Servlets
includes Java Server Pages


How a Browser Talks to a Web Server

  • The browser user:
    • types a url in the browser's address/location field, or
    • clicks on a link, or
    • clicks on a button.
  • The browser sends a "GET" or "POST" request to the main web server.
    • If the user typed a url and pressed <enter>, it is a GET request.
    • If the user clicked on a link, it is generally a GET request.
    • If the user pressed a button (usually after filling out a form), it can be a GET or a POST request. It will say which one it is in the METHOD=". . ." attribute of the <FORM . . .> tag if you look at the source code of the HTML page you are viewing.

Lifecycle of a Java Servlet

  1. When the Servlet Container (Java server) starts up, it creates one instance of the servlet class.
  2. The Java server calls that instances's "init()" method which is always allowed to complete before the "service()", "doGet()" or "doPost()" method can ever be called.
  3. From then on, whenever a browser requests a servlet, the server calls that instance's "service()" method. If multiple requests are received, the server calls the same "service()" method in a separate thread (unless the java server is maintaining instance pools).
  4. For GET and POST requests on HttpServlets, the "service()" method automatically calls "doGet()" or "doPost()" method WHICH YOU DEFINE IN THE SERVLET's CODE.
  5. If the server ever needs to shut down (or if too many servlets are loaded in memory and some of them are not being used), it calls the servlet instances's "destroy()" method. Any data in memory that needs to be saved can then be written to disk in the "destroy()" method.
  6. The server then forgets that the servlet instance ever existed. The instance is ultimately garbage-collected to free up memory.
  7. When the server restarts (or the next time the servlet is called), the lifecycle starts all over again.

How to Process a Request to a Servlet

  • The Java server automatically:
    1. processes the incoming query string (from GET method) AS WELL AS THE REQUEST BODY (from POST method),
    2. decodes the url-encoded strings and request body, and
    3. stores the resulting name-value pairs in a request object.
    4. calls the servlet's doGet() or doPost() method depending on the incoming request method.
  • Java's HTTP request object has a number of methods to access various parts of the incoming request. Some of the request object's get...() methods are listed above against corresponding CGI environment variables. But, HERE ARE THE METHODS YOU WILL USE MOST OFTEN!

Coding A Typical Servlet

Automatic Session Tracking

Using Persistent Cookies

Storing and Retrieving Intermediate Variables

See also Save User Data to Disk Between Accesses

Coding Java Server Pages

No name=value Pairs in A Request

Accessing Other Servlets or Server Resources

Servlet Chaining

How to Communicate with an Applet

Save User Data to Disk between Accesses

Delegating Actual Work to Regular Java Classes

Getting and Returning Binary Data (Images)

Miscellaneous Information


© 2000 Vipan Singla