Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My jsp printing 'null' instead of null

I am reading a String value from database and printing it onto a jsp page through a servlet. The Problem is that on the Jsp the String 'null' is getting printed in case the field in the database is null. I need to have a blank editbox instead if the value in the database is null.

My database Access Object:

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

My Servlet:

String custempid = rcvd.getVerEmpId(rcvb);
request.setAttribute("custempid", custempid);

My Jsp:

name="custEmpId" readonly="true" value="<%=request.getAttribute("custempid")%>"

My Display if the field is null: My Display

My database is Oracle 11g and Browser is FireFox.

like image 556
Stanley Mungai Avatar asked Dec 01 '25 14:12

Stanley Mungai


1 Answers

You should avoid using scriplets in JSP. Use JSTL and EL instead.

You need to test for null string before printing your value. When you try to print an object reference which is null, the null reference is converted to "null" string.

This is how you pre-test for null using EL:

<c:out value="${not empty custempid ? custempid: ''}" />

Note that not empty will check for both null and empty string.

See also:

  • How to avoid Java code in JSP files?
  • How to avoid using scriptlets in my JSP page?
like image 176
Rohit Jain Avatar answered Dec 03 '25 03:12

Rohit Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!