Search code examples
javajspjavabeans

JSP bean value not getting updated


I have 2 pages here, in first page i am accepting inputs and then displaying it on seconds page by validating input data using java class user.

INPUT FORM

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="/myweb/formbean.jsp" method="post">

Username : <input type="text" name="name" placeholder="name"><br>
Password : <input type="text" name="password" placeholder="password">

<input type="submit" value="OK">
</form>

</body>
</html>

DISPLAY FORM

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<jsp:useBean id="id1" class="test.user" scope="session" ></jsp:useBean>

<jsp:setProperty property="*" name="id1" />

<h1> <jsp:getProperty property="name" name="id1"/> </h1>
<h1> <jsp:getProperty property="password" name="id1"/> </h1>
<h1> <jsp:getProperty property="message" name="id1"/> </h1>

<%= id1.validate() %>
</body>
</html>

JAVA USER CLASS

public class user {

    private String name;
    private String password;
    private String message;

    public user() {

        validate();
    }

    public user(String name, String password) {

        this.name = name;
        this.password = password;
        validate();
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getMessage() {
        return message;
    }



    public void validate() {

        if (name == null) {
            message = "name cannot be null";

        } else if (password == null) {
            message = "passowrd cannot be null";

        }
        message="form validatuion ok";

    }

}

When i submit the form parameter message value never gets updated it always show me message "name cannot be null" Not sure what i am doing wrong here, please help me figure it out.


Solution

  • <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!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=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    
    <jsp:useBean id="id1" class="test.user" scope="session" ></jsp:useBean>
    
    <jsp:setProperty property="*" name="id1" />
    
    <h1> <jsp:getProperty property="name" name="id1"/> </h1>
    <h1> <jsp:getProperty property="password" name="id1"/> </h1>
    //==========================================
    <%= id1.validate() %>
    <h1> <jsp:getProperty property="message" name="id1"/> </h1>
    //==========================================
    
    </body>
    </html>
    //==============================================================
        public void validate() {
    
            if (name==null){message="name cannot be null";}
            else if(password==null) {message="passowrd cannot be null";}
            else{message="form validatuion ok";}
        }