0

I have created a jsp page with some credentials of the user and this time i dont want it to pass to the validation form servlet through doGet() but through doPost(). I am facing some problem while doing this validation of none of the field could be left blank.

PrintWriter out = response.getWriter();
        String name= request.getParameter("name");
        String address = request.getParameter("address");
        String username= request.getParameter("username");
        String password= request.getParameter("password");
        if(name==null)
        {
            out.println("The name field cannot be left blank <br>");
        }
        else if(address==null)
        {
            out.println("The Address field cannot be left blank <br>");
        }
        else if(username==null)
        {
            out.println("The Username field cannot be left blank <br>");
        }
        else if(password==null)
        {
            out.println("The Password field cannot be left blank <br>");
        }
        else if(name!=null && address!=null && username !=null && password !=null)
       {
           out.println("Succesfully logged in");
        }

When executed the jsp page gets redirected to the validationform servlet and am getting as output"Successfully logged in" even though i left some of the fields blank. I think request parameter is not concerned with doPost(). help me to find out the output

4
Contributors
11
Replies
12
Views
6 Years
Discussion Span
Last Post by mith_cool
0

AFAIK, you'll get NULL only if you request the value of a form element which is not present in your form. If "name"/"address"/"username"/"password" are valid form fields, their value would be a blank string and not NULL in case you leave those fields blank.

-1

there is no issue with the string the name, address, username etc are valid form fields. Here is my jsp page

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Validation Form</title>
    </head>
    <body>
        <form method="post" action="http://localhost:37206/Validation/ValidationForm">
            Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="name"><br>
            Address &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <textarea name="address" rows="6"></textarea><br>
            Username &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="username"><br>
            Password &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="password" name="password"><br>
            &nbsp;&nbsp;&nbsp;<input type="submit" value="Submit">

        </form>
    </body>
</html>
0

Your validation if statements should not use NULL but for example address.length() == 0

Edited by peter_budo: n/a

0

I dont know why didnt you understand me... let it be am asking you to provide a fix to my code

0

As I said previously you need to check string length as ~s.o.s~ advised in first place

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    String name = request.getParameter("name");
    String address = request.getParameter("address");
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    if (name.length() == 0) {
      out.println("The name field cannot be left blank <br>");
    } else if (address.length() == 0) {
      out.println("The Address field cannot be left blank <br>");
    } else if (username.length() == 0) {
      out.println("The Username field cannot be left blank <br>");
    } else if (password.length() == 0) {
      out.println("The Password field cannot be left blank <br>");
    } else {
      out.println("Succesfully logged in");
    }
  }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.