时间:2021-07-01 10:21:17 帮助过:3人阅读
三:项目中jsp文件的编写(jsp实质上就是一个servlet)
案例中用到最多的无非是HttpServletRequest request,HttpServletResponse response这两个参数结合页面中的表单form以及转发方式等在页面和后台servlet之间传递数据
开始页面的jsp:index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.List" %> <%@ page import="com.guodiantong.mvc.domain.Customer" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="script/jquery-1.7.2.js"></script> <script type="text/javascript"> $(function(){ $(".delete").click(function(){ var content=$(this).parent().parent().find("td:eq(1)").text(); var flag =confirm("确定要删除" + content + "的信息吗?"); return flag; }); }); </script> </head> <body> <form action="query.do" method="post"> <table> <tr> <td>CustomerName:</td> <td><input type="text" name=name></td> </tr> <tr> <td>Address:</td> <td><input type="text" name="address"></td> </tr> <tr> <td>Phone:</td> <td><input type="text" name="phone"></td> </tr> <tr> <td><input type="submit" value="Query"></td> <td><a href="addCustomer.jsp">Add Customer123</a></td> </tr> </table> </form> <hr> <br> <% List<Customer> customer=(List<Customer>)request.getAttribute("customers"); if(customer!=null && customer.size()>0){ %> <table border="1" cellspacing="0" cellpadding="10"> <tr> <th>id</th> <th>name</th> <th>address</th> <th>phone</th> <th>delete</th> <th>edit</th> </tr> <% for(Customer cust:customer) { %> <tr> <td><%=cust.getId() %></td> <td><%=cust.getName() %></td> <td><%=cust.getAddress() %></td> <td><%=cust.getPhone() %></td> <td><a class="delete" href="delete.do?id=<%=cust.getId()%>">delete</a></td> <td><a class="edit" href="edit.do?id=<%=cust.getId() %>">edit</a></td> </tr> <% } %> <% } %> </table> </body> </html>
新增页面的jsp:addCustomer.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% Object msg=request.getAttribute("message"); if(msg!=null){ %> <%=request.getAttribute("message")%> <% } %> <form action="addCustomer.do" method="post"> <table> <tr> <td>customerName:<input type="text" name="name" value="<%=request.getAttribute("name")==null? "":request.getAttribute("name") %>"></td> </tr> <tr> <td>address:<input type="text" name="address" value="<%=request.getAttribute("address")==null? "":request.getAttribute("address") %>"></td> </tr> <tr> <td>phone:<input type="text" name="phone" value="<%=request.getAttribute("phone")==null? "":request.getAttribute("phone") %>"></td> </tr> <tr> <td><input type="submit" value="submit"></td> </tr> </table> </form> </body> </html>
编辑页面的jsp:edit.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.guodiantong.mvc.domain.Customer" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% Object msg = request.getAttribute("message"); if(msg != null){ %> <br> <font color="red"><%= msg %></font> <br> <br> <% } String id = null; String oldName = null; String name = null; String address = null; String phone = null; Customer customer = (Customer)request.getAttribute("customer"); if(customer != null){ id = customer.getId() + ""; oldName = customer.getName(); name = customer.getName(); address = customer.getAddress(); phone = customer.getPhone(); }else{ id = request.getParameter("id"); oldName = request.getParameter("oldName"); name = request.getParameter("oldName"); address = request.getParameter("address"); phone = request.getParameter("phone"); } %> <form action="update.do" method="post"> <input type="hidden" name="id" value="<%= id %>"/> <input type="hidden" name="oldName" value="<%= oldName %>"/> <table> <tr> <td>CustomerName:</td> <td><input type="text" name="name" value="<%= name %>"/></td> </tr> <tr> <td>Address:</td> <td><input type="text" name="address" value="<%= address %>"/></td> </tr> <tr> <td>Phone:</td> <td><input type="text" name="phone" value="<%= phone %>"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="Submit"/></td> </tr> </table> </form> </body> </html>
四.后台中的控制类Servlet CustomerServlet.java
package com.guodiantong.mvc.servlet; import java.io.IOException; import java.lang.reflect.Method; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.guodiantong.mvc.dao.CustomerDao; import com.guodiantong.mvc.domain.CriteriaCustomer; import com.guodiantong.mvc.domain.Customer; import com.guodiantong.mvc.impl.CustomerDaoJdbcImpl; public class CustomerServlet extends HttpServlet { CustomerDao customerDao=new CustomerDaoJdbcImpl(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String servletPath=req.getServletPath(); System.out.println(servletPath); String methodName=servletPath.substring(1); methodName=methodName.substring(0, methodName.length()-3); /* * 利用反射获取methodName对应的方法 */ try { Method method=getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); //利用反射调用对应的方法 method.invoke(this, req,resp); } catch (Exception e) { e.printStackTrace(); resp.sendRedirect("error.jsp"); } } private void query(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{ String name=request.getParameter("name"); String address=request.getParameter("address"); String phone=request.getParameter("phone"); CriteriaCustomer criteriaCustomer=new CriteriaCustomer(name, address, phone); List<Customer> customers=customerDao.getForListWithCriteriaCustomer(criteriaCustomer); System.out.println(customers); request.setAttribute("customers", customers); request.getRequestDispatcher("/index.jsp").forward(request, response); } private void delete(HttpServletRequest request,HttpServletResponse response) throws IOException{ String idstr=request.getParameter("id"); int id=0; try { id=Integer.parseInt(idstr); System.out.println(id); customerDao.delete(id); } catch (Exception e) { } response.sendRedirect("query.do"); } private void addCustomer(HttpServletRequest resquest,HttpServletResponse response) throws IOException, ServletException{ String name=resquest.getParameter("name"); String address=resquest.getParameter("address"); String phone=resquest.getParameter("phone"); //String message="用户" + name + "已经注册过了"; long count=customerDao.getCountWithName(name); if(count>0){ //resquest.setAttribute("count", count); resquest.setAttribute("message","用户" + name + "已经注册过了"); resquest.setAttribute("address", address); resquest.setAttribute("phone", phone); resquest.getRequestDispatcher("/addCustomer.jsp").forward(resquest, response); } else{ Customer customer=new Customer(null, name, address, phone); customerDao.save(customer); response.sendRedirect("query.do");} } private void edit(HttpServletRequest request,HttpServletResponse response){ String idstr=request.getParameter("id"); int id=0; try { id=Integer.parseInt(idstr); Customer cus=customerDao.get(id); // String name=cus.getName(); // String address=cus.getAddress(); // String phone=cus.getPhone(); // request.setAttribute("name", name); // request.setAttribute("address", address); request.setAttribute("customer", cus); request.getRequestDispatcher("/edit.jsp").forward(request, response); } catch (Exception e) { // TODO: handle exception } } private void update(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{ //1. 获取表单参数: id, name, address, phone, oldName String id = request.getParameter("id"); String name = request.getParameter("name"); String phone = request.getParameter("phone"); String address = request.getParameter("address"); String oldName = request.getParameter("oldName"); //2. 检验 name 是否已经被占用: //2.1 比较 name 和 oldName 是否相同, 若相同说明 name 可用. //2.1 若不相同, 则调用 CustomerDAO 的 getCountWithName(String name) 获取 name 在数据库中是否存在 if(!oldName.equalsIgnoreCase(name)){ long count = customerDao.getCountWithName(name); //2.2 若返回值大于 0, 则响应 updatecustomer.jsp 页面: 通过转发的方式来响应 newcustomer.jsp if(count > 0){ //2.2.1 在 updatecustomer.jsp 页面显示一个错误消息: 用户名 name 已经被占用, 请重新选择! //在 request 中放入一个属性 message: 用户名 name 已经被占用, 请重新选择!, //在页面上通过 request.getAttribute("message") 的方式来显示 request.setAttribute("message", "用户名" + name + "已经被占用, 请重新选择!"); //2.2.2 newcustomer.jsp 的表单值可以回显. //address, phone 显示提交表单的新的值, 而 name 显示 oldName, 而不是新提交的 name //2.2.3 结束方法: return request.getRequestDispatcher("/updatecustomer.jsp").forward(request, response); return; } } //3. 若验证通过, 则把表单参数封装为一个 Customer 对象 customer Customer customer = new Customer(null, name, address, phone); customer.setId(Integer.parseInt(id)); //4. 调用 CustomerDAO 的 update(Customer customer) 执行更新操作 customerDao.update(customer); //5. 重定向到 query.do response.sendRedirect("query.do"); } }
java web基础之mvc模式设计(一)--使用httpservlet实现mvc分层设计,DAO层使用的是dbutils实现与数据库的链接
标签: