Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Interesting problem in Forwarding request dispatcher

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 27278
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here.

if (username == null || username == "")

To compare the contents of two strings, call the equals() method like this:
 
Tiger Dipu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Tiger Dipu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic