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 database is Oracle 11g and Browser is FireFox.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With