Wednesday, March 16, 2005

Servlets and JSP : An Introduction

I am going to give a short introduction to Servlets and Java Server Pages(JSP) which are used to create dynamic web pages using Java technology and are part of J2EE.

Both Servlets and JSP have to be run within a web-container. The web-server will start on a particular port on which pages are served. I recommend using Jakarta Tomcat as your web-server. Web-Containers are also embedded within application servers. Installation of Tomcat is very easy.

Both Servlets and JSP are basically simple Java class files. This is a simple servlet -

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

public class HelloWorldServlet extends HttpServlet {

public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

ServletOutputStream out = res.getOutputStream();
out.println(" < html >");
out.println(" < head > < title > Hello World < /title
> < / head >");
out.println(" < body >");
out.println(" < h1 > Hello World < /h1 > ");
out.println("Server time is " + System.currentTimeMillis());
out.println(" < /body > < /html > ");

public String getServletInfo() {
return "Create a page that says Hello World";

The code should be mostly self explanatory.

The HttpServlet contains two important methods, doGet and doPost. doGet is used to handle HTTP GET requests and doPost is used to handle HTTP POST requests. The path to invoke this Servlet will depend on the web-server used and settings made. When the url for this Servlet is requested, the doGet method is invoked.

Within the doGet method, the HttpRequest and HttpResponse are passed as params. So both can be checked. An output stream to the HttpResponse instance is opened and any html to be displayed to the user, is simply thrown to the output stream. As simple as that.

The second method getServletInfo is analogous to toString and will never be used that much.

As you'll notice the HelloWorldServlet is a simple java class file. So you can easily make your web-page dynamic. Like in this example, I simply print the Server time. If/Else loops can be added, Database connections opened, anything you can do within a normal class can be done here too. But this obviously leads to a mess, with UI, logic and everything else getting mixed up.

Basically the first time the url is requested, the Servlet class file is compiled. The next request onwards, the compiled class file is simply executed.

Also Servlets are a bit cumbersome to code. So JSP was introduced. This is a sample JSP page -

< html >
< head > < title > Hello World < /title > < / head >
< body >
< h1 > Hello World < /h1 >
Server time is < %= System.currentTimeMillis() >
< /body > < /html >

As in the servlet example, whenever this url is requested, the time of the server is shown within the response web-page. This code is written within a .jsp file and placed within the web-server. The web-container will automatically generate a Servlet for this JSP page and then serve requests. Lots of the boring stuff in Servlets is taken care of. But in essence, the Servlet generated for this Jsp page will be very similar to the above page.

The < %= "Java code here" > is used to embed java code within a Jsp page. There are additional tags, directives and methods for scripting. Both Servlets and Jsp also have advanced features for filters, custom tags, session management etc.

The best way to get the basics is to practice and read more examples. Download and install Tomcat. There are sample examples which explain a lot. Also you should get a whole bunch of tutorials at developerworks-java. This is one tutorial.

No comments: