小编典典

jsp useBean由servlet的getAttribute为NULL

jsp

servlet中的user为null。请让我如果做错了。

我正在尝试获取bean rateCode.jsp中的所有html元素

<%@page import="com.hermes.data.RateCode_" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title>Rate Code</title>
    </head>
    <body>      
         <jsp:useBean id="user" class="com.hermes.data.RateCode_" scope="request" >
            <jsp:setProperty name="user" property="*"/></jsp:useBean>
            <form  id="f_rateCode" action="/ratePromoCodes" method="post"  >
                <table align="center" border="1" cellspacing="0">
                    <tr>
                        <td colspan="2" align="center" class="header">Rate Code Administrations</td>
                    </tr>
                    <tr>
                        <td align="right" style="border-style: solid;">Rate Code:</td>
                        <td align="left" style="border-style: solid;">
                            <input type="text" id="code" name="code" value="${user.code}"  size="10" maxlength="32" style="width: 100px"/>
                    </td>
                </tr>

                <tr>
                    <td align="right" style="border-style: solid;">Rate Description:</td>
                    <td align="left" style="border-style: solid;">
                        <input type="text" id="description" name="description" value="<%=user.getDescription()%>" maxlength="128" size="40"></td>
                </tr>              
                <tr><td><input type="submit" value="ok" /></td> </tr>
            </table>
        </form>

Servlet-ratePromoCodes

protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        RateCode_ rc = (RateCode_) req.getAttribute("user");
        Enumeration an = req.getAttributeNames();
        Enumeration pn = req.getParameterNames();
        Object o = null;
        while (an.hasMoreElements()) {
            o = an.nextElement();
            System.out.println(o);
        }
        while (pn.hasMoreElements()) {
            o = pn.nextElement();
            System.out.println(o);
        }
    }

RateCode.java(javaBean)

public class RateCode_  {    
    private String code ;
    private String description;
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

阅读 323

收藏
2020-06-08

共1个答案

小编典典

您似乎误解了jsp:useBean

首先,您已声明该bean在会话范围内,并用当前请求的所有参数填充它。

<jsp:useBean id="user" class="com.hermes.data.RateCode_" scope="session">
    <jsp:setProperty name="user" property="*"/>
</jsp:useBean>

因此,此bean作为名称存储为 会话 属性user。您需要在servlet中将其作为会话属性而不是请求属性进行检索。

RateCode_ user = (RateCode_) request.getSession().getAttribute("user");

user顺便说rateCode一下,这是一个可怕且令人困惑的属性名称,我将其重命名为其他名称,但_最后没有这个奇数)

但是,它将不包含任何内容。在getCode()getDescription()返回null。在<jsp:setProperty>已即
没有
在你试图访问它在servlet这一点与所有请求参数填充它。仅当您将包含参数的请求转发回JSP页面时,才会这样做。但是,这发生在servlet中的业务逻辑之外。

您需要自己收集它们作为请求参数。首先 摆脱<jsp:useBean>
JSP中的全部内容,并在servlet的doPost()方法中执行以下操作:

RateCode_ user = new RateCode_();
user.setCode(request.getParameter("code"));
user.setDescription(request.getParameter("description"));
// ...
request.setAttribute("user", user); // Do NOT store in session unless really necessary.

然后您可以在JSP中如下访问它:

<input type="text" name="code" value="${user.code}" />
<input type="text" name="description" value="${user.description}" />

(这仅对 XSS攻击敏感,您想安装JSTL并使用fn:escapeXml

不,你 不是
需要<jsp:useBean>在JSP中。保持现状,当您将MVC(第2级)方法与实际的servlet一起使用时,它几乎没有任何价值。该<jsp:useBean>仅供MV设计(MVC1级)是有用的。要保存收集请求参数的样板代码,请考虑使用MVC框架或Apache Commons BeanUtils。另请参见以下链接以获取提示。

2020-06-08