<%@page import="java.util.List"%>
<%@page import="entity.Membership"%>
<!-- retrieve session object, memList -->
<%
List<Membership> memList = (List)session.getAttribute("memList");
%>
<!-- Display Member -->
<fieldset>
<form action="SMemberId.jsp" align="right">
<input TYPE="text" name="id" class="textbox" placeholder="Seach Member By ID">
<input type="SUBMIT" value="Search"/>
</form>
<legend><strong><font color="White"> Member List </font></strong></legend>
<div style="height:245px;overflow:auto;">
<table border="1" width="100%" height="auto" align='center' bgcolor="#00d2ff">
<tr >
<th>Member ID</th>
<th>Member Type</th>
<th>Name</th>
<th>IC</th>
<th>Phone</th>
<th>Address</th>
<th>Gender</th>
<th>Member Fees</th>
</tr>
<% for (Membership membership: memList){ %>
<tr>
<td><%= membership.getMemberid()%></td>
<td><%= membership.getMembertype() %> </td>
<td><%= membership.getName() %></td>
<td><%= membership.getIc() %></td>
<td><%= membership.getPhone() %></td>
<td><%= membership.getAddress() %></td>
<td><%= membership.getGender() %></td>
<td><%= membership.getFees() %> </td>
</tr>
<% } %>
</table>
</div>
</fieldset>
package controller;
import entity.MemService;
import entity.Membership;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class SearchMember extends HttpServlet {
@PersistenceContext
EntityManager em;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
MemService memService = new MemService(em);
List<Membership> memList = memService.findAll();
HttpSession session = request.getSession();
session.setAttribute("memList", memList);
response.sendRedirect("membership/DisplayMember.jsp");
} catch (Exception ex) {
Logger.getLogger(SearchMember.class.getName()).log(Level.SEVERE, null, ex);
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Currenly我只是可以从数据库中显示一个表。我有10个实体。 我可以问我如何按照我输入的标准搜索数据,它会自动出现。 我不知道如何按标准搜索。示例i键入M001,该表将自动出现具有M001的成员或i按性别M键入,将列出所有Gender M.so我需要创建文本字段并搜索?它需要javascript来做什么?
答案 0 :(得分:0)
你的问题是因为它太宽泛了。没有神奇的解决方案可以做到这一点。
您需要从基本解决方案开始,然后从那里进行增强。您可以通过更新下面的请求处理程序来轻松更新Servlet以添加一些基本搜索功能,例如,按名称搜索。
一旦完成这项工作,您将需要查看其他字段的搜索。也许是用户选择他们想要搜索的字段的下拉列表。完成所有这些操作后,您可以查看使用Ajax更新击键表。
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
MemService memService = new MemService(em);
List<Membership> memList;
if(request.getParameter("name") != null){
memList = memService.findByName(request.getParameter("name"));
}else{
memList = memService.findAll();
}
request.setAttribute("memList", memList);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("membership/DisplayMember.jsp");
dispatcher.forward(request, response);
} catch (Exception ex) {
Logger.getLogger(SearchMember.class.getName()).log(Level.SEVERE, null, ex);
}
}
你的表格:
<form align="right">
<input type="text" name="name" class="textbox" placeholder="Seach Member By Name">
<input type="SUBMIT" value="Search"/>
</form>