You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Sanjay Choudhary <ch...@gmail.com> on 2004/11/05 19:49:25 UTC

Using Forward and Include using RequestProcessor

Hi All,

I have an application which contains 100+ jsp's.  Use case is to
inject the Javascript conditionally into the JSP's.  Condition is - If
a cookie is present in the request, inject the Javascript else leave
it alone.

I started with a simple struts example project for the proof of
concept.  I thought of a Servlet Filter and I created a filter as
below

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class SecurePageFilter implements Filter {
    private static final String SECURITY_COOKIE = "ObSSOCookie";
    private static final String SECURITY_JS = "/check.js";

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void destroy() {
    }

    public void doFilter(
        ServletRequest oRequest,
        ServletResponse oResponse,
        FilterChain oFilterChain)
        throws IOException, ServletException {

        oFilterChain.doFilter(oRequest, oResponse);

        HttpServletRequest woRequest = (HttpServletRequest) oRequest;
        Cookie[] aoCookies = woRequest.getCookies();
        String strUrl = woRequest.getRequestURI();

        if (strUrl != null
            && strUrl.endsWith("deleteCookie.jsp")
            || aoCookies == null)
            return;

        for (int i = 0; i < aoCookies.length; i++) {
            Cookie oCookie = aoCookies[i];

            if (oCookie.getName().equals(SECURITY_COOKIE)) {
                String strValue = oCookie.getValue();
                if (strValue != null && strValue.length() > 0) {
                    RequestDispatcher rd =
woRequest.getRequestDispatcher(SECURITY_JS);
                    rd.include(oRequest, oResponse);
                    oResponse.flushBuffer();
                    return;
                }
            }
        }
        return;
    } // doFilter

} // SecurePageFilter

My filter is mapped to (*.jsp and *.do) in the web.xml.

This works fine if I have a URI as
http://localhost:9080/struts-example/welcome.jsp

But this doesn't work if I use the URL
http://localhost:9080/struts-example/welcome.do


To explore more I downloaded Struts 1.2 source and took a look in
RequestDispatcher class --> processForward(..) , processInclude(..),
doForward(..) and doInclude(..) methods.  And off course, process(..)
method. In the process method , if request is forwarded using the
RequestDispatcher, which is normally the case in *.do types URI's,
control is returned back to the servlet without processing the
'processInclude'.  I further extended the RequestDispatcher class and
implemented my own doForward method. This is more or less same as
Struts implementation but also calls includes. Code is as below :-
    protected void doForward(
        String uri,
        HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException {

        // Unwrap the multipart request, if there is one.
        if (request instanceof MultipartRequestWrapper) {
            request = ((MultipartRequestWrapper) request).getRequest();
        }

        RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
        if (rd == null) {
            response.sendError(
                HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                getInternal().getMessage("requestDispatcher", uri));
            return;
        }
        rd.forward(request, response);
       doIncludeJS(request,response);        
    }

    protected void doIncludeJS(
        HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException {
        Cookie[] aoCookies = request.getCookies();
        String strUrl = request.getRequestURI();

        if (strUrl != null
            && strUrl.endsWith("deleteCookie.jsp")
            || aoCookies == null)
            return;


        for (int i = 0; i < aoCookies.length; i++) {
            Cookie oCookie = aoCookies[i];

            if (oCookie.getName().equals(SECURITY_COOKIE)) {
                String strValue = oCookie.getValue();
                if (strValue != null && strValue.length() > 0) {
					doInclude(SECURITY_JS,request, response);
                    break;
                }
            }
        }
        return;
    }

Still to my surprise, JS file did not get injected in the response.( I
debugged the application do ensure that doInclude(..) method is
called)

[Q1] - Can we use RequestDispatcher to Forward and Include for the
same request/response?  (I know we can't do Include and Forward, it
throws an error. But in case of Forward and Include no error is thrown
but nothing gets included).

[Q2] - Is there anyother way of including into request with URI like *.do?


Any help is highly appreciated.

Thanks,

Sanjay

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


Fwd: Using Forward and Include using RequestProcessor

Posted by Sanjay Choudhary <ch...@gmail.com>.
---------- Forwarded message ----------
From: Sanjay Choudhary <ch...@gmail.com>
Date: Fri, 5 Nov 2004 10:49:25 -0800
Subject: Using Forward and Include using RequestProcessor
To: user@struts.apache.org


Hi All,

I have an application which contains 100+ jsp's.  Use case is to
inject the Javascript conditionally into the JSP's.  Condition is - If
a cookie is present in the request, inject the Javascript else leave
it alone.

I started with a simple struts example project for the proof of
concept.  I thought of a Servlet Filter and I created a filter as
below

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class SecurePageFilter implements Filter {
   private static final String SECURITY_COOKIE = "ObSSOCookie";
   private static final String SECURITY_JS = "/check.js";

   public void init(FilterConfig filterConfig) throws ServletException {
   }

   public void destroy() {
   }

   public void doFilter(
       ServletRequest oRequest,
       ServletResponse oResponse,
       FilterChain oFilterChain)
       throws IOException, ServletException {

       oFilterChain.doFilter(oRequest, oResponse);

       HttpServletRequest woRequest = (HttpServletRequest) oRequest;
       Cookie[] aoCookies = woRequest.getCookies();
       String strUrl = woRequest.getRequestURI();

       if (strUrl != null
           && strUrl.endsWith("deleteCookie.jsp")
           || aoCookies == null)
           return;

       for (int i = 0; i < aoCookies.length; i++) {
           Cookie oCookie = aoCookies[i];

           if (oCookie.getName().equals(SECURITY_COOKIE)) {
               String strValue = oCookie.getValue();
               if (strValue != null && strValue.length() > 0) {
                   RequestDispatcher rd =
woRequest.getRequestDispatcher(SECURITY_JS);
                   rd.include(oRequest, oResponse);
                   oResponse.flushBuffer();
                   return;
               }
           }
       }
       return;
   } // doFilter

} // SecurePageFilter

My filter is mapped to (*.jsp and *.do) in the web.xml.

This works fine if I have a URI as
http://localhost:9080/struts-example/welcome.jsp

But this doesn't work if I use the URL
http://localhost:9080/struts-example/welcome.do

To explore more I downloaded Struts 1.2 source and took a look in
RequestDispatcher class --> processForward(..) , processInclude(..),
doForward(..) and doInclude(..) methods.  And off course, process(..)
method. In the process method , if request is forwarded using the
RequestDispatcher, which is normally the case in *.do types URI's,
control is returned back to the servlet without processing the
'processInclude'.  I further extended the RequestDispatcher class and
implemented my own doForward method. This is more or less same as
Struts implementation but also calls includes. Code is as below :-
   protected void doForward(
       String uri,
       HttpServletRequest request,
       HttpServletResponse response)
       throws IOException, ServletException {

       // Unwrap the multipart request, if there is one.
       if (request instanceof MultipartRequestWrapper) {
           request = ((MultipartRequestWrapper) request).getRequest();
       }

       RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
       if (rd == null) {
           response.sendError(
               HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
               getInternal().getMessage("requestDispatcher", uri));
           return;
       }
       rd.forward(request, response);
      doIncludeJS(request,response);
   }

   protected void doIncludeJS(
       HttpServletRequest request,
       HttpServletResponse response)
       throws IOException, ServletException {
       Cookie[] aoCookies = request.getCookies();
       String strUrl = request.getRequestURI();

       if (strUrl != null
           && strUrl.endsWith("deleteCookie.jsp")
           || aoCookies == null)
           return;

       for (int i = 0; i < aoCookies.length; i++) {
           Cookie oCookie = aoCookies[i];

           if (oCookie.getName().equals(SECURITY_COOKIE)) {
               String strValue = oCookie.getValue();
               if (strValue != null && strValue.length() > 0) {
                                       doInclude(SECURITY_JS,request, response);
                   break;
               }
           }
       }
       return;
   }

Still to my surprise, JS file did not get injected in the response.( I
debugged the application do ensure that doInclude(..) method is
called)

[Q1] - Can we use RequestDispatcher to Forward and Include for the
same request/response?  (I know we can't do Include and Forward, it
throws an error. But in case of Forward and Include no error is thrown
but nothing gets included).

[Q2] - Is there anyother way of including into request with URI like *.do?

Any help is highly appreciated.

Thanks,

Sanjay

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


Using Forward and Include using RequestProcessor

Posted by Sanjay Choudhary <ch...@gmail.com>.
On Fri, 5 Nov 2004 10:49:25 -0800, Sanjay Choudhary
<ch...@gmail.com> wrote:
> Hi All,
>
> I have an application which contains 100+ jsp's.  Use case is to
> inject the Javascript conditionally into the JSP's.  Condition is - If
> a cookie is present in the request, inject the Javascript else leave
> it alone.
>
> I started with a simple struts example project for the proof of
> concept.  I thought of a Servlet Filter and I created a filter as
> below
>
> import java.io.IOException;
>
> import javax.servlet.Filter;
> import javax.servlet.FilterChain;
> import javax.servlet.FilterConfig;
> import javax.servlet.RequestDispatcher;
> import javax.servlet.ServletException;
> import javax.servlet.ServletRequest;
> import javax.servlet.ServletResponse;
> import javax.servlet.http.Cookie;
> import javax.servlet.http.HttpServletRequest;
>
> public class SecurePageFilter implements Filter {
>    private static final String SECURITY_COOKIE = "ObSSOCookie";
>    private static final String SECURITY_JS = "/check.js";
>
>    public void init(FilterConfig filterConfig) throws ServletException {
>    }
>
>    public void destroy() {
>    }
>
>    public void doFilter(
>        ServletRequest oRequest,
>        ServletResponse oResponse,
>        FilterChain oFilterChain)
>        throws IOException, ServletException {
>
>        oFilterChain.doFilter(oRequest, oResponse);
>
>        HttpServletRequest woRequest = (HttpServletRequest) oRequest;
>        Cookie[] aoCookies = woRequest.getCookies();
>        String strUrl = woRequest.getRequestURI();
>
>        if (strUrl != null
>            && strUrl.endsWith("deleteCookie.jsp")
>            || aoCookies == null)
>            return;
>
>        for (int i = 0; i < aoCookies.length; i++) {
>            Cookie oCookie = aoCookies[i];
>
>            if (oCookie.getName().equals(SECURITY_COOKIE)) {
>                String strValue = oCookie.getValue();
>                if (strValue != null && strValue.length() > 0) {
>                    RequestDispatcher rd =
> woRequest.getRequestDispatcher(SECURITY_JS);
>                    rd.include(oRequest, oResponse);
>                    oResponse.flushBuffer();
>                    return;
>                }
>            }
>        }
>        return;
>    } // doFilter
>
> } // SecurePageFilter
>
> My filter is mapped to (*.jsp and *.do) in the web.xml.
>
> This works fine if I have a URI as
> http://localhost:9080/struts-example/welcome.jsp
>
> But this doesn't work if I use the URL
> http://localhost:9080/struts-example/welcome.do
>
> To explore more I downloaded Struts 1.2 source and took a look in
> RequestDispatcher class --> processForward(..) , processInclude(..),
> doForward(..) and doInclude(..) methods.  And off course, process(..)
> method. In the process method , if request is forwarded using the
> RequestDispatcher, which is normally the case in *.do types URI's,
> control is returned back to the servlet without processing the
> 'processInclude'.  I further extended the RequestDispatcher class and
> implemented my own doForward method. This is more or less same as
> Struts implementation but also calls includes. Code is as below :-
>    protected void doForward(
>        String uri,
>        HttpServletRequest request,
>        HttpServletResponse response)
>        throws IOException, ServletException {
>
>        // Unwrap the multipart request, if there is one.
>        if (request instanceof MultipartRequestWrapper) {
>            request = ((MultipartRequestWrapper) request).getRequest();
>        }
>
>        RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
>        if (rd == null) {
>            response.sendError(
>                HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
>                getInternal().getMessage("requestDispatcher", uri));
>            return;
>        }
>        rd.forward(request, response);
>       doIncludeJS(request,response);
>    }
>
>    protected void doIncludeJS(
>        HttpServletRequest request,
>        HttpServletResponse response)
>        throws IOException, ServletException {
>        Cookie[] aoCookies = request.getCookies();
>        String strUrl = request.getRequestURI();
>
>        if (strUrl != null
>            && strUrl.endsWith("deleteCookie.jsp")
>            || aoCookies == null)
>            return;
>
>        for (int i = 0; i < aoCookies.length; i++) {
>            Cookie oCookie = aoCookies[i];
>
>            if (oCookie.getName().equals(SECURITY_COOKIE)) {
>                String strValue = oCookie.getValue();
>                if (strValue != null && strValue.length() > 0) {
>                                        doInclude(SECURITY_JS,request, response);
>                    break;
>                }
>            }
>        }
>        return;
>    }
>
> Still to my surprise, JS file did not get injected in the response.( I
> debugged the application do ensure that doInclude(..) method is
> called)
>
> [Q1] - Can we use RequestDispatcher to Forward and Include for the
> same request/response?  (I know we can't do Include and Forward, it
> throws an error. But in case of Forward and Include no error is thrown
> but nothing gets included).
>
> [Q2] - Is there anyother way of including into request with URI like *.do?
>
> Any help is highly appreciated.
>
> Thanks,
>
> Sanjay
>

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


Re: Using Forward and Include using RequestProcessor

Posted by Sanjay Choudhary <ch...@gmail.com>.
posting again


On Fri, 5 Nov 2004 10:49:25 -0800, Sanjay Choudhary
<ch...@gmail.com> wrote:
> Hi All,
> 
> I have an application which contains 100+ jsp's.  Use case is to
> inject the Javascript conditionally into the JSP's.  Condition is - If
> a cookie is present in the request, inject the Javascript else leave
> it alone.
> 
> I started with a simple struts example project for the proof of
> concept.  I thought of a Servlet Filter and I created a filter as
> below
> 
> import java.io.IOException;
> 
> import javax.servlet.Filter;
> import javax.servlet.FilterChain;
> import javax.servlet.FilterConfig;
> import javax.servlet.RequestDispatcher;
> import javax.servlet.ServletException;
> import javax.servlet.ServletRequest;
> import javax.servlet.ServletResponse;
> import javax.servlet.http.Cookie;
> import javax.servlet.http.HttpServletRequest;
> 
> public class SecurePageFilter implements Filter {
>    private static final String SECURITY_COOKIE = "ObSSOCookie";
>    private static final String SECURITY_JS = "/check.js";
> 
>    public void init(FilterConfig filterConfig) throws ServletException {
>    }
> 
>    public void destroy() {
>    }
> 
>    public void doFilter(
>        ServletRequest oRequest,
>        ServletResponse oResponse,
>        FilterChain oFilterChain)
>        throws IOException, ServletException {
> 
>        oFilterChain.doFilter(oRequest, oResponse);
> 
>        HttpServletRequest woRequest = (HttpServletRequest) oRequest;
>        Cookie[] aoCookies = woRequest.getCookies();
>        String strUrl = woRequest.getRequestURI();
> 
>        if (strUrl != null
>            && strUrl.endsWith("deleteCookie.jsp")
>            || aoCookies == null)
>            return;
> 
>        for (int i = 0; i < aoCookies.length; i++) {
>            Cookie oCookie = aoCookies[i];
> 
>            if (oCookie.getName().equals(SECURITY_COOKIE)) {
>                String strValue = oCookie.getValue();
>                if (strValue != null && strValue.length() > 0) {
>                    RequestDispatcher rd =
> woRequest.getRequestDispatcher(SECURITY_JS);
>                    rd.include(oRequest, oResponse);
>                    oResponse.flushBuffer();
>                    return;
>                }
>            }
>        }
>        return;
>    } // doFilter
> 
> } // SecurePageFilter
> 
> My filter is mapped to (*.jsp and *.do) in the web.xml.
> 
> This works fine if I have a URI as
> http://localhost:9080/struts-example/welcome.jsp
> 
> But this doesn't work if I use the URL
> http://localhost:9080/struts-example/welcome.do
> 
> To explore more I downloaded Struts 1.2 source and took a look in
> RequestDispatcher class --> processForward(..) , processInclude(..),
> doForward(..) and doInclude(..) methods.  And off course, process(..)
> method. In the process method , if request is forwarded using the
> RequestDispatcher, which is normally the case in *.do types URI's,
> control is returned back to the servlet without processing the
> 'processInclude'.  I further extended the RequestDispatcher class and
> implemented my own doForward method. This is more or less same as
> Struts implementation but also calls includes. Code is as below :-
>    protected void doForward(
>        String uri,
>        HttpServletRequest request,
>        HttpServletResponse response)
>        throws IOException, ServletException {
> 
>        // Unwrap the multipart request, if there is one.
>        if (request instanceof MultipartRequestWrapper) {
>            request = ((MultipartRequestWrapper) request).getRequest();
>        }
> 
>        RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
>        if (rd == null) {
>            response.sendError(
>                HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
>                getInternal().getMessage("requestDispatcher", uri));
>            return;
>        }
>        rd.forward(request, response);
>       doIncludeJS(request,response);
>    }
> 
>    protected void doIncludeJS(
>        HttpServletRequest request,
>        HttpServletResponse response)
>        throws IOException, ServletException {
>        Cookie[] aoCookies = request.getCookies();
>        String strUrl = request.getRequestURI();
> 
>        if (strUrl != null
>            && strUrl.endsWith("deleteCookie.jsp")
>            || aoCookies == null)
>            return;
> 
>        for (int i = 0; i < aoCookies.length; i++) {
>            Cookie oCookie = aoCookies[i];
> 
>            if (oCookie.getName().equals(SECURITY_COOKIE)) {
>                String strValue = oCookie.getValue();
>                if (strValue != null && strValue.length() > 0) {
>                                        doInclude(SECURITY_JS,request, response);
>                    break;
>                }
>            }
>        }
>        return;
>    }
> 
> Still to my surprise, JS file did not get injected in the response.( I
> debugged the application do ensure that doInclude(..) method is
> called)
> 
> [Q1] - Can we use RequestDispatcher to Forward and Include for the
> same request/response?  (I know we can't do Include and Forward, it
> throws an error. But in case of Forward and Include no error is thrown
> but nothing gets included).
> 
> [Q2] - Is there anyother way of including into request with URI like *.do?
> 
> Any help is highly appreciated.
> 
> Thanks,
> 
> Sanjay
>

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