1

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

brain storm
  • 30,124
  • 69
  • 225
  • 393

2 Answers2

1

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" />
jmj
  • 237,923
  • 42
  • 401
  • 438
  • I am setting the property in `setBean.jsp`. After setting it, I am forwarding to `displayinfo.jsp`. I know it is very confusing and complex. but just for learning purposes. – brain storm Jul 10 '14 at 00:24
  • you aren't setting `wu`'s property with proper names – jmj Jul 10 '14 at 00:24
  • @brainstorm If you don't want to use the core tags then use a JSP container and just use JSP EL, e.g., `${userId}`. – Dave Newton Jul 10 '14 at 00:26
  • @Dave that is little risky to use directly without trusting its content – jmj Jul 10 '14 at 00:27
  • @JigarJoshi Yes, I made some assumptions about the value contained in the user ID and password. I assume that dev's are performing due diligence on their input data, however, until shown otherwise. – Dave Newton Jul 10 '14 at 00:30
  • @JigarJoshi: correct me: you have indeed used EL above with `value=${userId}`? – brain storm Jul 10 '14 at 00:32
  • yes I have but it is internal at server side, that is secure and fine, It gets risky if you directly try to output only – jmj Jul 10 '14 at 00:34
  • and probably what you suggested might not work because my request **parameter** does not have `userId` but I stupidly try to setAttribute of `userId` on request, which the bean will not consume... – brain storm Jul 10 '14 at 00:34
  • having said that, if I have renamed my form input elements to have the same names as bean property, then what you suggested will work perfectly – brain storm Jul 10 '14 at 00:36
  • it will work because you are reading it with right parameter names and setting it in request as attributes – jmj Jul 10 '14 at 00:36
  • when you say set explicity, do you mean here `setBean.jsp`. I am confused now. will $(userId) be fetched from request **parameter** or request **attribute**? – brain storm Jul 10 '14 at 00:38
  • `$(userId) ` will be fetched as an attribute, I see the code where you are reading them from your login form parameter and setting it to request attribute in your `servlet` – jmj Jul 10 '14 at 00:39
  • so how would I fetch request parameter in EL? I mean is there a syntax for it or is it simply using `request.getParameter()`. the way I did above using "*" works only when the request parameter is set but Not request attributes correct?? – brain storm Jul 10 '14 at 00:43
  • You are submitting a form to servlet, at servlet you are fetching request parameters and setting it to request attribute by code, and now you are forwarding your request to another jsp - this is the flow, you don't need to fetch request parameter in side jsp because you are already posting and fetching it in servlet – jmj Jul 10 '14 at 00:44
  • I understand what you say. But I am just wondering if at all it is possible to get requestParameter using EL? just like how you used $(userId) for fetching request attribute. – brain storm Jul 10 '14 at 00:46
  • 1
    yes you can get request parameter in el by `${param.paramName}` – jmj Jul 10 '14 at 00:50
1

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.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I know I am doing it this in a complex manner. but if you notice, in the servlet, I get these parameters from request and set the new parameters in request for correct names for beans to handle. This should in theory work ? – brain storm Jul 10 '14 at 00:23
  • @brainstorm Don't confuse request parameters and request attributes. The `setProperty` seems to get request **parameters**. Yours are named differently than the bean's property names. – Sotirios Delimanolis Jul 10 '14 at 00:23
  • I got confused with parameter and attributes on request. And you corrected me saying that setPropery uses request parameters. I guess there is no way to set **request parameter** and is probably not useful right? – brain storm Jul 10 '14 at 00:29
  • @brainstorm When you submit a `
    `, its elements are submitted as form parameters which a servlet container translates to **request parameters**. Just change their names appropriately and the `setProperty` element will retrieve them correctly to then set them on the bean.
    – Sotirios Delimanolis Jul 10 '14 at 00:31
  • what will happen if you add another parameter in form and don't intend to update bean with new property ? – jmj Jul 10 '14 at 00:43
  • @Jigar Do you mean adding a new `` but not having a `wtv` property in the bean? I believe `setProperty` will look through all the bean's properties and try to find matching request parameters. – Sotirios Delimanolis Jul 10 '14 at 00:47
  • yes but its not precisely specified, what if it don't find matching property – jmj Jul 10 '14 at 00:49
  • I think it is clear enough to just ignore it. I have a similar case here. my bean has three property - uname,pwd, auth_level. but I set only two of them in the bean and it does not report any error – brain storm Jul 10 '14 at 00:55
  • @Jigar I'm trying to find how it's handled, but I would think it skips it. – Sotirios Delimanolis Jul 10 '14 at 01:02
  • @Jigar Yeah, I give up on finding it. Should be pretty simply to test but I don't have the env right now. – Sotirios Delimanolis Jul 10 '14 at 01:29