aspose file tools*
The moose likes JSP and the fly likes A forward in a include page? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "A forward in a include page?" Watch "A forward in a include page?" New topic
Author

A forward in a include page?

Aslan Namdar
Greenhorn

Joined: Oct 04, 2005
Posts: 5
Hi All

This is my case:
I have a top.jsp file which I include in every page. It not only has all the stuff of the page header and all the links it also contains a part which does a security check. Meaning if someone�s session is expired or for any other reason does not have a valid session to view that page I want to redirect them to somewhere else.

I have tried this in many ways and have not succeeded. I get errors such as I can not redirect to a new page since the server has committed to a response. I know there are better ways to handle the security check but out of curiosity am wondering is there any way to accomplish this task?

Any suggestions?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61214
    
  66

"Aslan",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
we can go this way simply.

> user logs in
> after a valid login we place a boolean variable, say isValid, in his session
> upon each and every request we used to check that variable from session, whether it is true or null
> if true continue
> otherwise redirect to the login page

You can use requestDispatcher.forward(request, response) method to forward the request to login page.
Aslan Namdar
Greenhorn

Joined: Oct 04, 2005
Posts: 5
Thanks Adeel

Maybe I wasn’t very clear explaining myself. The question is if we have a include page such as a top.jsp or header.jsp can we have any form of forward or redirect inside that include page?

I have some experience with coldfusion and this task is simply done. In any <cfinclude > you can have a <cflocation > since coldfusion has a java core I was wondering how can we achieve the same in jsp?


Aslan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61214
    
  66

Using a servlet filter would be a better means to accomplish this.
Aslan Namdar
Greenhorn

Joined: Oct 04, 2005
Posts: 5
>Using a servlet filter would be a better means to accomplish this.

filters work on a different level. what i am asking is :

> if we have a include page such as a top.jsp or header.jsp can we have any form of forward or redirect inside that include page?


Aslan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61214
    
  66

What happened when you tried it?
Aslan Namdar
Greenhorn

Joined: Oct 04, 2005
Posts: 5
Depends on type of includes I use, on some it doesn’t redirect the page but as soon as it gets to that condition on which it is suppose to redirect it stops. So I get the page to that point and nothing after that.
The other case is that it throws and error saying that the server is already making a response page and you cant forward or redirect to another page.
Aslan Namdar
Greenhorn

Joined: Oct 04, 2005
Posts: 5
anyone?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61214
    
  66

A redirect will cause an illegal state exception if it is issued after the response has been committed. So including a JSP that contains one after some output has been generated is an iffy thing to do. Depending upon the state of the output buffer, you may get an exception or you may not.

Best practice is to factor out any forwarding or redirecting decisions to the servlet controller for the page. If you are still including scriplets on your pages, at least make such decisions at the beginning of the page before any output can be emitted.

Other than that, a redirect within an include is ignored.
[ January 30, 2006: Message edited by: Bear Bibeault ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A forward in a include page?