Basic Servlet Structure

By: aathishankaran Emailed: 1697 times Printed: 2196 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 java.io (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.

ServletTemplate.java

import java.io.*;
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

Comments(1)


1. View Comment

fantastic
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: `java.net.BindException: 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 )
Sending Email using JSP
JSP Example to connect to MS SQL database and retrieve records
JSP Program for display Date
What are the different scopes in JSP?
JSP Alert Example
JSP CheckBox Example
Form processing in JSP
Sessions in JSP
Basic Servlet Structure
The Advantages of JSP
JSP Tags for SQL to connect to a database
Writing your first JSP page
The Request Object in JSP
Syntax For JSP Declaratives
Embedding java codes in jsp sciptlets
Most Emailed Articles (in JSP)
Scriptlets and Expressions in JSP
The JSP Program running first Time.
What is JSP?
Scriptlets
Tag Libraries in JSP
Embedding java codes in jsp sciptlets
Sessions in JSP
NetworkClient
Simple HTML-Building Utilities
Text Object
Server-side plug-Ins
Comparison operators in JSP
The Response Object in JSP
Cookies using JSP or Java Bean
Click to Activate and Use this control