aspose file tools*
The moose likes Servlets and the fly likes Interesting problem in Forwarding request dispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Interesting problem in Forwarding request dispatcher " Watch "Interesting problem in Forwarding request dispatcher " New topic
Author

Interesting problem in Forwarding request dispatcher

Tiger Dipu
Greenhorn

Joined: Apr 09, 2006
Posts: 4
My code:

public class MessageCenterLoginServlet extends HttpServlet {
/**
* @see javax.servlet.http.HttpServlet#void
(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String username = req.getParameter("username");
if (username == null || username == "") {
req.setAttribute("errorMessage", "User name not specified");
RequestDispatcher disp =
getServletContext().getRequestDispatcher("/login.jsp");
disp.forward(req, resp);
}
//Otherwise proceed to looking for the User object

User user = User.getUser(username);
if (user == null) {
System.out.println("No such user found");
req.setAttribute("errorMessage", "User not found");
RequestDispatcher disp =
getServletContext().getRequestDispatcher("/login.jsp");
disp.forward(req, resp);
}

HttpSession session = req.getSession(true);
session.setAttribute("user", user);
RequestDispatcher disp =
getServletContext().getRequestDispatcher("/messageCenter.jsp");
disp.forward(req, resp);

}
}
}

**************

Problem:
when I execute above programme, if I enter wrong data or null data:
It generates:
[Servlet Error]-[MessageCenterLoginServlet]: java.lang.IllegalStateException: Cannot forward. Response already committed. (Servlet 2.3, SRV 8.4)
**************
I believe if it is going in 1 loop and execute forward(), it should pass control to jsp page and should not execute further. But it continues and execute remaining statements too.

can anyone tell me where is the catch?

Here, if i chance code following, it works:


public class MessageCenterLoginServlet extends HttpServlet {
/**
* @see javax.servlet.http.HttpServlet#void
(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String username = req.getParameter("username");
if (username == null || username == "") {
req.setAttribute("errorMessage", "User name not specified");
RequestDispatcher disp =
getServletContext().getRequestDispatcher("/login.jsp");
disp.forward(req, resp);
}
//Otherwise proceed to looking for the User object

User user = User.getUser(username);
if (user == null) {
System.out.println("No such user found");
req.setAttribute("errorMessage", "User not found");
RequestDispatcher disp =
getServletContext().getRequestDispatcher("/login.jsp");
disp.forward(req, resp);
}

HttpSession session = req.getSession(true);
session.setAttribute("user", user);
RequestDispatcher disp =
getServletContext().getRequestDispatcher("/messageCenter.jsp");
disp.forward(req, resp);

}
}
}
[ June 09, 2006: Message edited by: Tiger Dipu ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Here.
if (username == null || username == "")
To compare the contents of two strings, call the equals() method like this:
Tiger Dipu
Greenhorn

Joined: Apr 09, 2006
Posts: 4
Hi Paul,

thanks for quick reply. That mistake I hv corrected. However, my question was:

If you use dis.forward();
after executing jsp page, will control come back to servlet and execute remaining line of codes too?? or it will not come back.

Because in previous code, if i dont use if, else. it also performs dis.forward() again and create error...

kapil
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Originally posted by Tiger Dipu:

If you use dis.forward();
after executing jsp page, will control come back to servlet and execute remaining line of codes too?? or it will not come back.


What happened when you tried it?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tiger Dipu
Greenhorn

Joined: Apr 09, 2006
Posts: 4
Problem:
when I execute above programme, if I enter wrong data or null data:
It generates:
[Servlet Error]-[MessageCenterLoginServlet]: java.lang.IllegalStateException: Cannot forward. Response already committed. (Servlet 2.3, SRV 8.4)

Question:?
If I have forwarded to jsp page. Will it comeback to servlet and execute remaining code or it will not return and response will be completed by jsp page.

In my case, as mentioned in the code: First code
if() {
disp.forward()
}
perform some other operations when if failed...
disp.forward().

In above case, it executes if loop and also remaining code too. So generate above error.

Instead above,
if i change programme --->> It is working fine.
if() {
disp.forward()
}
else
{
disp.forward()
}

So my question is::---???
If I have forwarded to jsp page. Will it comeback to servlet and execute remaining code or it will not return and response will be completed by jsp page.
Harpreet Hira
Ranch Hand

Joined: Sep 27, 2001
Posts: 72
The forwarded request will NOT come back. The resource to which request has been forwarded will commit the response.

If you want the remaining code to be processed use include
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Originally posted by Harpreet Hira:
The forwarded request will NOT come back.


That is not correct. Of course it will return. Whether the reponse is committed or not has no effect on the semantics of the Java language. Servlets are Java classes like any other and, unless an exception is thrown, any calls will return to the caller.

What is true is that a forward call is almost always immediately followed by a return so that no code that might trigger an IllegalStateException will execute after the forward and the committing of the response.
[ June 09, 2006: Message edited by: Bear Bibeault ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interesting problem in Forwarding request dispatcher