• Post Reply Bookmark Topic Watch Topic
  • New Topic

Strange Problem : Redirect to other Servlet using dispatch

 
Ken Shamrock
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've a servlet which will check login session first, if not logged in, it will redirect to LOGIN servlet using "req.getRequestDispatcher(forwardTo).forward(req, resp);", it works fine. For eg, if I press ServletA it will redirect to LoginServlet. It works fine IF I DON'T RESTART TOMCAT.
However, if I restart tomcat OR restart tomcat and close browser, then request ServletA again, it will display error:
Apache Tomcat/4.0.1 - HTTP Status 404 - /sun.userlogin
type Status report
message /sun.userlogin
description The requested resource (/sun.userlogin) is not available.
BUT if I press RELOAD, it will become normal and REDIRECT me to LoginServlet. It's very strange and it troubles my development. The following is a part of my code, do you have any idea what's going wrong?Thanks.
public class viewMyAcct extends HttpServlet
{
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, java.io.IOException
{
// Set the content type of the response
resp.setContentType("text/html");
// Create a PrintWriter to write the response
java.io.PrintWriter out = resp.getWriter();
HttpSession session = req.getSession(true);
if (session == null) {
out.println("ERROR: Internal servlet problem - no session");
out.flush();
out.close();
return;
}
UserLog ulog = (UserLog) session.getAttribute(USER);


cmd = new sqlcmd();
String uri = req.getRequestURI();

if (ulog == null) {

String forwardTo = "sun.userlogin?where=RegForm"; //return to login page
//String forwardTo = "../"+uri.substring(0,uri.indexOf(".")) + ".userlogin?where=RegForm"; //return to login page
req.getRequestDispatcher(forwardTo).forward(req, resp);
//http://127.0.0.1:8080/user/servlet/student.userlogin
}

//here sure have login,get user loginid
else{
...
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can save yourself a lot of grief by looking a FORM-based authentication. You set it up in Tomcat (and other app servers) via configuation settings and it takes care of everything you are trying to accompish. Have a look at (I think its called) the Realm-HOWTO in the Tomcat docs.
 
Rishi Singh
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ken,
The trouble starts when u restart tomcat,the servelet which is there in memory gets removed so the next time it has to cater to a request it has to go through init(), before getting into the service(), so when u restart and press for Re-login page,it has still not compiled, which it does on getting the first request and hence when u reload it, it responds
Rishi Singh
SCJP,SCWCD
 
Ken Shamrock
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David O'Meara and Rishi Singh
So do you mean that the problem is not come from the program but tomcat itself, since time is limited so i don't have time to look at some doc in tomcat. So do I need to solve this by changing the config of tomcat? Thanks
 
Ken Shamrock
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David, the Realm things seems not too suitable (if i understand correctly), as my username and password is already in database
Rishi Singh , I understand why this problem occurs, but do u have any solution for this?Thanks
 
Ken Shamrock
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,do anyone got some solutions? if not, i had to use javascript to redirect but i don't want to do so,thanks.
(Seems not too many people use redirectdispatch in servlet? otherwise they should notice this problem and I should be able to hear solution quickly)
 
Rishi Singh
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ken,
why do u need to restart tomcat, when ur ServletA uses requestDispatcher.forward() and forwrads it to the Login Servlet...suppose that is envitable, then I guess u need to maintain a globals properties file, in which the username,password entry is already hard-coded, then if u restart tomcat it has to read the properties file first before compiling the serveletA and it should not give u the error....u can try this...hope this solves ur problem
 
Ken Shamrock
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rishi,
Do you mean that username and password are saved in a properties file? But the user info are saved in data base now, maybe I don't know the actual flow of your solution. Or do you mean that by making a properties file storing the init servlet?
About restarting tomcat, every company or hosting would restart it for a period of time, so if users click this, an error will be displayed.
 
Sunit Mukherjee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
make tomcat call init on your servlet as it starts up..that will fix it
 
Ken Shamrock
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sunit,
Do you know or do you mind to tell me how to set tomcat to init the servlets? I browser through the setting and i've no idea about it,thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!