aspose file tools*
The moose likes Other Application Frameworks and the fly likes How to redirect to a servlet when accessing an HTML page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "How to redirect to a servlet when accessing an HTML page" Watch "How to redirect to a servlet when accessing an HTML page" New topic
Author

How to redirect to a servlet when accessing an HTML page

Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 146
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?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

Usually this sort of thing is handled by a servlet filter.
Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 146
Can I use servlet filter with the design that I have?I mean I do not have any servlets as of now.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

A filter can be configured to match any URL pattern.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 146
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
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

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

Joined: Jun 19, 2004
Posts: 146
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
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

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

Joined: Jun 19, 2004
Posts: 146
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.
 
 
subject: How to redirect to a servlet when accessing an HTML page