You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Tuan H. Le" <tu...@yahoo.com> on 2005/02/13 18:51:41 UTC

Problem with front controller for redirecting to a Struts action

Hi,

PROBLEM:
Please help with my servlet mapping issue. It's
calling my redirector class repeately and the
redirector does not work.

REQUIREMENTS:
I want to give my client a personalized URL like
http://www.mydomain.com/myWebApp/FirstName_LastName
(NOTE: no file extension).

When the user enters that URL above, it would be
processed by a servlet
RequestProfileRedirectorServlet, which is defined in
web.xml and it has a servlet mapping to "/*" (NOTE:
see servlet code and web.xml config below). 

The main task for the RequestProfileRedirectorServlet
is to get the request path info (NOTE: in my case it's
/FirstName_LastName), build a redirect URL, and
redirect it to a Struts welcome action (welcome.do) to
query the user profile from the database and display
the user profile in a welcome view. An example of a
redirect URL would be

http://www.mydomain.com/myWebApp/welcome.do?cid=FirstName_LastName

SOURCE:
public final class RequestProfileRedirectorServlet
extends HttpServlet implements IConstants {

	/**
	 * Commons Logging instance.
	 */
	private Log log = LogFactory.getLog(PACKAGE);
	
	public RequestProfileRedirectorServlet() {
		super();
	}

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

    /**
     * This method handles the GET requests from the
client.
     */
    public void doGet(HttpServletRequest request, 
                      HttpServletResponse  response)
                      throws IOException,
ServletException {
      
        String servletPath = request.getServletPath();
        String contextPath = request.getContextPath();
        String pathInfo = request.getPathInfo();
        String queryString = request.getQueryString();
        String requestURL =
request.getRequestURL().toString();
        String requestURI = request.getRequestURI();
        
        log.debug("servletPath --> " + servletPath);
        log.debug("contextPath --> " + contextPath);
        log.debug("pathInfo --> " + pathInfo);
        log.debug("queryString --> " + queryString);
        log.debug("requestURL --> " + requestURL);
        log.debug("requestURI --> " + requestURI);
        
        String customerId = pathInfo;
        StringBuffer sbURL = new StringBuffer();
       
sbURL.append("http://localhost:8080/msa/welcome.do?");
        sbURL.append("&cid=" + customerId);
       
response.sendRedirect(response.encodeRedirectURL(sbURL.toString()));
       
    }
}

WEB.XML
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web
Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd">
  
<web-app>
  <display-name>My Application</display-name>
	   
  <servlet>
    <servlet-name>Log4jInitServlet</servlet-name>
   
<servlet-class>com.pps.msa.actions.Log4jInitServlet</servlet-class>
    <init-param>
       <param-name>LOG4J_CONFIG_FILE</param-name>
      
<param-value>WEB-INF/classes/com/pps/msa/resources/log4j.properties</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
    
  <servlet>
    <servlet-name>redirector</servlet-name>
<servlet-class>com.pps.msa.actions.RequestProfileRedirectorServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
    	
  <servlet>
    <servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
     
  <servlet-mapping>
    <servlet-name>redirector</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <!-- more other stuffs... -->  
</web-app>

Do you know what I'm missing?

Thanks in advance for your help!
Tuan

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org