Deploying Web Applications to Tomcat

Deploying Web Applications to Tomcat

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

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

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

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 com.onjava.login.java

package com.onjava;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
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 {

  super.init(config);
 }

 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 =
   context.getRequestDispatcher(target);
  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.

Prev  [1] [2] [3] [4] [5] Next

Close    To Top
  • Prev Article-Java:
  • Next Article-Java:
  • Now: Tutorial for Web and Software Design > Java > Java Servlets > Java Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction