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" name="searchBox" 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 (e.g. 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 how do I store a resultset/multiple values into a java bean and display in another JSP page?
Is there other solution being using a JSTL function?
You can do this with ArrayList
where you store multiple objects (Friends
)
It's recommended that you use servlet as an interface between View (JSP) and Model(Bean). so in the Model you put the business logic of your application
In you Friends class add this method :
public List<Friends> loadList(String searchText) {
List<Friends> Mylist = new ArrayList<Friends>();
// ... Your code to fill the list
return Mylist;
}
to fill your arrayList in each while loop add a Freinds object to arrayList
while (rs.next())
{
searchBean.setFirstName(rs.getString("firstName"));
searchBean.setLastName(rs.getString("lastName"));
searchBean.setUserId(rs.getString("userId"));
searchBean.setDisplayname(rs.getString("displayname"));
Mylist.add(searchBean);
}
In your servlet you call this method to get data and pass it to JSP :
// ...
Friends searchBean = new Friends();
List<Friends> list = searchBean.loadList(searchText);
request.setAttribute("recolist", list);
RequestDispatcher view = request.getRequestDispatcher("My.jsp");
view.forward(request, response);
And in your JSP add :
<c:forEach var="VAR" items="${recolist}">
${VAR./*WHAT YOU WANT*/}
</c:forEach>
Note that VAR is a Friends object you can call any attribute for example :firstName , lastname ...