简体   繁体   中英

Java Bean return null value

JSP code

<form method="get" action="/asq/searchBox">

<select id="searchType" name="searchType">
<option id="username" value="username" >Username</option>
<option id="realName"  value="realName">Real name</option>
<option id="emailAddress" value="emailAddress">Email Address</option>
<option id="interest" value="interest">Interest</option>
</select>
<input type="text" id="searchBox"/>
<br>
<input type="submit" name="submit" value="Submit" />
</form>

Java Bean

public class Friends {
    private String userId;
    private String firstName;
    private String lastName;
    private String displayname; 

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getDisplayname() {
        return displayname;
    }
    public void setDisplayname(String displayname) {
        this.displayname = displayname;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }

}

Servlet Code

String searchType = request.getParameter("searchType");
        String searchText = request.getParameter("searchBox");

        Friends searchBean = new Friends();

        if(searchType.equals("username") == true){
            try {

            //SQL Statement
            String sql = "SELECT firstName, lastName, userId, displayname FROM azq.registration WHERE azq.registration.displayname LIKE '%" + searchText+ "%'";
            // create the java statement
            Statement st = conn.createStatement();

            // execute the query, and get a java resultset
            ResultSet rs = st.executeQuery(sql);

            // iterate through the java resultset
            while (rs.next())
            {
                searchBean.setFirstName(rs.getString("firstName"));
                searchBean.setLastName(rs.getString("lastName"));
                searchBean.setUserId(rs.getString("userId"));
                searchBean.setDisplayname(rs.getString("displayname"));
            }

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    else if(searchType.equals("realName")== true){
        try {

            //SQL Statement
            String sql = "SELECT firstName, lastName, userId, displayname FROM azq.registration WHERE azq.registration.firstName LIKE '%" + searchText+ "%' OR azq.registration.lastName LIKE '%" + searchText+ "%'";
            // create the java statement
            Statement st = conn.createStatement();

            // execute the query, and get a java resultset
            ResultSet rs = st.executeQuery(sql);

            // iterate through the java resultset
            while (rs.next())
            {
                searchBean.setFirstName(rs.getString("firstName"));
                searchBean.setLastName(rs.getString("lastName"));
                searchBean.setUserId(rs.getString("userId"));
                searchBean.setDisplayname(rs.getString("displayname"));
            }

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    else if(searchType.equals("emailAddress")== true){
        try {

            //SQL Statement
            String sql = "SELECT firstName, lastName, userId, displayname FROM azq.registration WHERE azq.registration.emailAddress LIKE '%" + searchText+ "%'";
            // create the java statement
            Statement st = conn.createStatement();

            // execute the query, and get a java resultset
            ResultSet rs = st.executeQuery(sql);

            // iterate through the java resultset
            while (rs.next())
            {
                searchBean.setFirstName(rs.getString("firstName"));
                searchBean.setLastName(rs.getString("lastName"));
                searchBean.setUserId(rs.getString("userId"));
                searchBean.setDisplayname(rs.getString("displayname"));
            }

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    else if(searchType.equals("interest")== true){
        try {

            //SQL Statement
            String sql = "SELECT firstName, lastName, userId, displayname FROM azq.registration WHERE azq.registration.interest LIKE '%" + searchText+ "%'";
            // create the java statement
            Statement st = conn.createStatement();

            // execute the query, and get a java resultset
            ResultSet rs = st.executeQuery(sql);

            // iterate through the java resultset
            while (rs.next())
            {
                searchBean.setFirstName(rs.getString("firstName"));
                searchBean.setLastName(rs.getString("lastName"));
                searchBean.setUserId(rs.getString("userId"));
                searchBean.setDisplayname(rs.getString("displayname"));
            }

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    request.getSession(true).setAttribute("searchBean", searchBean);
    request.getRequestDispatcher("resultPage.jsp").forward(request, response);

}

Retrieve code

<jsp:useBean id="searchBean" class="sg.edu.nyp.sit.bean.Friends" scope="session"/>
<html>
<head>
<title>resultPage</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
    <jsp:getProperty name="searchBean" property="firstName" />
    <jsp:getProperty name="searchBean" property="lastName" />
    <br>
    <jsp:getProperty name="searchBean" property="displayname" />
</body>
</html>

I am allowing my user to set what they want to search (eg Username, Email etc.) and key in their input. Then after pressing the Submit button, they will redirected to another page and the page will display whatever they searched. However, the problem I am facing currently is that the bean is returning me null value. May I know why?

in your servlet code, you are storing it to session,

request.getSession(true).setAttribute("searchBean", searchBean);

so use this, to get the searchBean you stored.

<% request.getSession().getAttribute("searchBean") %>

Just add this Then you can get your object in Jsp page like this.

request.setAttribute("searchBean", searchBean);

<html>
<head>
<title>resultPage</title>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
    ${searchBean.firstName}
    ${searchBean.lastName}
    ${searchBean.displayname}
</body>
</html>

or you can use like this

<html>
<head>
<title>resultPage</title>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
    ${sessionScope.searchBean.firstName}
    ${sessionScope.searchBean.lastName}
    ${sessionScope.searchBean.displayname}
</body>
</html>

The problem is located here in the input tag: <input type="text" id="searchBox"/>
It should have attribute name . Try this:
<input type="text" name="searchBox" id="searchBox"/> <br>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM