2

I am creating a jsp page to test a validator servlet for a uni assessment.

I am new to jsp so I just want to know how to make sure the field is empty on the first page load.

The idea is that when the form is submitted it is sent to a servlet that checks if the fields are empty or not (they have value entered by the user) and redirects them back to the form with error messages, if they are empty.

So here is a snippet of what my jsp fields look like:

<td class="formlabel">Name:</td>
<td><input type="text" size="27" id="name" name="name" value="<%= session.getAttribute("name")%>" /></td>

<%
if((String)session.getAttribute("name") == "") {
%>
    <span class="error">No name entered</span>
<%
}
%>

but currently the problem I am getting is that the session attribute is null. So null comes up in the input field. I don't want this for the first jsp load, I just want it to be blank. How do I do this?

Prakash K
  • 11,669
  • 6
  • 51
  • 109
Bundy
  • 717
  • 3
  • 12
  • 23

2 Answers2

4

Use JSP EL:

<input type="text" name="name" value="${name}" />

JSP EL won't output anything when an attribute is null.

Scriptlets should be avoided anyway.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • I wasn't criticizing - I said that **I** am not familiar with this syntax - and I'm always happy to learn new things... – Nir Alfasi Aug 15 '12 at 02:48
  • 1
    [EL](http://stackoverflow.com/tags/el/info) was introduced by JSTL at Jun 2002 and adopted by JSP spec at Nov 2003. So it definitely isn't new stuff. @alfasin: [read this](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202) before continuing the wrong path. – BalusC Aug 15 '12 at 02:55
  • you guys, relax... I didn't mean that JSP EL is new, what I meant was that the use of `"${name}"` inside a JSP is new **to me**. Thanks for the link BalusC. – Nir Alfasi Aug 15 '12 at 02:59
  • @alfasin JSP EL in *no* way encourages the spaghetti code that can be created via arbitrary Java source in JSP pages--it's not a general-purpose language. I'm not sure how you arrive at your conclusion, IMO it's quite a bit off-base. – Dave Newton Aug 15 '12 at 03:08
  • Maybe I'm missing something, but if you can include getters and setters (and probably other stuff) inside the UI, then it looks the same as the spaghetti code of the scriptlets (perhaps a bit more elegant). But then again, I'm not familiar with EL so I'll go back home and do my HW... :) – Nir Alfasi Aug 15 '12 at 03:34
  • @alfasin Yep, you're missing something :) – Dave Newton Aug 15 '12 at 03:36
1

You can check the value by simply breaking: value="<%= session.getAttribute("name")%> into into a few statements:

<%= String name = session.getAttribute("name");
    if(name == null) name = "";    
%>value="<%=name%>

But in general, instead of validating many fields, I would suggest adding another hidden input parameter to the form with id="submitted" and value="Y":

<input type="hidden id="submitted" name="submitted" value="Y">

then add a line that checks for this POST/GET parameter:

String submitted = request.getParameter("submitted");

and change the if condition to:

 <%if((String)session.getAttribute("name") == "" && "Y".equals(submitted)){%>

Remark:
String comparison (in Java) should be done using equals() - not with ==

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129