JSP代码
<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代码
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);
}
检索代码
<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>
我允许我的用户设置他们想要搜索的内容(例如用户名,电子邮件等)并输入他们的输入。然后在按下“提交”按钮后,他们将重定向到另一个页面,页面将显示他们搜索的内容。但是,我目前面临的问题是bean返回null值。我可以知道为什么吗?
答案 0 :(得分:0)
在您的servlet代码中,您将它存储到会话
request.getSession(true).setAttribute("searchBean", searchBean);
所以使用它来获取你存储的searchBean。
<% request.getSession().getAttribute("searchBean") %>
答案 1 :(得分:0)
添加此内容 然后你可以像这样在Jsp页面中获取你的对象。
request.setAttribute(&#34; searchBean&#34;,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>
或者您可以像这样使用
<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>
答案 2 :(得分:0)
问题出在输入标记中:<input type="text" id="searchBox"/>
它应该具有属性name
。
试试这个:
<input type="text" name="searchBox" id="searchBox"/>
<br>