0

I don't get why my server side isn't working.. We just started learning about jsp, we're not diving into servlets so just java codes (and of course html and javascript). I have to validate if all camps are filled.

HTML:

<form method="post" action="server.jsp">

Name: <input type="text" value="" name="name" size="30">
Surname: <input type="text" value="" name="surname">
Age: <select name="age" size="1">
                        <option value="selectcard">---</option>
                        <% int i; for (i = 16; i <=80; i++){
                        out.print("<option>" + i + "</option>");
                        }
                        %>
                        </select>
How did you know about us?
                        <br><input type="radio" value="a friend"     name="know_us">a friend
                        <br><input type="radio" value="a teacher" name="know_us">a teacher
                        <br><input type="radio" value="the board" name="know_us">the board
                        <br><input type="radio" value="on internet" name="know_us">on internet
                        <br><input type="radio" value="other" name="know_us">other (specify) <input type="text" value="" name="know_us">

<input type="submit" name="submit" id="submit" value="submit">
<input type="reset" name="reset" id="reset" value="clear">
</form>

Server side:

<%
    String name = request.getParameter("name");
    String surname = request.getParameter("surname");
    String age = request.getParameter("age");
    String know_us = request.getParameter("know_us");

if (name == null){
    out.print("<span style='color: red;'>There was an error</span>");
}else if(surname == null){
    out.print("<span style='color: red;'>There was an error</span>");
}else if(age.equals("selectcard")){
    out.print("<span style='color: red;'>There was an error</span>");
}else if(know_us == null){
    out.print("<span style='color: red;'>There was an error</span>");
}else{
    out.print("<h1 style='color: #666; text-align: center;'>Success!</h1>");
}
%>

My javascript is working so i didnt include it here. Whenever i don't fill a field it doesn't give me any error? It only shows "Success".. Please help, I'm getting frustrated! D:

shean
  • 93
  • 1
  • 2
  • 8
  • 2
    You also need to [check if the string is empty](http://stackoverflow.com/questions/3598770/java-check-whether-a-string-is-not-null-and-not-empty), not just `null`. – Mike Cluck Apr 11 '16 at 19:19
  • What is the `out` object that you print to? Does it reflect (prints) on the page? If so where on the page ? – MaxZoom Apr 11 '16 at 19:23
  • @MikeC thanks mike, that was helpful... i completely forgot – shean Apr 11 '16 at 20:08

1 Answers1

-1

Try looking for an empty string as well.

if (name == null || name.isEmpty())
ManoDestra
  • 6,325
  • 6
  • 26
  • 50
  • Where did you get tr? What does that mean? and isEmpty()? – shean Apr 11 '16 at 19:16
  • If your parameter is posted but empty it won't be null but it will be an empty string. A empty string "" is not equal to null. In your case, if you have a empty string posted it will get to your Sucess line. – nbernier Apr 22 '16 at 14:35