Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to redirect page

 
Gaurav Chhabras
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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

1)a.jsp (Login Page)
2)b.jsp
3)c.jsp
4)d.jsp
5)e.jsp

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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
response.sendRedirect(url);
=========================================================================

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
Marshal
Pie
Posts: 64712
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic