Thursday, September 11, 2008

How to write, compile and execute JAVA servlets

Servlets are core java programs that can be executed on the webserver upon the request via the url.

Prerequests for running a java servlet:

1. install JRE (Java Runtime Environment)

1.1 Download the JRE installation pack. Click here to download
1.2 Install the pack as per instructions.
1.3 Set the Environment Variable, JRE_HOME to the installed location.

1.3.1 Steps to set the Environment Variable in Windows XP/Vista MyComputer-> Properties -> ADVANCED tab . Click the "Environment Variables" button. Add a new System Variable:
Variable name: JRE_HOME
Variable value: C:\Program Files\Java\jre1.5.0_01\
(or ur insalled location)

1.4 You have done with installing JRE

2. Install apache-tomcat.

2.1 Download a binary distribution of Tomcat from:
2.2 Unpack the binary distribution into a convenient location so that the distribution resides in its own directory (conventionally named "apache-tomcat-[version]").

3. Include the servlet-api.jar to the CLASSPATH environment varialbe.
servlet-api.jar is not contained in JDK. It comes along with the apache-tomcat server. It is located "lib" folder of Tomcat installation direcotry.

eg: set classpath=C:\apache-tomcat-6.0.14\lib\servlet-api.jar;%classpath%

4. Start the ApacheTomcat server(startup.bat inside bin folder).

5. Now your server is ready and running.

Writing a Sample Java Servlet ::

Java servlets are no different from normal java programs. The servlet file is of .java extension. It uses javax.servlet package.

A sample and simple java servlet example :

import javax.servlet.*;
import javax.servlet.http.*;

public class myExample extends HttpServlet {

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

PrintWriter out = response.getWriter();
out.println("< HTML > \n" +
"< HEAD > < TITLE > Servlet Example from </TITLE > < /HEAD >\n" +
"< BODY >\n" );

out.println( "<H1> Servlet Example < /H1 >\n" + " A simple Servlet..
<br/><br/><br/>Thought for the day: You never get a second chance to make the first impression.
</BODY></HTML >");


public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);


How to run the servlet :

step 1: Compile the file using javac compiler just like anyother java file is compiled.

Step 2: Move the .class file to $java_installation_directory\webapps\ROOT\WEB-INF\classes\
If any directory is not found in the path, create it.

Step 3: Open the web.xml file in
if it does not exist, create it. We use this file to map the servlet. It is must

Step 4: its content should look like this:

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

<web-app xmlns=""

 <display-name >Welcome to Tomcat </display-name >
<description >
     Welcome to Tomcat
</description >

<servlet >
<servlet-name >HelloWWW </servlet-name >
<servlet-class >HelloWWW </servlet-class >
</servlet >
<servlet-mapping >
<servlet-name >HelloWWW </servlet-name >
<url-pattern >/servlet/HelloWWW </url-pattern >
</servlet-mapping >

</web-app >

Step 5: Run the servlet using the browser. Type http://localhost:8080/servlet/myExample in the adress bar.

Step 6: Enjooyyy...>!!!!

Note: You can use servlets to receive form-data as POST and GET. Do a google search on it and you will find it. Feeling too lazy to write that too here... check back late.. i might have posted it.

No comments: