• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create a "Check Login" script which sends a response?

 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a login java file which works fine, although I want to use "response.sendRedirect(somepage.jsp)" to forward the user on if they are not authorised to view this page.

Although since I am calling this java file from a servlet, I am getting the error "Cannot forward after response has been committed" when I try to view a page I shouldn't when not logged in.

This is due to me having a "response.sendRedirect()" in the java file and also one in the servlet.

Since there are going to be several servlets using the "check login" script I needed to put it into a separate file so it saves repeating loads of code in each servlet.

How can I do what I have done so it will work correctly?

Thanks
Michael
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should be something that you check independently of any servlets in a filter. That way, you (a) don;t have a bunch of repeated code in each servlet checking authentication, and (b) the filter can make its decision prior to any servlet deciding to emit output (after which the response becomes committed).
 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick response.

I have not used filters before although just had a quick read up on them and it seems like I would put them in the Document Descriptor for all URLs containing /restricted/*

Then what happens whenever someone tries to access one of those files, the request first gets passed through a java file which does all the authentication checking based on the HttpSession data & redirects the user if they are not authorised.

Have I got the gist of that correctly?

Sounds simple enough..... now time to give it a go :-)

Thanks
Michael
 
Paul Clapham
Sheriff
Pie
Posts: 20729
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Cropper wrote:Have I got the gist of that correctly?

Yup.
Sounds simple enough..... now time to give it a go :-)

Let us know how it works out.
 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let us know how it works out.


Worked out great Only took about 1 hr to get it all working correctly.

Noticed that there is such a thing as a 'response filter' too which sounds pretty cool, so ill have to read up on that too.

Thanks
Michael
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there's no concept of a separate "response filter". A servlet filter wraps the entire request/response.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic