tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Denis Shurtleff <dcs2...@draper.com>
Subject I get a 404 when using requestDispatcher (using Tomcat-4.0.2)? servlets
Date Mon, 17 Jun 2002 14:39:17 GMT
If anyone needs them to be a simple little test or whatever:

Switch.java:

/* 
 * 
 * Switch.java
 *
 * $Author: dcs2120 $
 * $Date: 2001/12/04 15:02:21 $
 * $Revision: 1.2 $
 *
 */
package adminForms;


import java.io.IOException;
import java.io.PrintWriter; 
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Test requestDispatcher forwarding.
 */
public final class Switch extends HttpServlet{

    /* the client notification message, used only when running 
       this servlet; not on form generation */
    private String message = "";

    /**
     * Provides HTTP GET action for processing.
     * Not used by the form as it's defined as a POST.
     *
     * @param request the request sent via the QUERYSTRING.
     * @param response the servlet response handler.
     */
    public void doGet
      (HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        processRequest(request,response);
    }

    /**
     * Provides HTTP POST action for processing.
     * Primary way of accessing this form.
     *
     * @param request the request in the contents.
     * @param response the servlet response handler.
     */
    public void doPost
      (HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        processRequest(request, response);
    }
     
    /**
     * @return a string containing the form for this servlet.
     */
    public static String getSwitchForm(){
        return " <b>Test RequestDispatcher forwarding.</b>\n" + 
            "<form method=POST name=\"testRd\" " + 
            "action=\"/app/switch\">" +
            "<table>\n" +
            " <tr>\n" +
            "   <td><b>Reload this page.</b></td>\n" +
            "   <td><input type=submit name=\"action\" value=\"" + 
            "Reload\"></td>\n" +
            "  </tr>\n" +
            "  <tr>\n" +
            "   <td><b>Get the panel.</b></td>\n" +
            "   <td><input type=submit name=\"action\" " +
            "value=\"Panel\"></td>\n" +
            "  </tr>\n" +
            "  </table>\n" +
            " </tr>\n" +
            "</table>\n" + 
            "</form>\n";
    }

    /**
     * Redirect the request to another servlet/jsp.
     * 
     * @param address the address to forward the request to.
     * @param request the request to forward.
     * @param response the response to forward.
     */
    private void redirectRequest
        (String address, HttpServletRequest request, 
         HttpServletResponse response) 
    throws IOException, ServletException{
        RequestDispatcher rd = 
            getServletContext().getRequestDispatcher
            (address);
        rd.forward(request, response);
    }
    
    /**
     * Provides HTTP GET action for processing.
     * Not used by the form as it'eees defined as a POST.
     *
     * @param request the request sent via the QUERYSTRING.
     * @param response the servlet response handler.
     */
    private void processRequest
      (HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        
        // Get all the form parameters; set to null if not found.
        String action  = (String)request.getParameter("action");
        // the form string
        String form   = "";
        // reset message to "" because or reuse issues.
        message = "";
        
        if("Panel".equals(action)){
            redirectRequest("/app/panel", request, response);
        }else if("Reload".equals(action))
            message += "Reload pushed.";
        form = Switch.getSwitchForm();
        
        // great, we must print out a proper html page back
        // to the client.
        PrintWriter printy = new PrintWriter(response.getWriter());
        printy.println
            ("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD " + 
             "HTML 4.0 Transitional//EN\">");
        printy.println("<html>\n" + 
                       "<head>\n" + 
                       "<title>RequestDispatcher Test</title>\n" +
                       "</head>\n" +
                       "<body>\n");
        // get the form in there.
        printy.print(form);
        // if we have a message to report; do it.
        if(message != null && message.length() > 0)
            printy.print("<br>\n<i>" + message + "</i>\n");
        // clean up the page by terminating it properly.
        printy.print("</body>\n" +
                     "</html>");
    }
}


Panel.java:

package adminForms;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public final class Panel extends HttpServlet{
    
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
      throws IOException, ServletException {
        processRequest(request,response);
    }
    
    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
      throws IOException, ServletException {
        processRequest(request,response);
    }

    private void processRequest
      (HttpServletRequest request,HttpServletResponse response)
        throws IOException, ServletException{
        
        String action = (String)request.getParameter( "action" );       
        String form = null;
        
        form = "<h1><b><i>IT WORKED!!!!!" + action + "</i></b></h1>\n";
        
        PrintWriter printy = new PrintWriter(response.getWriter());
        
        printy.print
            ("<html>\n" +
             "<head>\n" +
             "<title>My God did it work?!</title>\n" +
             "</head>\n" +
             "<body>\n");
        printy.print( form );
        printy.print("</body>\n</html>\n");
    }
}

pretty simple.

-Denis Shurtleff

Programmer/Integrator
Charles Stark Draper Labs


--
To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>


Mime
View raw message