Here are the steps I follow in my jsps code: 1) login page and am posting it to servlet.
2) In the servlet, I am setting up some request attributes and forward the request using dispatcher to another to set bean property.
3) The bean set the property and forwards to a different url.
4) In this url, I pull the attributes from the request to display on page. I get a null
out of the request, indicating that my attribute is not set on request. That is the displayinfo.jsp
below displays Welcome Null. why the attribute is not set?
Here is my code:
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>
My bean
is correct. 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
Or
Set wu
's property explicitly
<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" />