HOME
J2ee tutorials
Java introduction
Java basic
Java installation
Java Packages Learn
Applets Java Threads Java Gui component

Java Event Handaling Java Streams and Files
 Java Swings Java JDBC
 Java Network Programming Java RMI
 Java Servlets Java javabeans
 EJB Struts

|
Java servlets
Servlet are programs that run on a web server and build web pages on the fly. Building web pages on the fly is useful for number of reasons.
The webpage is based on data submitted by the user. for example the results pages from search engine are generated this way, and programs that process orders for e-commerce sites do this as well.
The data changes frequently. For example, a weather –report or news headlines page might build the [page dynamically, perhaps returning a previously built if it I still up to date.
The web page uses the information from corporate databases or other such sources. For example you would use this for making a web page at an on-line that
lists current prices and number of items in stock
A server can be thought as a server side applet .servlet are loaded and executed by a web server in the same manner as the applets are loaded and executed by a web browser. Servlet access requests from the client, perform some task, and return results.
The following are the basic steps of using servlets
The client makes a request
The web server forwards the request to the servlet after receiving it from the client
Some kind of process is done by the servlet after receiving the request
A response is returned back to the web server from the servlet
The web server will forward the response to the client
WhY use servlet
Servlet have various benefits over other technologies that are used for writing server side program. On such technology is CGI scripts. Servlets have less start up cost. They run continuously, and they are platform independent.
Requirement for writing servlets
You have install java servlet development kit (JSDK) before creating any servlet. This kit includes the javaservlet and sun.servlet packages, sample servlets for Windows 95 and NT and servletRunner to test servlets
You have set the environment variable CLASSPATH , to the requires location so that your servlets can find the servlets packages. The following environment variable need to be set for windows system, with the JSDK installed in the C:\j2sdk
Classpath: c:\j2sdk\j2sdk.jar;
Path: c:\j2sdk\bin;
The JSDK can be downloaded form the javasoft home page(http://java.sun.com). The java servlet development kit is included as apart of JDK 1.2
To run java servlet u need a web server that supports the servlet API we will be using J2EE to run servlets.
The following steps are required to run any servlet
If your web server is not running, strat it
Configures the web server to install the servlet
Run your web browser
Direct the browser to the particular url that refers to the new servlet
Another thing to remember while creating servlets is that you need to create some kind of client application that invokes the servlet. The client application can be created some kind of client application can be provided in form of HTML or applets.
The basic steps requires for writing servlet are as follows
-
Extend GenricServlet class
-
Override the service() methods
-
Get the output stream from the servletResponse object and create aprintstream object with it
-
Finally print the request string to the printstream
Let us write a simple servlet program
|
Public java.io*;
Import javax.servlet.*;
Public class simplservletdemo extends GenricServlet
{
public String getServletInfo()
{
return “Localhost”;
}
public void service(ServiceRequest req,ServletResponse res)
{
try
{
PrintStream out=new PrintStream(res.getOutputStream());
Out.println(“Hello” + req.getRemoteHost());
Out.println9”this is the environment where the servlet is running…….”);
Out.Println(“the name of the server running is “+getServletInfo());
}
catch(Exception e)
{}
}
} |
Save the above file in the folder serveltexample. Compile in using javac compiler as given below
c:\servletexample> javac simplesevletdemo.java
now letus write thre HTML which will invoke the servlet. The HTML is given below
|
<html>
<head>
Servlet example
</head>
<form method=”GET” action=”simpleservletdemo”>
<input name=”test” type=submit value=”test”.
</input>
</form>
</html> |
Save this file as servlet.html in the folder servletexample.the action attributes of the form tag takes the alias name of the servlet. This is the alias name that should be provided while deploying the servlet using J2EE,
Handling HTTP Rrequests
We have written a program by extending the genericSerlet class. Because most servlet are implemented on a web servers that use the HTTP protocol to interect with clients,the most common way to develop servlets is by extending the javax.servlet.http.HttpServlet
Class.
The HttpServlet class extends the GenricServlet base class and provides a framework for handling the HTTP protocol. Because it is an abstract class, servlet writers must subclass it and override at least one method. The methods normally overridden are
-
doGet,if HTTP GET requests are supported. Overriding the doGet methods automatically also provides support for the HEAD and conditional GET operation
-
doPost, if HTTP POST requests are supported
-
doPut,if HTTP PUT request at=re supported
-
doDelete,if HTTP DELETE requests are supported
Lets us write sample program
|
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
Public class GetServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
{
try
{
ServletContext sc=getServeletContext();
PrintStream out=new PrintStream(res.getOutputStream());
Res.setStatus(HttpservletResponse.SC_OK);
Res.setContentType(“text/html”);
Out.println(“ simple servlet output,/h1>”);
Out.println(“,p>this output is form getservlet/”);
Out.println(“the name and version of the server running is” +sc.getServerInfo());
Out.println(“”);
Out.close();
}
catch(Exception e)
{}
}
} |
Write the HTML file for above servlet similar to one given in the last program. Remember to use the Get request for the methods attribute of the form tag and the action name should be the same as the alias name provided while deploying the servlet in J2EE
Compile the servlet and then deploy the servlet following the same steps as given before.
|
|
Inform Friend About This Site |