Deploying Web Applications to Tomcat
Pages: 1, 2, 3, 4, 5

If you changed the default HTTP port, as mentioned in Installing and Configuring Tomcat, you will need to request the URL from the appropriate port value. If everything was configured correctly, you should see an image similar to Figure 1.

Screen shot.
Figure 1. If everything is configured correctly, you'll see something like this.

If you do not see a page similar to this image, make sure you have the correct entry in the server.xml file, as described in the section, "Creating a Web Application ServletContext."

The second JSP is the target JSP referenced by the servlet defined in the following section. This JSP will retrieve the request attribute USER that was added to the request with the following servlet. It will then output the String value of the attribute. Listing 3 contains the source for the target JSP.

Listing 3 welcome.jsp

 <title>OnJava Demo</title>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

 <table width="500" border="0" cellspacing="0" cellpadding="0">
   <td> </td>
   <img src="/onjava/images/monitor2.gif"></td>
   <b>Welcome : <%= request.getAttribute("USER")
   <td> </td>

As we stated earlier to deploy this JSP, you simply need to copy it to the public directory of your web application, which in this case is TOMCAT_HOME/webapps/onjava/.

Adding Servlets

The next component to add is a servlet. This servlet will be the action of login.jsp's form. It will retrieve the username and password values from HttpServletRequest, look up the associated user, and then forward the request to a target JSP. The source code for this servlet can be found in Listing 4.

For our example the value of the USER is static. Normally you would perform a real lookup of some sort, but, to keep things simple, I'm just returning String Bob.

Listing 4

package com.onjava;

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

public class login extends HttpServlet {

 private String target = "/welcome.jsp";

 private String getUser(String username, String password) {

// Just return a static name
  // If this was reality, we would perform a SQL lookup
  return "Bob";

 public void init(ServletConfig config)
  throws ServletException {


 public void doGet(HttpServletRequest request,
  HttpServletResponse response)
  throws ServletException, IOException {

  // If it is a get request forward to doPost()
  doPost(request, response);

 public void doPost(HttpServletRequest request,
  HttpServletResponse response)
  throws ServletException, IOException {

  // Get the username from the request
  String username = request.getParameter("username");
  // Get the password from the request
  String password = request.getParameter("password");

  String user = getUser(username, password);

  // Add the fake user to the request
  request.setAttribute("USER", user);

  // Forward the request to the target named
  ServletContext context = getServletContext();

  RequestDispatcher dispatcher =
  dispatcher.forward(request, response);

 public void destroy() {

To deploy a servlet as part of a web application you first need to compile the servlet and move it into the web application's /WEB-INF/classes directory. For this example, you should compile this servlet and move it to the /onjava/WEB-INF/classes/com/onjava/ directory.

This class file is in the subdirectory com.onjava because of its package name.

Pages: 1, 2, 3, 4, 5

Next Pagearrow