当我们尝试使用RequestDispatcher
从servlet转发请求到jsp时,我们用户request.setAttribute()
方法。
request.setAttribute("uname", name);
RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
rd.forward(request, response);
但是当我们尝试将来自servlet的请求转发给另一个servlet时,我们不会使用request.setAttribute()
方法,结果打印得很好。
RequestDispatcher dis=request.getRequestDispatcher("welcome");
dis.forward(request, response);
你能解开为什么会这样吗?谢谢。
的的login.jsp
<form action="login" method="post">
User Name:<input type="text" name="uname"/><br/>
Password:<input type="password" name="upass"/><br/>
<input type="submit" value="SUBMIT"/>
</form>
LoginDetails servlet
String name = request.getParameter("uname");
String pass = request.getParameter("upass");
if (name.equals("dilini") && pass.equals("123")) {
// ONLY ONE OPTION IS USING HERE
// for a jsp
request.setAttribute("uname", name);
request.getRequestDispatcher("welcome.jsp").forward(request, response);
// for a servlet
request.getRequestDispatcher("welcome").forward(request, response);
}