Newbies guide to Servlets.

18 Mar 2006

I better kiss ASP.NET and bang the heck out of servlets. Agreed, servlets are easy to program to a certain extent, but when it is for presentation oriented applications they are terrible to work with. They just mess up your code so much that you begin to hate it yourself.
Anyway, for those who doesnt know servlets - heres a sample servlet and steps.

1. Download Apache Tomcat Server(the best for servlets) and install it.

2. In the installation folder you can see " webapps " folder which is the root folder for your server. You copy your applications into this folder.

3. Write your servlet and compile it. Take a look at sample servlet.
import javax.servlet.http.*;
import java.io.*;
public class SampleServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws Exception
{
doPost(req,resp); //call post method handler
}

public void doPost(HttpServletRequest req,HttpServletResponse resp) throws Exception
{
PrintWriter out=resp.getWriter();//obtain the stream to the output - to the browser
//we need to render html content as shown below.doesnt this suck???
out.println("
<html><head><title>Sample Servlet</title></headgt;<body>");
out.println("Bhargava's blog rocks!!!</body></html>");
}
}

How to compile??
Nothing different, but make sure you add path to the Servlet-api.jar which is present in the common/lib folder of Apach tomcat installation folder. How do you do that? well figure it out yourself. otherwise mail me.

4. Now that the servlet is ready, compile it and keep the .class file aside. Now Create a folder, say TestServlet and create a WEB-INF folder inside this folder. Inside the WEB-INF Folder, create a "classes" folder and copy your servlet class into the file. I shall give you a web.xml which is to be placed inside the WEB-INF Folder. Copy the TestServlet folder to webapps directory of the Tomcat installation folder. The following is the directory structure you should get as a result.
Apache installation path.....\webapps |-TestServlet
|-WEB-INF (-> web.xml is also present in web-inf)
|-classes
|-SampleServlet.class

5. Actually the better way is to create a .war archive. Create WEB-INF and place web.xml in it. Also classes directory with SampleServlet .class inside it. Go to command line and navigate to the folder which has this WEB-INF and type the following
jar cvf TestServlet .war WEB-INF
You can just copy the generated war archive into the webapps folder of Tomcat and restart Tomcat.
6. The web.xml is the configuration file that tells the container what servlets are available and other information(i dont know !! ) create the web.xml as it is. Try to understand what it says.

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


<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<!-- SERVLETS INFORMATION STARTS HERE -->
<!-- sample servlet for testing -->
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>

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

<!-- end sample servlet-->
</web-app>


Notice the value. It says when ever SampleServlet is called from browser , invoke the servlet whose name is SampleServlet whcih inturn loads the servlet whose class is mapped to this servlet, throuh

Assuming apache runs on localhost, and that you have deployed your servlet(by the way the web.xml is called Deployment Descriptor) you should type the following URL on browser

http://localhost/TestServlet/SampleServlet



You should get
Bhargav 's blog rocks!!


In case you dont get the output and get a 404 Error(resource not found) please verify if you have created the directory as i mentioned and most importantly the web.xml is correct or not. I once got stuck up with this problem and no where on the internet you can find solution for the problem!(not now cos i posted it on my blog! isnt my blog informative?) The problem was that i did not specify the schema to follow in the element inside web.xml, that is even the following portion is very important.



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

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">



I hope this brief tutorial gives a kick start for those interested in learning servlets. Actually there is more of servlets than what i just covered(what i told is simply about Deployment in Tomcat, nothing else)? Just google out and if you have any questions , feel free to contact me at krishnabhargav@yahoo.com. I am not bad at J2EE, though not as good as .NET. Anyway i can help you out.

Next on this servlets issue, i would post a tutorial on Forms based authentication using Servlets and Apache Tomcat container. Again there is no proper step-by-step tutorial for this online, so meanwhile please go through some sample servlets code. All the best. And please hate servlets :)) cos compared to servlets Asp.NEt is veryyyyy easy to work with.


Krish.