In the Previous Chapter we saw how to get a Servlets Initialization parameters. In this chapter, we are going to take a look at how to get the Header information from the Http Request.
So, lets get started!!!
Retrieving HTTP Request Header Information
The Http Request header is the area where all the details of the request are bundled. This is where the browser specifies the file wanted, date, image file support, and a lot more. Let us take a look at a sample Servlet that is going to read through the Http Request Header.
Servlet code:
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Displaying request headers
*
* @author Anand V
*/
public class ReadHttpRequestHeaderServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("< html >");
out.println("< head >");
String title = "Read Request Header Example";
out.println("< title >" + title + " title >");
out.println("< / head >");
out.println("< body >");
out.println("< h3 >" + title + "< / h3 >");
out.println("< table >");
Enumeration e = request.getHeaderNames();
while (e.hasMoreElements())
{
String headerName = (String)e.nextElement();
String headerValue = request.getHeader(headerName);
out.println("< tr >< td bgcolor=\"#CCCCCC\" > " + headerName);
out.println("< / td >< td > " + headerValue + " < / td > < / tr >");
}
out.println("< / table >");
out.println("< / body >");
out.println("< / html >");
}
}
When you deploy this Servlet on your local tomcat server you will see something like below:
Previous Chapter: Chapter 11 - Obtaining a Servlets Initialization Parameters
Next Chapter: Chapter 13 - Web Application Scope
Topics Covered in the Blog - Synopsis
Showing posts with label example servlet. Show all posts
Showing posts with label example servlet. Show all posts
Monday, March 7, 2011
Monday, February 28, 2011
Chapter 3: A Sample Servet
We saw what the purpose of a Servlet Container and a Web server is, in the previous chapter. In this chapter, we are going to look at how a Servlet code would look like.
So, lets get started!!!
Servlet Skeleton
If I ask you, what are the components of a Java class, you’ll happily tell me that, there are first package statements and then imports and then the class declaration. Within the class brackets, we have constructors, instance variables, methods etc. That was easy, wasn’t it?
The same way, every Servlet has a certain set of components that are mandatory for its well-being. (I just got carried away a bit) Or I must say, for its proper functioning.
A Servlets skeleton would look like below:
The above is what a Servlets skeleton would look like. Now let us take a look at some sample code as to how a properly coded Servlet would look like:
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.PrintWriter;
import java.io.IOException;
public class OurFirstServlet extends HttpServlet
{
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("< html >");
out.println("< head >< title >Servlet Example " +
" title > head > ");
out.println("< body >");
out.println("Not Much code, but this is enough for a Servlet.");
out.println(" body >");
out.println(" html >");
}
}
The above is a simple Servlet. It would display an almost blank HTML page that contains the message we put in “Not Much code, but this is enough for a Servlet.”
Note: A Servlet is not a simple java class, that you can run using a main() method. You have deploy this Servlet on a web server in order to view the output. Lets not get too ahead of ourselves here. We’ll be looking at all that later in detail. For now, this is how a Servlet would look like and that wraps up our current chapter.
Previous Chapter: Web Servers & Servlet Containers
Next Chapter: A sample JSP
So, lets get started!!!
Servlet Skeleton
If I ask you, what are the components of a Java class, you’ll happily tell me that, there are first package statements and then imports and then the class declaration. Within the class brackets, we have constructors, instance variables, methods etc. That was easy, wasn’t it?
The same way, every Servlet has a certain set of components that are mandatory for its well-being. (I just got carried away a bit) Or I must say, for its proper functioning.
A Servlets skeleton would look like below:
/*
* servlet name
*
* servlet description
* All other stuff that is part of a Standard Class comment section
*/
//package declarations
//import statements
public class ServletName extends HttpServlet {
// Instance Variables
/**
* Code to Initialize the Servlet
*/
public void init() throws ServletException
{
// Servlet Initialization Code goes here
}
/**
* The Service Method
* Gets invoked for every request submitted to the Servlet
* This method is optional. We mostly use doGet, doPost Methods
*/
protected void service(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException
{
// Code for the Service Method goes here
}
/**
* Process a GET request
*
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
// Code for the doGet() method goes here
}
/**
* Process a POST request
*
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
// Code for the doPost() method goes here
}
/**
* Process a PUT request
*
*/
protected void doPut(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException
{
//Code for the doPut() method goes here
}
/**
* You can have any number of methods for your processing
* here. There is no limit as to the number of methods or
* any restrictions on what you can name them.
* Since this is all java code, you need to keep them
* syntactically correct as per Java Coding Standards.
*/
/**
* Clean-Up
*/
public void destroy()
{
// clean up activities before the Servlet is put to death
}
}
The above is what a Servlets skeleton would look like. Now let us take a look at some sample code as to how a properly coded Servlet would look like:
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.PrintWriter;
import java.io.IOException;
public class OurFirstServlet extends HttpServlet
{
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("< html >");
out.println("< head >< title >Servlet Example " +
" title > head > ");
out.println("< body >");
out.println("Not Much code, but this is enough for a Servlet.");
out.println(" body >");
out.println(" html >");
}
}
The above is a simple Servlet. It would display an almost blank HTML page that contains the message we put in “Not Much code, but this is enough for a Servlet.”
Note: A Servlet is not a simple java class, that you can run using a main() method. You have deploy this Servlet on a web server in order to view the output. Lets not get too ahead of ourselves here. We’ll be looking at all that later in detail. For now, this is how a Servlet would look like and that wraps up our current chapter.
Previous Chapter: Web Servers & Servlet Containers
Next Chapter: A sample JSP
Labels:
example servlet,
first servlet,
getting scwcd certified,
sample servlet,
scwcd,
scwcd certification,
servlet,
servlet code,
servlet container,
servlet skeleton,
servlets
| Reactions: |
Subscribe to:
Posts (Atom)
© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

