[英]unable to set attributes in requests in JSP/servlets - Java?
Here are the steps I follow in my jsps code: 1) login page and am posting it to servlet. 这是我在jsps代码中遵循的步骤:1)登录页面并将其发布到servlet。
2) In the servlet, I am setting up some request attributes and forward the request using dispatcher to another to set bean property. 2)在servlet中,我正在设置一些请求属性,并使用调度程序将请求转发到另一个以设置bean属性。
3) The bean set the property and forwards to a different url. 3)bean设置了属性并转发到另一个URL。
4) In this url, I pull the attributes from the request to display on page. 4)在此url中,我从请求中提取属性以显示在页面上。 I get a
null
out of the request, indicating that my attribute is not set on request. 我从请求中得到一个
null
,表明我的属性未根据请求设置。 That is the displayinfo.jsp
below displays Welcome Null . 那就是下面的
displayinfo.jsp
显示Welcome Null 。 why the attribute is not set? 为什么未设置属性?
Here is my code: Login page:
这是我的代码:
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
: 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>
My bean
is correct. 我的
bean
是正确的。 I tested it. 我测试了 Above I pasted the code that is relevant.
上面我粘贴了相关的代码。 It is not complete code
它不是完整的代码
Just change 只是改变
<jsp:getProperty name="wu" property="userId" />
to 至
<c:out value="${userId}" />
to read it directly from request attribute 从请求属性直接读取
by using <jsp:getProperty>
you are requesting wu.getUserId()
and you haven't set wu
's property in available scope 通过使用
<jsp:getProperty>
您正在请求wu.getUserId()
,但尚未在可用范围内设置wu
的属性
Or 要么
Set wu
's property explicitly 显式设置
wu
的属性
<jsp:setProperty name="wu" property="userId" value="${userId}"/>
<jsp:setProperty name="wu" property="password" value="${password}/>
and access it same way as you are doing now 并以与您现在相同的方式访问它
<jsp:getProperty name="wu" property="userId" />
According to this , the following notation 根据此 ,下面的符号
<jsp:setProperty name="wu" property="*" />
will retrieve and set properties based on request parameter names. 将根据请求参数名称检索和设置属性。 So change your request parameters from
因此,请更改您的请求参数
String uid = request.getParameter("uid");
String pwd = request.getParameter("pwd");
to 至
String uid = request.getParameter("userId");
String pwd = request.getParameter("password");
Obviously change your form
's input
parameter names as well. 显然也要更改
form
的input
参数名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.