jsp useBean由servlet的getAttribute为NULL [英] jsp useBean is NULL by getAttribute by servlet

查看:150
本文介绍了jsp useBean由servlet的getAttribute为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

user在servlet中为null.请让我如果做错了.

user is null in servlet. Pls let me if doing mistake.

我试图在bean rateCode.jsp中获取所有html元素

i m trying to get all html element in bean rateCode.jsp

<%@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

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)

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;
    }
}

推荐答案

您似乎误解了jsp:useBean的工作和目的.

You seem to misunderstand the working and purpose of jsp:useBean.

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

First of all, you've declared the bean to be in the session scope and you're filling it with all parameters of the current request.

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

此bean因此以名称user的形式存储为 session 属性.您需要在servlet中将其作为会话属性而不是请求属性进行检索.

This bean is thus stored as session attribute with the name user. You need to retrieve it in the servlet as session attribute, not as request attribute.

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

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

(user is a terrible and confusing attribute name by the way, I'd rename it rateCode or something, without this odd _ in the end)

但是,它将不包含任何内容. getCode()getDescription()将返回null. <jsp:setProperty>即已 not 填充了所有请求参数,但此时您正尝试在servlet中访问它.仅当您将包含参数的请求转发回JSP页面时,才会这样做.但是,这超出了servlet中的业务逻辑.

However, it'll contain nothing. The getCode() and getDescription() will return null. The <jsp:setProperty> has namely not filled it with all request parameters yet at that point you're attempting to access it in the servlet. It will only do that when you forward the request containing the parameters back to the JSP page. However this takes place beyond the business logic in the servlet.

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

You need to gather them as request parameters yourself. First get rid of whole <jsp:useBean> thing in the JSP and do as follows in the servlet's doPost() method:

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中访问它,如下所示:

and then you can access it in the JSP as below:

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

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

(this is only sensitive to XSS attacks, you'd like to install JSTL and use fn:escapeXml)

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

No, you do not need <jsp:useBean> in JSP. Keep it out, it has practically no value when you're using the MVC (level 2) approach with real servlets. The <jsp:useBean> is only useful for MV design (MVC level 1). To save boilerplate code of collecting request parameters, consider using a MVC framework or Apache Commons BeanUtils. See also below links for hints.

  • Easy way of populating Javabeans based on request parameters
  • Using beans in servlets
  • Our Servlets wiki page

这篇关于jsp useBean由servlet的getAttribute为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆