You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Vikramjit Singh <vi...@gtllimited.com> on 2002/06/18 06:39:47 UTC

RE: I get a 404 when using requestDispatcher (using Tomcat-4.0.2) ? servlets

i dont think that u get 404 is not due to tomcat, but due to the path which
you are giving, in your request dispatcher. I didnt go through your code,
but for RequestDispatcher to work the path should be relative to the
context. If you have the servlets in the same package, which you have, then
i think this should work

RequestDispatcher rd = getServletContext().getRequestDispatcher("/Panel");

the url pattern should be defined in your web.xml file.

Regards,
Vikram.


-----Original Message-----
From: Denis Shurtleff [mailto:dcs2120@draper.com]
Sent: Monday, June 17, 2002 7:39 AM
To: tomcat-user@jakarta.apache.org
Subject: I get a 404 when using requestDispatcher (using Tomcat-4.0.2)?
servlets


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:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>