Handling HTTP GET Requests in Java Servlets

Earlier, we’ve introduced you to Java Servlets — what they are, life cycle, advantages. Let’s move ahead with Java Servlets and get some simple code to handle the HTTP GET Request to show you how to write a servlet.

Servlets can be used for handling both the GET Requests and the POST Requests. However in this post, let’s write the code for handling the GET Request. The HttpServlet class is used for handling HTTP GET Requests as it has some specialized methods that can efficiently handle the HTTP requests. These methods are;

  • doGet()
  • doPost()
  • doPut()
  • doDelete()
  • doOptions()
  • doTrace()
  • doHead()

An individual developing servlets for handling HTTP Requests needs to override one of these methods in order to process the request and generate a response. The servlet is invoked dynamically when an end-user submits a form.
Continue reading

Java Servlets, an introduction

We’ve been talking a lot about Linux lately. Let’s break out a bit from that and talk about another topic — the Java Servlet Technology. We’ll try to cover some basic aspects of Java Servlet Technology and may be a little more.

To give you a basic idea, Servlets are the server side components that are the Java counterpart to dynamic web content technologies like CGI, PHP and ASP.NET.

A lot of techniques have been developed to overcome the problems associated with Common Gateway Interface (CGI) and Servlets lie among them. Now, if you’re unsure of the problems associated with CGI then you need to move back to the early days of the Web. In the early days of Web, a server could construct a page dynamically but a separate process was required to handle each request, separate processes were required to read the data from the HTTP request and to write the data to the HTTP response, separate processes were required to communicate with the database. All these activities required a lot of server resources and hence, resulted in the development of programs that were not able to deliver the expected performance.

Continue reading