Java Servlets & JSP


Java Servlets

Java Servlets are very like Java Applets. In fact Java Servlets can be embedded into web pages in much the same manner as Java Applets can be. Java Servlets run on a server and Java Applets run on a client machine. Therefore Java Servlets should execute faster than Java Applets since Applets are downloaded to a client machine prior to execution on the client and Servlets are executed on the server. Servlets are commonly used to generate web pages from scratch or to generate sub-sections of web pages. These web pages typically include HTML, DHTML and JavaScript. Java Servlets allow for a complete web site to be executed from the server.

Below is a simple example of a Java Servlet.

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

public class hithere extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<HTML>");
        out.println("<BODY>");
        out.println("<HEAD>");
        out.println("<TITLE>Hi There !</TITLE>");
        out.println("</HEAD>");
        out.println("<BODY>");
        out.println("<H1><font color=\"red\" size=\"6\">Hi There !</font></H1>");
        out.println("</BODY>");
        out.println("</HTML>");
    }
}

JSP (Java Server Pages)

JSP (Java Server Pages) is the Java equivalent of ASP (Active Server Pages). JSP allows the inclusion of Java coded commands into a JSP page as ASP allows the inclusion of VBScript commands into an ASP page.

There are a number of possible advantages of JSP over ASP.