Posts Tagged ‘Java Servlets’

Configuring and Compiling Servlet

डिसेंबर 15, 2008

Here are some steps to import javax.Servlet packages in .java file.
First you need to download and install Apache tomcat web server from http://apache.multidist.com

1. To import javax.servlet.* package,

Right click My Computer -> Click Properties

Go to “Advanced” tab, Click On “Environment Variables” Button

In the “System Variables” section append the “PATH” variable as

C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar)

2. Create a java class extending GenericServlet

3. Add service method into it, which takes two arguements ServletRequest and ServletResponse

4. Handle the request and send appropriate response to the client using the service() method

5. save and compile the .java file

6. Copy and paste the .class file generated into “C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\examples\WEB-INF\classes” directory
(I’m using Tomcat 6.0 here, directory name may vary according to apache server version)

7. edit “web.xml” file located in “C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\examples\WEB-INF” directory

8. add appropriate <servet> and <servlet-mapping> tag in “web.xml” file
e.g. Here name of servlet is myServlet

<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>myServlet</servlet-class>

</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>

</servlet-mapping>

9. execute the servlet
e.g.
http://127.0.0.1:8080/examples/myServlet

10. All Done!!

NIIT Malad Center Rocks!!