All Things Techie With Huge, Unstructured, Intuitive Leaps
Showing posts with label how to redirect. Show all posts
Showing posts with label how to redirect. Show all posts

JSP ~ Login before going to bookmark or URL

So you have a web app or website in JSP where you want the user to login before proceeding to a bookmarked page or a URL. And you want to proceed to that page or URL automatically after login. How do you do it?

For me, every protected JSP page has an include page:

<%@ include file="isLoggedIn.jsp"%>

That file, isLoggedIn.jsp is called on every page. In that file, the first thing that I do, is I get the request URL. I do it like this:

String redirectUrl = request.getRequestURL().toString();

Then is there is a query string afterwards (www.mysite.com?showMe=cars -- the query string is showMe=cars), you need to get the query string in a separate step:

String queryString = request.getQueryString();

Since the queryString doesn't have the question mark, you need to re-assemble the called URL:

redirectUrl = redirectUrl +"?"+queryString;

Now I usually have a session attribute called "AccessGranted" or "isLoggedIn". I check for that:

String accessGranted = (String) session.getAttribute("AccessGranted");

if (accessGranted.equalsIgnoreCase("false"))
response.sendRedirect("login.jsp?NotLoggedIn=True&requrl="+redirectUrl);


What you see above, is that I send the request URL to the login page.

On the login page, if the login is successful, I redirect to a menu page. Otherwise, I redirect to the URL that was called in the first place. I do it this way:

String redirectURL = (String) request.getParameter("requrl");
if (redirectURL == null)
redirectURL = "menuPage.jsp";

If the login fails, then:

redirectURL="login.jsp";

Finally I send the user to where they have to go with the jsp redirect directive:

response.sendRedirect(redirectURL);


Easy as pie.


Java Server Faces Redirect

I have been created a JSP/JSF web application and a certain aspect of it was coded in XHTML using JSF and Facelets. It has been giving me no end of grief. On every page, I do a check of a session attribute to see if the person is logged in or not. It is a simple check. I have a page that is included with every JSP page that contains a little scriplet that queries the session attribute. If an attribute called "Access Granted" is true, then the person is logged in and gets to view the page. If not, they are re-directed to the index page which is the login page.

Here is the normal code that works great as a scriplet:

<%

//If a user bookmarks a page, then this check is performed to see
// if they are properly logged in. If not they get re-directed
// to the login page.

// The way that it is done, is that the login validation sets an
// Access Granted attribute.

String accessGranted = (String) session.getAttribute("AccessGranted");
if(accessGranted == null)
accessGranted = "false";
if(accessGranted.equalsIgnoreCase("false"))
response.sendRedirect("index.jsp?NotLoggedIn=True");
%>

Simple stuff. Then I had a page coded in XHTML and I used the ui:include tag:


The tag worked, but the page didn't. The scriptlet tag <% some code ... %> fails because it is not well formed html. It is not closed like a regular tag "/>".

So I rewrote the page in well-formed XML. I added the JSTL jar so that I would have access to the c:if conditional tag and the c:redirect tag. One can always get a session attribute with the following syntax:

"#{sessionScope['attribute_name']}"

OK, so I use the conditional tag in the jstl core to see if the session attribute was true, and if not, then re-direct to the index page. Piece of cake, right? Wrong.

After dicking around for a long time, I had an error message that said something like the tag was defined in "http://java.sun.com/jsp/jstl/core", but no class could be found. It was after a bit of hair pulling that I found out that the c:redirect tag was not supported by facelets.

A lot more dicking around ensued until I found this javascript work-around that enables a JSF redirect. Here is the code in its entirety:




Having a problem as this blog wants to parse the tags in the code. Replace '<' with < and '>' with >.




'<'ui:composition
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html" '>'

'<'c:if test="#{!sessionScope['AccessGranted']}" '>'

'<'form jsfc="h:form" id="redirect"'>'
'<'a jsfc="h:outputLink" value="#{'index.jsp'}" id="path" '>''<'/a'>'
'<'script type="text/javascript"'>'
var link = document.getElementById("redirect:path");
location.replace(link);
'<'/script'>'
'<'/form'>'
'<'/c:if'>'

'<'/ui:composition'>'



It works perfectly. However, JSF is supposed to uncomplicate your coding life. It just did the opposite for me.