• 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 redirect to a servlet when accessing an HTML page

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guyz,

First I apologize if I posted it in wrong forum.

I have an HTML page which is controlled by jquery i.e. I have implemented jquery specific syntax to read "click" of a button.So there is no Java controller (i.e. Spring controller or a servlet) involved here.For data fetching and updating I am using DWR(direct web remoting) framework calls.
Now there is a requirement to force users to redirect to a Login screen - that will just have username,password and submit button.So anytime user accesses the HTML page without authentication(meaning cookies are not there in browser),he should be forced to enter username and password.
In effect,when user accesses http://hostname/context/management.html without authenticating himself,he should be directed to http://hostname/context/login.do .After entering correct authentication details and pressing submit button,he should again be taken to http://hostname/context/management.html .

What is the best way to do it ?is it even possible with the current design that I have in place?
 
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
Usually this sort of thing is handled by a servlet filter.
 
Jitesh Sinha
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I use servlet filter with the design that I have?I mean I do not have any servlets as of now.
 
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
A filter can be configured to match any URL pattern.
 
Jitesh Sinha
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thanks Bear.

So I can configure a filter for my url pattern.
Now my next question is how I can use a filter to show a JSP or an HTML page(in this case login page)?
I know these questions are very basic but I have never done this before.
Appreciate your patience.
 
Paul Clapham
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

Paul Clapham wrote:Usually this sort of thing is handled by a servlet filter.



You have observed that I didn't post a lot of detail explaining what a servlet filter was or how to write one. That's because if you google for "servlet filter tutorial" you will find... some servlet filter tutorials. There is no point in me writing a servlet filter tutorial for you personally, because somebody else has already done that and put them on the Internet where everybody can find them. And they have done a better job than I would have.

So don't just sit there waiting. There's a ton of information on the web free for you to find.
 
Jitesh Sinha
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.
I have been looking at filter tutorial since yesterday - but all examples end with this line of code -

chain.doFilter(request, response);

I need filters to show an HTML or JSP page(login screen).
In my code there is no controller so I cannot set an attribute in request in filter code and read it afterwards .


 
Paul Clapham
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
I don't understand. The filter will run because a request for a particular servlet or URL was received. Calling the chain.doFilter response simply causes that request to be processed in the normal way.

So yeah, it's true that setting a request attribute would be pointless if the request was an HTML page, since it couldn't do anything with that attribute. But don't any of the examples in your tutorial involve forwarding or redirecting to some other page? That would be the normal way to make your users go to the login page instead of whatever they asked to go to.
 
Jitesh Sinha
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

But don't any of the examples in your tutorial involve forwarding or redirecting to some other page? That would be the normal way to make your users go to the login page instead of whatever they asked to go to.



Ok thanks Paul - I think this was the hint I needed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic