Servlet in java

Posted by Satyapriyanayak under Others category on | Points: 40 | Views : 927
Servlet : - it is technology to create dynamic WebPages. This is a program, which resides in a web server to process request of the user and to provide dynamic response to users.

Storing compilation and deployment of servlet

1.Store the servlet file inside ‘classes’ folder of the context for predefined context (root) the classes folder required to be created inside the WEB-INF folder.

2.For compilation of the servlet files use servlet-api.jar in classpath option of javac command. This jar file contains the package used by the servlet files. This jar file can be found inside installation folder of the tomcat/lib folder.

3.Deployment:- This is a process to create an url for the servlet .The user will use the url to access the servlet .This can be done by providing few tags in web.xml file The tags are as follows:
In web.xml file present side to classes folder


<url-pattern>/ url-pattern </url-pattern>


import javax.servlet.*;
import javax.servlet.http.*;
public class httpserv extends HttpServlet
public void doGet(HttpServletRequest req ,HttpServletResponse res)throws IOException ,ServletException
PrintWriter out = res.getWriter();
out.println("<h1> First servlet</h1>");

How to run this program

First create a folder name Servlet in any drive (here e: drive). Inside that folder create a WEB-INF folder (all letters will be caps mandatory). Copy a web.xml file from (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) folder.Again Inside WEB-INF folder create a folder named classes.In classes folder we will store the Servlet files accoding to there class name ,here the servlet-api.jar (E:\Program Files\Apache Software Foundation\Tomcat 6.0\lib) file in classes folder.

web.xml setting:-

<?xml version="1.0" encoding="ISO-8859-1"?>




Context path setting: - Move to (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) folder and open server.xml file.create a context path (any name) and docBase="E:\Servlet" (this is the folder path where we are storing the (WEB-INF\classes) servlet files)
<Context path="/javaservlet" docBase="E:\Servlet" reloadable="true" debug="0" />

<?xml version='1.0' encoding='utf-8'?>
<Context path="/javaservlet" docBase="E:\Servlet" reloadable="true" debug="0" />


javac -cp servlet.jar (for tomcat 4.0)
javac -cp servlet-api.jar (for tomcat 6.0)

cp means classpath

Running the servlet in web browser

First run the tomcat 6.0

http://localhost:8081/ javaservlet /httpserv

Comments or Responses

Login to post response