Hi, right now I have:
<%
if(session.getAttribute("verified") == null){
response.sendRedirect("../PortalLogin.jsp");
return;
}
%>
Inside of a jsp page. Basically if the user isn't verified, it redirects to the login page.
Well, I have a struts action (a global forward) that forwards to the login page and i would like to be able to call the struts forward from within every jsp page rather than do the response redirect. Can this be done, and how? Thanks!
Discussions
Web tier: servlets, JSP, Web frameworks: How to redirect to a struts action from within JSP - easy?
-
How to redirect to a struts action from within JSP - easy? (13 messages)
- Posted by: A. Ritchie
- Posted on: November 04 2005 09:57 EST
Threaded Messages (13)
- How to redirect to a struts action from within JSP - easy? by Jyothish John on November 07 2005 04:07 EST
- more info by A. Ritchie on November 07 2005 09:39 EST
-
more info by Jyothish John on November 08 2005 02:29 EST
-
that's good by A. Ritchie on November 08 2005 11:41 EST
-
Don't allow users to access your JSP pages by Michael Jouravlev on November 08 2005 04:51 EST
-
tellling me by A. Ritchie on November 09 2005 08:56 EST
- tellling me by Michael Jouravlev on November 10 2005 03:40 EST
-
tellling me by A. Ritchie on November 09 2005 08:56 EST
-
Don't allow users to access your JSP pages by Michael Jouravlev on November 08 2005 04:51 EST
-
that's good by A. Ritchie on November 08 2005 11:41 EST
-
more info by Jyothish John on November 08 2005 02:29 EST
- more info by A. Ritchie on November 07 2005 09:39 EST
- How to redirect to a struts action from within JSP - easy? by Alex Thieme on November 07 2005 11:07 EST
- have that by A. Ritchie on November 07 2005 14:01 EST
- How to redirect to a struts action from within JSP - easy? by paul strachan on November 10 2005 21:51 EST
- Re: Redirect struts action from jsp by benjamin thomas on November 14 2005 18:23 EST
- thats the ticket by A. Ritchie on November 15 2005 10:26 EST
- thanks again by A. Ritchie on November 16 2005 09:16 EST
- thats the ticket by A. Ritchie on November 15 2005 10:26 EST
-
How to redirect to a struts action from within JSP - easy?[ Go to top ]
- Posted by: Jyothish John
- Posted on: November 07 2005 04:07 EST
- in response to A. Ritchie
Hi, right now I have: <% if(session.getAttribute("verified") == null){ response.sendRedirect("../PortalLogin.jsp"); return; } %>Inside of a jsp page. Basically if the user isn't verified, it redirects to the login page.Well, I have a struts action (a global forward) that forwards to the login page and i would like to be able to call the struts forward from within every jsp page rather than do the response redirect. Can this be done, and how? Thanks!
Why are you doing the redirect from the JSP, instead do the redirect from the underlying Action itself..! -
more info[ Go to top ]
- Posted by: A. Ritchie
- Posted on: November 07 2005 09:39 EST
- in response to Jyothish John
What I'm doing is posting the code in the jsp that checks to see if there is a variable in the session. If that variable is not there, then I want to redirect the user to the login page and do Not display any of the page contents. So that is why the piece of code is in there. Does that make sense? -
more info[ Go to top ]
- Posted by: Jyothish John
- Posted on: November 08 2005 02:29 EST
- in response to A. Ritchie
What I'm doing is posting the code in the jsp that checks to see if there is a variable in the session. If that variable is not there, then I want to redirect the user to the login page and do Not display any of the page contents. So that is why the piece of code is in there. Does that make sense?
Well that did make sense, that you are using a piece of code (scriptlet) in JSP, for checking the existence of a session variable.
What I wanted to say is; since you are developing in struts framework (You are since you said you have a global Struts Action ?),
The easier way in struts to do such a task , is to check the existence/validity of such a session variable inside the execute method of an action class from which you can define forward mappings in struts-config.xml file and control your navigation instead.
/**Action Class Execute Method*/
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)throws Exception
{
// DO something like this
if(request.getSession().getAttribute("xxx")==null)
{
return mapping.findForward("login");
}
return mapping.findForward("doXXX");
}
And in the struts-config.xml you can have an entry
<!-- Redirect -->
<action path="/doXXX"
type="za.co.xxx.MyAction">
<forward name="login" path="Specify the page" />
<forward name="doXXX" path="Specify the page" />
</action>
This will help you without having the 'piece of code' on any JSP Page;
If I am still mistaken , be free to revert .>!
J -
that's good[ Go to top ]
- Posted by: A. Ritchie
- Posted on: November 08 2005 11:41 EST
- in response to Jyothish John
That's good, and I actually use that same type of code in my action classes. HOWEVER, what if a user types in the direct jsp page into the browser such as www.mysite.com/documents.jsp
No action will be called, so that wont work. The jsp has to have the code in it to protect your page, assuming you aren't using a filter. Right? Am I off the deep end :)? Set me straight here. -
Don't allow users to access your JSP pages[ Go to top ]
- Posted by: Michael Jouravlev
- Posted on: November 08 2005 16:51 EST
- in response to A. Ritchie
That's good, and I actually use that same type of code in my action classes. HOWEVER, what if a user types in the direct jsp page into the browser such as www.mysite.com/documents.jspNo action will be called, so that wont work. The jsp has to have the code in it to protect your page, assuming you aren't using a filter. Right? Am I off the deep end :)? Set me straight here.
Either put JSP pages under WEB-INF, or create a security rule which prohibits users to load files from a certain directory or with certain extension. -
tellling me[ Go to top ]
- Posted by: A. Ritchie
- Posted on: November 09 2005 08:56 EST
- in response to Michael Jouravlev
So you guys are telling me that there is no way to do something like this:
page.jsp:
<%
if(session.getAttribute("verified") == null){
return(mapping.findForward("portallogin"));
}
%>
??? Really? The above fake code is what i would like to do. -
tellling me[ Go to top ]
- Posted by: Michael Jouravlev
- Posted on: November 10 2005 15:40 EST
- in response to A. Ritchie
So you guys are telling me that there is no way to do something like this:page.jsp: <% if(session.getAttribute("verified") == null){ return(mapping.findForward("portallogin")); } %>??? Really? The above fake code is what i would like to do.
Of course, you can do that. But you would need to write this (or include this piece) in every page you want to protect.
Also, I personally seem this as illogical. If users are supposed to use actions, and JSP pages are shown only by server, then before displaying a page server should already know is it allowed to display it or not. You might say that this protection is from occasional direct access by a user. Great, then hide these files from a user altogether. -
How to redirect to a struts action from within JSP - easy?[ Go to top ]
- Posted by: Alex Thieme
- Posted on: November 07 2005 11:07 EST
- in response to A. Ritchie
I wasn't sure if having the code in your JSP was a requirement at all; but, one approach you might consider is implementing a servlet Filter and reference that in web.xml. You could have the filter apply to all requests, or a subset of request (by URL). In there, you could check for your session variable, and either proceed to the intended URL or redirect to your Struts action.
For example:
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
{
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
HttpSession session = request.getSession();
Boolean tVerified = (session.getAttribute("verified") != null);
if (!tVerified)
{
response.sendRedirect("PortalLogin.jsp");
return; // return so that we do not chain to other filters
}
// allow others filter to be chained
chain.doFilter(request, response);
} -
have that[ Go to top ]
- Posted by: A. Ritchie
- Posted on: November 07 2005 14:01 EST
- in response to Alex Thieme
I actually have that exact same servlet filter implemented already. I just have the jsp code there as a backup, in case my program goes on a machine and there isn't a servlet filter. Plus, I'd like to just know the answer so i know for knowing sake :) Any ideas on how to do the call from the jsp if possible? -
How to redirect to a struts action from within JSP - easy?[ Go to top ]
- Posted by: paul strachan
- Posted on: November 10 2005 21:51 EST
- in response to A. Ritchie
could you have this in your JSP:
<%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" prefix="logic" %>
<logic:redirect forward="NAME_OF_YOUR_GLOBAL_FORWARD"/>
A good option might be to verify the user on every request...and put your redirect logic in a servlet filter.
You filter setup(in web.xml) might look something like
<filter>
<filter-name>AuthorisationFilter</filter-name>
<filter-class>com.your.package.filters.AuthorisationFilter</filter-class>
<init-param>
<!-- This parameter is relative to the application context, so must start with a / -->
<param-name>UNAUTHORISED_REDIRECT_PAGE</param-name>
<param-value>/accessDenied.do</param-value>
</init-param>
<init-param>
<!-- This parameter is relative to the application context, so must start with a / -->
<param-name>LOGON_REDIRECT_PAGE</param-name>
<param-value>/logonRequired.do</param-value>
</init-param>
</filter> -
Re: Redirect struts action from jsp[ Go to top ]
- Posted by: benjamin thomas
- Posted on: November 14 2005 18:23 EST
- in response to A. Ritchie
Hi, right now I have: <% if(session.getAttribute("verified") == null){ response.sendRedirect("../PortalLogin.jsp"); return; } %>Inside of a jsp page. Basically if the user isn't verified, it redirects to the login page.Well, I have a struts action (a global forward) that forwards to the login page and i would like to be able to call the struts forward from within every jsp page rather than do the response redirect. Can this be done, and how? Thanks!
Use the struts redirect tag in logic library. The scriplet in a previous thread also would work
http://struts.apache.org/struts-el/tlddoc/logic/redirect.html
Thanks,
Benjamin
http://www.benjaminthomas.info -
thats the ticket[ Go to top ]
- Posted by: A. Ritchie
- Posted on: November 15 2005 10:26 EST
- in response to benjamin thomas
That's what I've been looking for guys, the logic:redirect tag. I will try it out and see if it does the trick. Thanks! -
thanks again[ Go to top ]
- Posted by: A. Ritchie
- Posted on: November 16 2005 09:16 EST
- in response to A. Ritchie
Thanks again, appreciate the help.
"The CubeSlacker"
-www.CubeSlacker.com