以下是我在 jsps 代码中遵循的步骤: 1) 登录页面并将其发布到 servlet。
2)在servlet中,我正在设置一些请求属性并使用调度程序将请求转发到另一个以设置bean属性。
3)bean设置属性并转发到不同的url。
4) 在这个 url 中,我从请求中提取属性以显示在页面上。我从null
请求中得到一个,表明我的属性未按请求设置。也就是displayinfo.jsp
下面显示Welcome Null。为什么没有设置属性?
这是我的代码:
Login page:
<form id="login" method="post" action="setBeansAllPropertiesLoginUser.do">
<span>UserName:</span><input name="uid" type="text" style="width:250px;" />
<span>Password:</span><input name="pwd" type="password" style="width:250px;"/>
</form>
servlet:
@WebServlet("/setBeansAllPropertiesLoginUser.do")
public class SetBeansAllPropertiesLoginuser extends HttpServlet {
public SetBeansAllPropertiesLoginuser() {
super();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uid = request.getParameter("uid");
String pwd = request.getParameter("pwd");
request.setAttribute("userId", uid);
request.setAttribute("password", pwd);
//verify login details
int authLevel = 1;
String base = "setBean.jsp";
/*
String params = String.format("?userId=%s&password=%s&authLevel=%d"
, uid, pwd, authLevel);
*/
String dest = String.format("%s%s"
,base
,params);
//RequestDispatcher rd = request.getRequestDispatcher(dest);
RequestDispatcher rd = request.getRequestDispatcher(base);
rd.forward(request, response);
}
}
setBean.jsp
<table style="width:100%;">
<tr>
<td style="width:25%;height:80%;" valign="top">
<jsp:include page="navbar.jsp" />
</td>
<td style="width:75%;height:80%;">
<jsp:useBean id="wu" class="com.worldmanager.models.WebUser"
scope="request">
<jsp:setProperty name="wu" property="*" />
</jsp:useBean>
<jsp:forward page="displayinfo.jsp" />
</td>
</tr>
</table>
displayinfo.jsp
:
<table style="width:100%;">
<tr>
<td style="width:25%;height:80%;" valign="top">
<jsp:include page="navbar.jsp" />
</td>
<td style="width:75%;height:80%;">
<jsp:useBean id="wu" class="com.worldmanager.models.WebUser" scope="request"/>
<h1>Welcome
<jsp:getProperty name="wu" property="userId" />
</h1>
</td>
</tr>
</table>
我bean
的是正确的。我测试了它。上面我粘贴了相关的代码。它不是完整的代码