Handling HTTP GET Requests in Java Servlets

Servlets can be used for handling both the GET Requests and the POST Requests. 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.

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.

Let’s look at a sample HTML FORM

<form name="F1" action=/servlet/ColServlet>

Select the color:
<select name = "col" size = "3">
    <option value = "blue">Blue</option>
    <option value = "orange">Orange</option>
</select>

<input type = "submit" value = "Submit">
</form>

Here’s the code for ColServlet.java that overrides the doGet() method to retrieve data from the HTTP Request and it then generates a response as well.

// import the java packages that are needed for the servlet to work
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// defining a class
public class ColServlet extends HttpServlet
{

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException  

// request is an object of type HttpServletRequest and it's used to obtain information
// response is an object of type HttpServletResponse and it's used to generate a response
// throws is used to specify the exceptions than a method can throw
 
  {

    String colname =  request.getParameter("col"); 
   
   // getParameter() method is used to retrieve the selection made by the user
    response.setContentType("text/html");
    PrintWriter info = response.getWriter();
    info.println("The color is: ");
    info.println(col);
    info.close();
    }
}

In order to make this code work, compile the servlet and open the HTML file and hit the “submit” button after selecting a color. Once you press the submit button, the browser will display a response that is generate dynamically by invoking the servlet. Well, this was a very simple servlet and it’s always good to start with basic stuff. However, there’s no end to what you can do with Java Servlets as you can make complex servlets to do several complex tasks.


Don't like it? There are lots of published articles, pick a random one.

oCricket

Nikhil Bansal posted this article on Wed, Jul 2nd, 2008 at 5:35 pm
Categorized under Asides, Java and has the following tags

Prev Article: The Open Source Paradigm

Next Article: How about an Open Sourced office?


Possibly Related Articles

Archives: Visit the Archives for more articles.

Comments Post Yours

There are one response so far. You can follow any responses to this entry through the RSS feed. You can leave a response, or trackback from your own site.

  1. I’m not sure if compiling the servlet is enough to get going !
    Don’t you need to run it on a servlet container?

Post yours

Sidenotes

Quick notes, scribbles, somehow related to this website and to what I do. Or perhaps I'm just plain lazy to make them into a full article.

Adobe releases Flash Player 10 (Mac, Win & Linux)

Adobe have released the shipping version of Flash Player 10 for Mac, Windows and Linux. More about Flash Player 10 * About Flash Player 10 * Get Flash Player 10 (Official Release version 10.0.12.36) * Download Debug and Standalone ...15th Oct, 2008

What a bad team can do to a Startup?

Flash/Flex developer, Orian Marx, started his Startup with much zeal and enthusiasm but had to give-in to failures because of none other but the team - the founders involved. His article outlined the story so ...13th Oct, 2008

Introduction to Adobe AIR (NetTuts)

Nettuts have a nice Introduction to Adobe AIR which focus on using HTML/JS to build an AIR Application. It covers -- Adobe AIR Architecture, How to install AIR, Get the development SDK, Configurations, gets on to ...2nd Oct, 2008

Decide Team Type from Books

What do you think of the "Team-Type" if they got these books recently? What "Team-Type" do these books suggest? ( surveys) 29th Sep, 2008

What do investors look for in a startup?

A great video Interview with Venture Hacker Naval Ravikant answering the age-old question: What do you look for in a startup? Investors look for two things that are paramount above all: # Great team: It's obvious. It's a ...27th Sep, 2008

View the Sidenotes Archive

Play the Penguin Game

Recommended

  • My Special Job My Special Job is a place where you can look for your weird necessities, strangely superb employees, when your need are more of those hackers, geeks, and ultimate rockstars in the Internet Technology.
  • ActionScript 3.0 Reference Flash/Flex ActionScript 3.0 Reference.
  • Downloads All downloads, Free and Open Source.
  • Ode to Apple Dedicated to Apple - Mac, iPhone, iPod, iTunes, Quicktime, Apple TV and all the awesome softwares for the Apple Mac.
  • oCricket oCricket is about Cricket and people enthusiastic about it.
  • Not Safe for Work Ever clicked a link and felt embarrassed with the content in front of your co-workers? Ever caught unaware because the funny link your friend sent was a little beyond funny? Let’s minimize that with NSWF.
  • AS 2.0 Reference Reference for ActionScript 2.0 Programming Language used in Flash. Primarily stashed here for my own personal reference.

Download free Brajeshwar Wordpress Theme

Brajeshwar

Brajeshwar I firmly believe in keeping things simple, easy for users and I envison pushing the technical envelop time and again for the betterment of viable commercial and practical applications. More about me.

Brajeshwar Personal Identity Portal powered by VeriSign Labs

Brajeshwar's affinity with Adobe

My Photos

More photos on Flickr

Member of 9rules Network

Since its inception on 11th June, 2001, "Brajeshwar" has 865 Articles and 6,015 comments, contained within 20 categories and 1,234 tags.