This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Servlets and the fly likes RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher" Watch "RequestDispatcher" New topic
Author

RequestDispatcher

Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
Is this right?

public class Login extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{
String user, pass;
boolean response = false;

user = req.getParameter("username");
pass = req.getParameter("password");

DatabaseChecker ct = (DatabaseChecker)(getServletContext().getAttribute("ct"));
response = ct.validate(user,pass);

if(response == true)
{
HttpSession userSession = req.getSession(true);
userSession.putValue("userName", user);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/Welcome.jsp");
if (rd != null)
{
rd.forward(req,res);
}
}
else
{
//Return to LoginPage.
RequestDispather rd2 = getServletContext().getRequestDispatcher("/Login.jsp");
rd2.forward(req,res);
}
}//end doGet.
}//end Login.
[ March 24, 2004: Message edited by: Lucky Singh ]
Kaustubh Patil
Ranch Hand

Joined: Aug 13, 2001
Posts: 164
r u having any problems in executing it ?


Kaustubh. Mumbai, India.
Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
Would like to knwo if it is correct in syntax.
Kate Head
Ranch Hand

Joined: Oct 31, 2002
Posts: 67
Hi there,
I was told to put return; after the call to rd.forward(req, resp);
I also used String nextScreen to hold the "/Welcome.jsp" or "/Login.jsp" names so used the following format:

Hope that helps.
All the best,
Kate!!
[ March 24, 2004: Message edited by: Kate Head ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: RequestDispatcher
 
Similar Threads
HF Final Mock Q 56
Could not get session
problem using requestdispatcher interface
Pass session from JSP to servlet?
Interesting problem in Forwarding request dispatcher