在【com.item.servlet】下以此创建
【GetInfoServlet】
package com.item.servlet; import com.item.dao.TbTourinfoDAO; import com.item.model.TbTourinfo; import sun.misc.CharacterEncoder; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; /** * 本类为GetInfo查询所有的类 */ @WebServlet("/GetInfo") public class GetInfoServlet extends HttpServlet { /** * 这是DOPOST访问接口 * @param request * @param response * @throws ServletException * @throws IOException */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<TbTourinfo> list = TbTourinfoDAO.GetInfo(); request.setAttribute("lists",list); request.getRequestDispatcher("GetInfo.jsp").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
【SelectByNameSexTypeServlet】
package com.item.servlet; import com.item.dao.TbTourinfoDAO; import com.item.model.TbTourinfo; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @WebServlet("/SelectByNameSexType") public class SelectByNameSexTypeServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String name = request.getParameter("name"); String sex = request.getParameter("sex"); String type = request.getParameter("type"); List<TbTourinfo> list = TbTourinfoDAO.SelectByNameSexType(name==""?null:name, sex==""?null:sex, type==""?null:type); request.setAttribute("lists",list); request.getRequestDispatcher("GetInfo.jsp").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
【AddInfoServlet】
package com.item.servlet; import com.item.dao.TbTourinfoDAO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/AddInfo") public class AddInfoServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String name = request.getParameter("name"); String sex = request.getParameter("sex"); String id_no = request.getParameter("id_no"); String age = request.getParameter("age"); String type = request.getParameter("type"); TbTourinfoDAO.AddInfo(name,sex,id_no,Integer.parseInt(age),Integer.parseInt(type),0); response.sendRedirect("GetInfo"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
【DeleteByIdServlet】
package com.item.servlet; import com.item.dao.TbTourinfoDAO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/DeleteById") public class DeleteByIdServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); TbTourinfoDAO.DeleteById(Integer.parseInt(id)); response.sendRedirect("GetInfo"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
【UpdateByIdServlet】
package com.item.servlet; import com.item.dao.TbTourinfoDAO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/UpdateById") public class UpdateByIdServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String visit_state = request.getParameter("visit_state"); String id = request.getParameter("id"); TbTourinfoDAO.UpdateById(Integer.parseInt(visit_state.equals("0")?"1":"0"),Integer.parseInt(id)); response.sendRedirect("GetInfo"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
在【webapp】下创建
【GetInfo.jsp】
<%@ page import="java.util.List" %> <%@ page import="com.item.model.TbTourinfo" %><%-- Created by IntelliJ IDEA. User: Administrator Date: 2022/5/18 0018 Time: 22:58 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> <h1 align="center">景区预约登记管理系统</h1> <hr/> <% List<TbTourinfo> list = (List<TbTourinfo>) request.getAttribute("lists");%> <form action="/SelectByNameSexType"> 游客姓名:<input type="text" name="name" placeholder="请输入搜索姓名"/> 游客性别:<input type="text" name="sex" placeholder="请输入游客性别"/> 游客类别:<select name="type"> <option value="" selected>所有</option> <option value="0">非会员</option> <option value="1">普通会员</option> <option value="2">VIP会员</option> <option value="3">钻石会员</option> <option value="4">年卡会员</option> </select> <input type="submit" value="查询" class="btn btn-primary"/> <a href="/AddInfo.jsp" class="btn btn-primary">添加</a> </form> <table class="table table-bordered table-hover"> <tr class="info"> <th>编号</th> <th>名称</th> <th>性别</th> <th>身份证</th> <th>年龄</th> <th>类型</th> <th>创建时间</th> <th>是否使用</th> <th>登录时间</th> <th>操作</th> </tr> <% for (TbTourinfo t : list) { %> <tr> <td><%=t.getId()%> </td> <td><%=t.getName()%> </td> <td><%=t.getSex()%> </td> <td><%=t.getId_no()%> </td> <td><%=t.getAge()%> </td> <td><%=t.getType()%> </td> <td><%=t.getVisit_date()%> </td> <td><%=t.getVisit_state()==0?"未使用":"已使用"%> </td> <td><%=t.getRegister_time()%> </td> <td> <a href="/UpdateById?visit_state=<%=t.getVisit_state()%>&id=<%=t.getId()%>" class="btn btn-primary">修改</a> <a href="/DeleteById?id=<%=t.getId()%>" onclick="return confirm('是否删除此行?')" class="btn btn-primary">删除</a> </td> </tr> <% } %> </table> </body> </html>
【AddInfo.jsp】
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2022/5/18 0018 Time: 22:58 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> <form action="/AddInfo" method="post" style="width:50%;position: relative;left: 30%"> <p> <input type="text" name="name" class="form-control" placeholder="请输入姓名"/> </p> <p> <input type="text" name="sex" class="form-control" placeholder="请输入性别"/> </p> <p> <input type="text" name="id_no" class="form-control" placeholder="请输入身份证"/> </p> <p> <input type="text" name="age" class="form-control" placeholder="请输入年龄"/> </p> <p> <input type="text" name="type" class="form-control" placeholder="请输入类型"/> </p> <p> <input type="submit" value="提交" class="btn btn-success btn-block"/> </p> </form> </body> </html>