aspose file tools*
The moose likes Servlets and the fly likes redirect implementation with URL class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "redirect implementation with URL class" Watch "redirect implementation with URL class" New topic
Author

redirect implementation with URL class

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1963
    
    7

Hello I want to redirect/open a webpage using the URL class. Would do the trick?


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Don't know. Not sure you are in the best part of the forum for this question. Anybody else: should I move this thread?

But your code looks as if it would work: try it. Remember it will declare lots of checked Exceptions.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1963
    
    7

Nope tried it not doing anything.

The purpose is really when a session timeout, I want to auto-logout (got that working) THEN redirect to say login page automatically without user clicking anything.

by the way I'm using a regular java class implementing the HttpSessionListener interface, with sessionDestroyed() method taking HttpSessionEvent as parm. So technically I'm not using or have access to HttpServletResponse or its sendRedirect() method.

Yet if I did have access to HttpServletResponse (say make it into a servlet by extending HttpServlet) then ... tried that too but got a "java.lang.IllegalStateException: ServletConfig has not been initialized
" exception. I did

Any idea on how to initialize the ServletConfig?
[ June 19, 2008: Message edited by: K. Tsang ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
No, I don't know, sorry. I shall try moving you to the Servlets forum; you might have a better chance of an answer there.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

To redirect, you would use response.sendRedirect().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1963
    
    7

Originally posted by Bear Bibeault:
To redirect, you would use response.sendRedirect().


Yes but I DON'T have a HttpServletResponse instance. From my previous posts if I extend the HttpServlet class (make it into a servlet), I still need an instance of HttpServletRespose in order to call doGet or doPost and tell its response to redirect for me.

Similarly, if I put everything in doGet or doPost then i need to activate or trigger it (put in the web.xml) which my intention isn't that. When session "timeout" calls sessionDestroyed() of HttpSessionLister interface (which I auto logout) then redirect to login page directly WITHOUT user clicking anything. By the way when user does click it checks for session variable through getAttribute if its null goto login page.

Therefore I want to see if URL class or other classes in java.net package will allow to implement redirect.

Better is it possible to insert javascript within java classes WITHOUT PrintWriter ... cos can't do response.getWriter().

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Not possible. You can't initiate a response without a request.

How would you even know if the user was looking at your site when the session times out?

What most people do is to place an object of some type in the session and add a filter to their web app. The filter checks for the object, and if it's not there, you know that the session had previously timed out.

Alternatively, you could place an object into the session in your listener to let the filter know there's been a timeout, but that's not as common.
[ June 19, 2008: Message edited by: Bear Bibeault ]
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1963
    
    7

OK maybe codes will make things clear. first some logic:
user logins and idle till session timeout

In web.xml I have a listener thing
<listener>
<listener-class>listener.SessionListener</listener-class>
</listener>

In my SessionListener I have


As you can see it doesn't need to be a servlet hence no HttpServletResponse instance. If I did suppose I extend HttpServlet at the top and have those doGet or doPost method and put the sendRedirect there... I still need an HttpServletRxxx instance to call the doGet/doPost
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

No, that won't work either. You can't fake a response to a request that doesn't exist no matter what you extend.

You will not be able to do what it is you are trying to accomplish. Please read my response again for things that can work.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2711
    
    5

Hi just curious that if he extends from Httpservlet, then on invocation of that servlet wont the container provide the request and response objects.


Thanks in advance.


SCJP, SCWCD.
|Asking Good Questions|
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Originally posted by Amit Ghorpade:
... then on invocation of that servlet wont the container provide the request and response objects.
He has stated many times that this is not being invoked as a servlet.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: redirect implementation with URL class
 
Similar Threads
how to call servlet from struts application
redirect within a servlet
How do you pass a string from servlet to jsp if jsp is on another server?
How to Implement Post Redirect Get in Struts 2
How to redirect to a new url from Action class with post method?