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

Recommended Answers

All 11 Replies

what else do you have in that jsp ?

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.

Nothing much in JSP page i used method="post" thats it

@mith_cool did you actually read and understand what ~s.o.s~ told you above??? Check for string length

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>

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

am not getting you provide me with the code please

am not getting you provide me with the code please

Your reply doesn't make sense...

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

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

tHank you :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.