Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sendRedirect method not working

 
praveen pillai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
 
praveen pillai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is 'TrainingNomination' the name of your context?
If not, you need to remove the leading slash '/' from the relative url.
 
praveen pillai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic