Servlets And JSP

What Are Servlets And JSP For?

Servlets and JSP are Java technologies for processing HTML forms on the Web. Almost any Java-based Web application will therefore use either Servlets, JSP, or both. If you're not using Java, most other frameworks have something to do the same job: Microsoft's technologies are ASP (obsolete) and ASP.NET. You can also use Perl, PHP, or ColdFusion. Finally, there is the granddaddy of all of these technologies, CGI (common gateway interface) which is supported by just about any Web server you might find yourself using.

In order to use Servlets and JSP, you need to have a Web server configured to pass form submissions to a JSP/Servlet processing engine. Tomcat is the reference implementation for JSP and Servlets, so it's a quick and easy tool to use during development. We will use it in this class. You can even consider using it in production as long as you are aware of its limitations. There are a bunch of commercial J2EE containers: Sun's iPlanet, BEA WebLogic, IBM WebSphere, etc...

Servlets vs. JSP

Servlets are written in Java. To create a servlet, you extend the Servlet class, just as you would extend the Applet class to make an Applet. When a user submits a Web form to a Servlet, the servlet is executed by the Servlet Container (Tomcat in our case). Since Servlets are JavaBeans, the state of the servlet can be automatically populated by the form. The servlet can also get parameters and attributes directly from the HttpServletRequest object.  The servlet then processes the input (for example, saves it to a database) and writes out its output in the form of HTML code, which is then used to generate the Web page the user sees following the form submission.

JSP is designed to help separate the business logic (processing) from the presentation. Instead of outputting HTML directly in your Java code, you can use an XML syntax to embed special JSP tags within HTML. Thus you can have a Web designer first develop a nice set of Web pages, then you can insert your JSP code into the designer's HTML.  JSP is similar to other such tag-based technologies: ASP, PHP, ColdFusion (which started it all).

Servlets and JSP are often combined inside of more advanced frameworks. Struts is one example. JavaServer Faces (from Sun) is another. For better or worse, there are more (lots more). If you are developing a fairly large application with Java, I recommend investigating a framework. I have had good experiences using Struts, which is one of the most popular Java Web Development frameworks. It is widely used and has been around since the late 90's. However, I haven't used any of the other frameworks, so I don't have any basis for comparison.

Servlet and JSP Deployment

There is a standard way to deploy Servlet and JSP applications inside of a J2EE container. Thus you can develop an application for Tomcat and you should be able to deploy it to some other container such as iPlanet fairly painlessly. We will have a look at this structure in class and have a brief discussion of War files.