我的jsp打印'null'而不是null

Err*_*ion 1 java jsp servlets oracle11g

我正在从数据库中读取一个String值,并通过servlet将其打印到jsp页面上.问题是在Jsp上,如果数据库中的字段为空,则打印字符串'null'.如果数据库中的值为null,我需要有一个空白的编辑框.

我的数据库访问对象:

 public String getVerEmpId(retailcustomervergobean rcvb) {
        String var = "";
        custid = rcvb.getCustid();
        Connection conn;
        try {
            conn = db.getDbConnection();
            String sql = "select CUST_EMP_ID from retail_cif_master where cust_id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, custid.toUpperCase());
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                var = rs.getString("CUST_EMP_ID");
            }
        } catch (Exception asd) {
            System.out.println(asd.getMessage());
        }
        return var;
    }
Run Code Online (Sandbox Code Playgroud)

我的Servlet:

String custempid = rcvd.getVerEmpId(rcvb);
request.setAttribute("custempid", custempid);
Run Code Online (Sandbox Code Playgroud)

我的Jsp:

name="custEmpId" readonly="true" value="<%=request.getAttribute("custempid")%>"
Run Code Online (Sandbox Code Playgroud)

如果字段为空,我的显示: 我的展示

我的数据库是Oracle 11g,浏览器是FireFox.

Roh*_*ain 5

你应该避免在JSP中使用scriplets.请改用JSTL和EL.

null在打印值之前,您需要测试字符串.当您尝试打印对象引用时null,null引用将转换为"null"字符串.

这是您null使用EL进行预测试的方法:

<c:out value="${not empty custempid ? custempid: ''}" />
Run Code Online (Sandbox Code Playgroud)

请注意,not empty将检查两者null和空字符串.

也可以看看: