• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, there's no concept of a separate "response filter". A servlet filter wraps the entire request/response.
 
reply
    Bookmark Topic Watch Topic
  • New Topic