Basic Servlet Structure

By: aathishankaran Emailed: 1728 times Printed: 2331 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Listing outlines of a basic servlet that handles GET requests. GET requests, for those unfamiliar with HTTP, are the usual type of browser requests for Web pages. A browser generates this request when the user types a URL on the address line, follows a link from a Web page, or submits an HTML form that does not specify a METHOD. Servlets can also very easily handle POST requests, which are generated when someone submits an HTML form that specifies

METHOD="POST". For details on using HTML forms. To be a servlet, a class should extend HttpServlet and override doGet or doPost, depending on whether the data is being sent by GET or by POST. If you want the same servlet to handle both GET and POST and to take the same action for each, you can simply have doGet call doPost, or vice versa.

Both of these methods take two arguments: an HttpServletRequest and an HttpServletResponse. The HttpServletRequest has methods by which you can find out about incoming information such as form data, HTTP request headers, and the client’s hostname. The HttpServletResponse lets you specify outgoing information such as HTTP status codes (200, 404, etc.), response headers (Content-Type, Set-Cookie, etc.), and, most importantly, lets you obtain a PrintWriter used to send the document content back to the client. For simple servlets, most of the effort is spent in println statements that generate the desired page. Form data, HTTP request headers, HTTP responses, and cookies will all be discussed in detail in the following chapters. Since doGet and doPost throw two exceptions, you are required to include them in the declaration. Finally, you have to import classes in (for PrintWriter, etc.), javax.servlet (for HttpServlet, etc.), and javax.servlet.http (for HttpServletRequest and HttpServlet- Response).  

Strictly speaking, HttpServlet is not the only starting point for servlets, since servlets could, in principle, extend mail, FTP, or other types of servers. Servlets for these environments would extend a custom class derived from Generic- Servlet, the parent class of HttpServlet. In practice, however, servlets are used almost exclusively for servers that communicate via HTTP (i.e., Web and application servers), and the discussion in this article will be limited to this usage.

import javax.servlet.*;  
import javax.servlet.http.*;  
public class ServletTemplate extends HttpServlet {

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

// Use "request" to read incoming HTTP headers  
// (e.g. cookies) and HTML form data (e.g. data the user  
// entered and submitted).  
// Use "response" to specify the HTTP response status  
// code and headers (e.g. the content type, cookies).

PrintWriter out = response.getWriter();

// Use "out" to send content to browser


JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

make something like this

View Tutorial          By: jyoti maheshwari at 2009-03-27 22:15:36

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by aathishankaran
Web Security Issues
The Web User's Perspective
Server-side plug-Ins
The best way to avoid security vulnerabilities with new server
JavaScript Security
Window Object
Working with Status Bar Messages
Retrieving a Portion of a String
Referencing Windows
Math Object
Frame Object
Document Object
Closing Windows
Built-in Object in Javascript
Textarea Object

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: ` Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
JSP Example to connect to MS SQL database and retrieve records
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: ` Permission denied <null>:80
Automatically Refreshing a JSP
JSP CheckBox Example
JSP Alert Example
JSP Program for display Date
What are the different scopes in JSP?
Example Using Initialization Parameters Connection refused: connect
Comparison operators in JSP
Sending Email using JSP
The Response Object in JSP
Embedding java codes in jsp sciptlets
JSP pages in servlet
Basic Servlet Structure
Most Emailed Articles (in JSP)
Combining Scriptlets with HTML
Form processing in JSP
Protecting your website with a login page
The Servlet Life Cycle
Closing Windows
WebClient: Talking to Web Servers Interactively
Drawbacks of the JSP Model 1 Architecture
Select Object
Working with Status Bar Messages Connection refused: connect
What are the different scopes in JSP?
Calling JSP from a batch file
Embedding a QuickTime movie in a JSP