<< Chapter < Page Chapter >> Page >
(This may not make you popular with the server administrator because it could cause your single-threaded servlet to become a bottleneck.)

The third stage

The third and last stage of a servlet's life cycle is removal. When a server removes a servlet, it runs the servlet's destroy method. This method is run only once. The server will not run it again until after it reloads and reinitializes the servlet. When the destroy method runs, other threads might be running service requests. If it is necessary to access shared resources while doing cleanup, that access should be synchronized.

The javax.servlet.Servlet interface

Servlets must implement the javax.servlet.Servlet interface. For writing servlets that run under control of servers that use the HTTP protocol, the most common way to write servlets is to extend the javax.servlet.http.HttpServlet class which is a way to indirectly implement the Servlet interface.

The HttpServlet class implements the Servlet interface by extending the GenericServlet base class, and provides a framework forhandling the HTTP protocol.

Four methods

For servlets that extend the HttpServlet class, the following four methods may be overridden to cause your servlet to interact with the client.

  • doGet - for handling GET, conditional GET and HEAD requests
  • doPost - for handling POST requests
  • doPut - for handling PUT requests
  • doDelete - for handling DELETE requests

You may need to do some outside research on the HTTP protocol to learn about the nature of each type of client request listed above. You may also want to go back and review Java4350: Form Processing with JSP for information regarding the differences between the HTML GET and POST methods.

By default, if called but not overridden, these methods return a BAD_REQUEST (400) error.

The servlet discussed earlier used the doGet method to handle GET requests.

Differences between doGet and doPost

I will describe a couple of differences between the two methods that you may find useful. Assume there is a servlet named Java4550a on a localhost web server. You can execute that servlet by typing the followingaddress into the address field of the browser and pressing the Enter key :

http://localhost:8080/Java4550a

First consider the contents of the browser's address window. When an HTML page calls the doGet method of a servlet named Java4550a , you will often see something like the following in the address window:

http://localhost:8080/Java4550a?firstName=Dick...

As you can see, some information has been appended onto the end of the address with the ? character being used to separate the two.

However, if the HTML page calls the doPost method of the same servlet, this is what you should see:

http://localhost:8080/Java4550a

(Note that some browsers don't display the http:// portion of the address.)

In other words, with the doPost method, no visible information is appended onto the actual address as is the case with the doGet method.

It is probably also safe to say that the doGet method is the default. By this, I mean that if you enter address given above into the browser's address field and press the Enter key, the doGet method (and not the doPost method) belonging to the servlet named Java4550a will be called.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask