It's not a secret anymore!
The moose likes JSP and the fly likes how to redirect page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "how to redirect page " Watch "how to redirect page " New topic

how to redirect page

Gaurav Chhabras
Ranch Hand

Joined: Sep 21, 2005
Posts: 126

I am making a project and in that i have done like i have 5 pages :

1)a.jsp (Login Page)

Now what i have done is like first user have to login then only he can access the remaining pages i.e. b.jsp , c.jsp , d.jsp , e.jsp.

Now like user enters c.jsp directly in the browser and he is not logged in then he will be redirected to a.jsp which is login page.

After successful login i want that user will redirected to the page he entered previously like to c.jsp and not b.jsp which is the next page after successful login.

It should go to c.jsp after successful login but not to b.jsp
samart mateo
Ranch Hand

Joined: Feb 06, 2006
Posts: 37
i would advise that u utilize jsp session. here's one example

put this in every page

<% //<!--
//response.setHeader("Cache-Control","no-cache"); //Forces caches to obtain a new copy of the page from the origin server
//response.setHeader("Cache-Control","no-store"); //Directs caches not to store the page under any circumstance
//response.setDateHeader("Expires", 0); //Causes the proxy cache to see the page as "stale"
response.setHeader("Pragma","no-cache"); //HTTP 1.0 backward compatibility
String userNameSess = (String) session.getAttribute("theName");
String urlR = request.getRequestURI();//saves the url that the user logs in but yet validated by the system

if (null == userNameSess) {
request.setAttribute("Error", "Session has ended. Please login.");//also applies to unvalidate user
request.setAttribute("url",urlR);//set url so that the page loads back to the previous page after re-login
RequestDispatcher rd = request.getRequestDispatcher("loginAgain.jsp");
rd.forward(request, response);


and in the login page, u shud have this...

String url = (String) request.getAttribute("url");
input type="hidden" name="url" value="<%=url%>

and finally in the login check page (the page where you posted the login name and password for validation)


String url =request.getParameter ("url");

//and after you have validate the user, do this

you can combine step 2 and 3 in the same page if you validate the user in the same page.

hope it answer your question
[ February 07, 2006: Message edited by: samart mateo ]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63838

You can use a servlet filter to check for login credentials and forward to the login page as appropriate. You can also grab the original url from the request and remember it (in the session, or perhaps in hidden elements) so that you can redirect to the original target after login.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: how to redirect page
jQuery in Action, 3rd edition