Learning JSP

Servlets and JSP



A simple "HelloWorld" servlet, that also prints the current date.


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

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

	PrintWriter out = res.getWriter();

	out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>");
	out.println("<H1>Hello World</H1>");
	out.println("Today is: " + (new java.util.Date().toString()) );
   } // doGet

} // HelloWorld

In order to run it, do the following:

  1. Place it in a file, HelloWorld.java
  2. Compile it.
  3. Place the resulting HelloWorld.class file in the "servlets" directory.
  4. Run it by pointing browser to: http://server.address/servlets/HelloWorld



The same page as above using JSP looks as shown below:


  <TITLE>Hello World</TITLE>
  <H1>Hello World</H1>
  Today is: <%= new java.util.Date().toString() %>


In order to run it, do the following:

  1. Place it in a file, HelloWorld.jsp in the same directory as your .html files
  2. Run it by pointing browser to: http://server.address/~youraccount/HelloWorld.jsp


Created by Deepak Kumar (dkumar@acm.org)