Structure of a Servlet
Java Servlets are server side Java
programs that require either a Web Server or an Application Server for
execution. Examples for Web Servers include Apache’s Tomcat Server and
Macromedia’s JRun. Web Servers include IBM’s Weblogic and BEA’s Websphere
server. Examples for other Server programs include Java Server Pages (JSPs) and
Enterprise Java Beans (EJBs).
Here's the outline of a basic servlet
that handles GET requests. GET requests are requests made by browsers when the
user types in a URL on the address line, follows a link from a Web page, or
makes an HTML form that does not specify a METHOD. Servlets can also very
easily handle POST requests, which are generated when someone creates an HTML
form that specifies METHOD="POST".
import
java.io.*;
import
javax.servlet.*;
import
javax.servlet.http.*;
public class <<ServletName>>
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 line and headers
// (e.g. specifying the content type,
setting cookies).
PrintWriter out = response.getWriter();
// Use "out"
to send content to browser
}
}
|
To be a servlet, a class should extend
HttpServlet and override doGet or doPost (or both), depending on whether the
data is being sent by GET or by POST. These methods take two arguments: an
HttpServletRequest and an HttpServletResponse. The doGet and doPost throw two exceptions, so you
are required to include them in the declaration. You have to import classes in
java.io (for PrintWriter, etc.), javax.servlet (for HttpServlet, etc.), and
javax.servlet.http (for HttpServletRequest and HttpServletResponse). The doGet
and doPost are called by the service method, and sometimes you may want to
override service directly, e.g. for a servlet that handles both GET and POST
request.
Configuring
Apache Tomcat to run Servlets
1.
Download the Apache Tomcat Software
Go to http://tomcat.apache.org/download-60.cgi
and download and unpack the zip file for the current release build of Tomcat 6
2.
Set the JAVA_HOME Variable
8
Next, you must set the JAVA_HOME environment
variable to tell Tomcat where to find Java. Failing to properly set this
variable prevents Tomcat from compiling JSP pages. This variable should list
the base JDK installation directory, not the bin subdirectory.
8
On Windows XP, you could also go to the Start
menu, select Control Panel, choose System, click on the Advanced tab, press the
Environment Variables button at the bottom, and enter the JAVA_HOME variable
and value directly.
3.
Turn on Servlet Reloading
To turn on servlet reloading, edit
Edit install_dir/conf/context.xml and change
<Context> to
<Context reloadable="true"
privileged="true">
4.
Enable the Invoker Servlet
- The invoker servlet lets
you run servlets without first making changes to your Web application's
deployment descriptor (i.e., the WEB-INF/web.xml file). Instead,
you just drop your servlet into WEB-INF/classes and use the URL http://host/servlet/ServletName
- To enable the invoker
servlet, uncomment the following servlet and servlet-mapping elements in install_dir/conf/web.xml.
Do not confuse this Apache Tomcat-specific web.xml file with the
standard one that goes in the WEB-INF directory of each Web application.
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
...</servlet>
...<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
5. Turn
on Directory Listings (Optional)
To make this change, edit install_dir/conf/web.xml
and change the init-param value of listings for the default servlet, as below.
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
6. Set the CATALINA_HOME Variable (Optional)
Set the CATALINA_HOME environment
variable to refer to the top-level directory of the Apache Tomcat installation
(e.g., C:\apache-tomcat-6.0.28). This variable identifies the Tomcat
installation directory to the server.
7. Running
the Tomcat Server
For Running Tomcat, click on install_dir/bin/startup.bat.
Or
8 Open a command prompt.
8 Move the directory tomcat install-dir/bin
8 Type startup
Next, enter the URL http://localhost:8080/
in your browser and make sure you get the Tomcat welcome page
Compile and Execute your Servlet
Step 1 – Compile your servlet program
Neither the javax.servlet.* nor the
javax.servlet.http.* is part of the standard JDK. It has to be exclusively
added in the CLASSPATH.
>Set classpath=
C:\apache-tomcat-6.0.20\lib\servlet-api.jar
>javac servletfilename.java
Step 2 – Copy the servlet class to the classes folder
Copy the
servlet class file to the C:\apache-tomcat-6.0.20\webapps\ROOT\WEB-INF\classes
folder in order to run the servlet that we created. If the classes folder is
not present then create anew one.
Step 3: Run Tomcat server and then execute your Servlet
Start the tomcat server
Open a browser and type the URL as
http://localhost:8080/sevlet/serveltname.
Example:
Program:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends
HttpServlet
{
public
void doGet(HttpServletRequest request,
HttpServletResponse
response)throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello</TITLE></HEAD>");
out.println("<BODY
BGCOLOR=\"#FDF5E6\">");
out.println("<H1>Welcome
to MSPVL</H1>");
out.println("</BODY>");
out.println("</HTML>");
}
}
Output:
Posted in : Servlet Posted on : October 30, 2010 at 5:52 PM Comments : [ 0 ]
Configuring
And Running Servlet Program into tomcat 6
Follow the steps to compile and run
servlet application
- At first you need to install java ( jdk1.5 or above
version ) .
- Set the JAVA_HOME as- go to the Start ->Control
Panel ->Performance And Maintenance ->System ->Advance ->
Environment Variable -> New -> Then write the variable name
"JAVA_HOME" and in Variable Value value write the full path of
java install directory.
- Then download the tomcat 6 and then un-zip the zip the tomcat into
the C:\ directory.
- Then go to the apache-tomcat-6.0.29\webapps\ROOT\WEB-INF
directory make a new folder name it "classes". Remember that the
name of classes directory must be written in small latter.
- Then write the following code in note pad or other
editor you prefer and save it as TestServlet.java and save it to the
apache-tomcat-6.0.29\webapps\ROOT\WEB-INF\classes directory-
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest
request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out =
response.getWriter();
out.println("");
out.println("");
out.println("<h1>This is my Fisrt Servlet</h1>");
out.println("");
out.println("");
}
}
- Then for compilation open your command prompt and by
command prompt go to the apache-tomcat-6.0.29\webapps\ROOT\WEB-INF\classes
and type javac TestServlet.java -classpath
"C:\apache-tomcat-6.0.29\lib\servlet-api.jar" and then press
enter.
- Now configure your servlet. To configure follow the
steps go to the C:\apache-tomcat-6.0.29\webapps\ROOT\WEB-INF and open web.xml file in edit mode
- write the following block of code within
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
Write your code here .......................
</web-app>
Then write
the fillowing mapping code
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>TestServlet</servlet-class> </servlet> <!--
servlet mapping --> <servlet-mapping>
<servlet-name>Test</servlet-name> <url-pattern>/test</url-pattern>
</servlet-mapping> Here Test within
<servlet-name>Test</servlet-name> is name of your servlet.
TestServlet is your servlet class and /test is your url pattern or your servlet
name by which you call this servlet on browser
- Then Start the tomcat, go to the
apache-tomcat-6.0.29\bin and double click on the startup. if a window
appears like this the your tomcat have been started. To check whether you
tomcat started or not open your web browser and type URL http://localhost:8080 and press enter then a windows appears like this
- To run your servlet write the following URL in your
browser http://localhost:8080/test and press enter then the output appears like
this