my dog learned polymorphism*
The moose likes Servlets and the fly likes sendRedirect method not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect method not working" Watch "sendRedirect method not working" New topic
Author

sendRedirect method not working

praveen pillai
Greenhorn

Joined: Feb 09, 2005
Posts: 11
Hi,
I am using Websphere 4.0.6 to host my application involving servlets and jsps.
But when I invoke response.sendRedirect method from the servlet class, to an HTML page, I get a HTTP 404 Not Found error. Also, no error message is thrown on the server console.
The same code works on Websphere 5.1.1

I even tried using both the absolute and relative paths as argument to the sendRedirect method, but no change in the response.

Please let me know the cause of this error.
Thanking in advance,
praveen.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Post the relevent code.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
praveen pillai
Greenhorn

Joined: Feb 09, 2005
Posts: 11
I tried running this code in debug mode and it is completing normally but without any response.

public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

PrintWriter out = null;
HttpSession session = null;

try {
out = resp.getWriter();
String source = req.getParameter("source");
String action = req.getParameter("action");

UserLoginBean userBean = null;

if (source != null) {
if (source.equals("login")) { //validating the login
String loginId = req.getParameter("LoginID");
String password = req.getParameter("Password");
ValidateLogin vLogin = new ValidateLogin();
boolean validLogin = vLogin.validate(loginId, password);
if (validLogin) {
session = req.getSession(true);
session.setAttribute("loginID", loginId);
boolean firstLogin = vLogin.firstLogin;
if (!firstLogin) {
ViewSchedule viewSchedule = new ViewSchedule();
ArrayList courseList = viewSchedule.getCourseDetails();
session.setAttribute("CourseList", courseList);
UserLoginBean userLoginBean = new UserLoginBean(loginId);
session.setAttribute("userloginbean", userLoginBean); resp.sendRedirect("/TrainingNomination/HTML/home.html");
}
}
}
}
} catch (Exception e) {
LOG.error("Exception occurred ", e);
} finally {
if (out != null) {
out.close();
}
}
}
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Is 'TrainingNomination' the name of your context?
If not, you need to remove the leading slash '/' from the relative url.
praveen pillai
Greenhorn

Joined: Feb 09, 2005
Posts: 11
Yes Ben.
The context root is TrainingNomination
I am able to access the resource by explicitly entering the URL
http://localhost:8080/TrainingNomination/HTML/home.html in the browser
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I don't know if this is affecting something but, why are you explicitly closing the printWriter?

Also, it's a good habit to add a 'return' statement just afer your call to sendRedirect.

Try removing the out.close() statement and adding the return statement.
The 404 error might be an indirect result of something else going wrong in your servlet.
praveen pillai
Greenhorn

Joined: Feb 09, 2005
Posts: 11
Hi Ben,
Thanks for your replies.
But I just tried commenting the out.close() call and also including a return statement after sendRedirect, but still am unable to get any response on the browser.
As I had mentioned before, I tried debugging the code and it seems to flow properly without throwing any exceptions.
Has this problem got anything to do with WebSphere 4.0.6 because the same code is working perfectly fine on WebSphere 5.1.1

Please let me know your response.
Thanks
Praveen.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I don't use websphere.

You may want to move this thread to the IBM/Websphere forum.
I can't see anything wrong with your code that would cause this.
praveen pillai
Greenhorn

Joined: Feb 09, 2005
Posts: 11
Thanks Ben.
I have moved this question to the WebSphere forum.
But if anybody has any inputs to share on this topic, please do so.
Thanks
praveen.
praveen pillai
Greenhorn

Joined: Feb 09, 2005
Posts: 11
Hii
I have made some progress in atleast getting the code in a working state, but this was possible only when I provide the absolute URL as argument to the sendRedirect method. i.e.
resp.sendRedirect("http://localhost:8080/TrainingNomination/HTML/home.html");

But the Servlet API states that relative URL can be provided, which would be converted into absolute URL before sending the response to the client.

If anybody could throw some light on why it fails for relative URLs and not for absolute URLs, please do so.
Thanks
praveen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sendRedirect method not working