cart包
cart.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 02:49 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>购物车</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> </head> <body> <div id="header"> <img class="logo_img" alt="" src="static/img/logo.gif"> <span class="wel_word">购物车</span> <%-- 静态包含登录成功之后的菜单--%> <%@ include file="/pages/common/login_sucess_menu.jsp"%> </div> <%--静态包含页脚内容--%> <%@include file="/pages/common/footer.jsp"%> </body> </html>
checkout.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 02:49 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>结算页面</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> <style type="text/css"> h1 { text-align: center; margin-top: 200px; } </style> </head> <body> <div id="header"> <img class="logo_img" alt="" src="../../static/img/logo.gif"> <span class="wel_word">结算</span> <%-- 静态包含登录成功之后的菜单--%> <%@ include file="/pages/common/login_sucess_menu.jsp"%> </div> <%--静态包含页脚内容--%> <%@include file="/pages/common/footer.jsp"%> </body> </html>
manager包
book_edit.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 02:46 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>编辑图书</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> <style type="text/css"> h1 { text-align: center; margin-top: 200px; } h1 a{ color:red; } input { text-align: center; } </style> <body> <div id="header"> <img class="logo_ing" alt="" arc="../../static/1mg/logo.gif"> <span class="wel_word">编辑图书</span> <%-- 静态包含manager 管理模块的菜单 --%> <%@ include file="/pages/common/manager_menu.jsp"%> </div> <div id="main"> <form action="book_manager.jsp"> <table> <tr> <td>名称</td> <td>价格</td> <td>作者</td> <td>销量</td> <td>库存</td> <td colspan="2">操作</td> </tr> <tr> <td><input nane="book_name" type="text" value="时间简史"/></td> <td><input nane="book_price" type="text" value="30.00"/></td> <td><input name="book_author" type="text" value="霍金"/></td> <td><input name="book_sales" type="text" value="200"/></td> <td><input name="book_stock" type="text" value="300"/></td> <td><input type= "submit" value="提交"/></td> </tr> </table> </form> </div> <%--静态包含页脚内容--%> <%@include file="/pages/common/footer.jsp"%> </body> </html>
book_manager.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 02:47 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta charset="UTF-8"> <title>图书管理</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> </head> <body> <div id="header"> <img class="logo_img" alt="" src="../../static/img/logo.gif"> <span class="wel_word">图书管理系统</span> <%-- 静态包含manager 管理模块的菜单 --%> <%@ include file="/pages/common/manager_menu.jsp"%> </div> <div id="main"> <table> <tr> <td>名称</td> <td>价格</td> <td>作者</td> <td>销量</td> <td>库存</td> <td colspan="2">操作</td> </tr> <tr> <td>时间简史</td> <td>20.00</td> <td>霍金</td> <td>200</td> <td>400</td> <td><a href="book_edit.jsp">修改</a> </td> <td><a href="#">删除</a> </td> </tr> <tr> <td>时间简史</td> <td>20.00</td> <td>霍金</td> <td>200</td> <td>400</td> <td><a href="book_edit.jsp">修改</a> </td> <td><a href="#">删除</a> </td> </tr> <tr> <td>时间简史</td> <td>20.00</td> <td>霍金</td> <td>200</td> <td>400</td> <td><a href="book_edit.jsp">修改</a> </td> <td><a href="#">删除</a> </td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td><a href="book_edit.jsp">添加图书</a> </td> </tr> </table> </div> <%--静态包含页脚内容--%> <%@include file="/pages/common/footer.jsp"%> </body> </html>
manager.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 02:47 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>后台管理</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> <style type="text/css"> h1{ text-align: center; margin-top: 200px; } </style> </head> <body> <div id="header"> <img class="logo_img" alt="" src="../../static/img/logo.gif"> <span class="wel_word">后台管理系统</span> <%-- 静态包含manager 管理模块的菜单 --%> <%@ include file="/pages/common/manager_menu.jsp"%> </div> <div> <h1>欢迎管理员进入后台管理系统</h1> </div> <%--静态包含页脚内容--%> <%@include file="/pages/common/footer.jsp"%> </body> </html>
order_manager.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 02:47 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <title>订单管理</title> </head> <body> <div id="header"> <img class="logo_img" alt="" src="../../static/img/logo.gif"> <span class="wel_word">订单管理系统</span> <%-- 静态包含manager 管理模块的菜单 --%> <%@ include file="/pages/common/manager_menu.jsp"%> </div> <div id="main"> <table> <tr> <td>2015.04.20</td> <td>20.00</td> <td><a href="#">查看详情</a></td> <td>已发货</td> </tr> <tr> <td>2016.04.20</td> <td>20.00</td> <td><a href="#">查看详情</a></td> <td>已发货</td> </tr> <tr> <td>2015.04.20</td> <td>20.00</td> <td><a href="#">查看详情</a></td> <td>已发货</td> </tr> <tr> <td>2014.04.20</td> <td>20.00</td> <td><a href="#">查看详情</a></td> <td>已发货</td> </tr> </table> </div> <%--静态包含页脚内容--%> <%@include file="/pages/common/footer.jsp"%> </body> </html>
order
order.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 02:45 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>我的订单</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> <style type="text/css"> h1 { text-align: center; margin-top: 200px; } </style> </head> <body> <div id="header"> <img class="logo_img" alt="" src="../../static/img/logo.gif"> <span class="wel_word">我的订单</span> <%-- 静态包含登录成功之后的菜单--%> <%@ include file="/pages/common/login_sucess_menu.jsp"%> </div> <div id="main"> <table> </table> </div> <%--静态包含页脚内容--%> <%@include file="/pages/common/footer.jsp"%> </body> </html>
b)抽取页面中相同的内容
common包 在pages下新建
i. head 中css、 jquery、base标签
head.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 03:59 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String basePath=request.getScheme() +"://" +request.getServerName() +":" +request.getServerPort() +request.getContextPath() +"/"; %> <!--写base标签,永远固定相对路径跳转的结果--> <base href="<%=basePath%>"> <link type="text/css" rel="stylesheet" href="static/css/style.css"> <script type="text/javascript" src="static/script/jquery.js"></script>
ii.每个页面的页脚
footer.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 04:37 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <div id="bottom"> <span> 尚硅谷书城.Copyright ©2015 </span> </div>
iii.登录成功后的菜单
login_sucess_menu.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 03:51 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <div> <span>欢迎<span class="um_span">韩总</span>光临尚硅谷书城</span> <a href="../order/order.jsp">我的订单</a> <a href="../../index.jsp">注销</a> <a href="../../index.jsp">返回</a> </div>
iv. manager模块的菜单
manager_menu.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 05:38 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <div> <a href="pages/manager/book_manager.jsp">图书管理</a> <a href="order_manager.jsp">订单管理</a> <a href="../../index.jsp">返回商城</a> </div>
c)登录,注册错误提示,及表单回显
d) BaseServlet的抽取
代码优化一:合并LoginServlet和RegistServlet程序为UserServlet
在实际的项目开发中,一个模块,一般只使用一个Servlet程序。
给login.jsp页面添加隐藏域action,和修改请求地址
给regist.jsp页面添加隐藏域action,和修改请求地址
代码优化二:使用反射优化到了else if 代码
UserServletTest test包下测试类
package com.atguigu.test; import java.lang.reflect.Method; public class UserServletTest { public void login(){ System.out.println("这是login()方法调用了"); } public void regist(){ System.out.println("这是regist()方法调用了"); } public void updateUser(){ System.out.println("这是updateUser()方法调用了"); } public void updateUserPassword(){ System.out.println("这是updateUserPassword()方法调用了"); } public static void main(String[] args) { String action="regist"; try { //获取action业务鉴别字符串,获取相应的业务方法 反射对象 Method method = UserServletTest.class.getDeclaredMethod(action); System.out.println(method); //调用目标业务方法 method.invoke(new UserServletTest()); } catch (Exception e) { e.printStackTrace(); } } }
抽取BaseServlet程序
其他xxxServlet继承BaseServlet即可
BaseServlet
package com.atguigu.web; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.Method; public abstract class BaseServlet extends HttpServlet { protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String action=req.getParameter("action"); // System.out.println(action); //action的value和调用的方法名是统一的 // if ("login".equals(action)){ System.out.println("处理登录的需求"); // login(req,resp); // }else if ("regist".equals(action)){ System.out.println("处理注册的需求"); // regist(req,resp); // } //反射 try { //获取action业务鉴别字符串,获取相应的业务方法 反射对象 Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class); //调用目标业务方法 method.invoke(this,req,resp); } catch (Exception e) { e.printStackTrace(); } } }
e)数据的封装和抽取BeanUtis的使用
BeanUtils 工具类,它可以一次性把所有请求的参数注入到JavaBean中。
BeanUtils不是JDK的类。而是第三方的工具类。所以需要导包。
1、导入需要的jar包
commons-beanutils-1.9.4.jar 下载地址
commons-logging-1.2.jar BeanUtils依赖logging包
2、使用BeanUtils类方法实现注入。
BeanUtils.populate(user,req.getParameterMap());//出现NoClassDefFoundError错误导入commons-collections 3.x.jar,4.x不行
WebUtils 在Utils包下
package com.atguigu.utils; import org.apache.commons.beanutils.BeanUtils; import java.util.Map; public class WebUtils { /** * 把Map中的值注入到对应的JavaBean属性中 * @param value * @param bean * * HttpServletRequest * Dao层 * Service层 * web层 耦合度高 * */ // public static void copyParamToBean(HttpServletRequest req,Object bean ){ public static <T> T copyParamToBean(Map value, T bean ){ try { System.out.println("注入之前:"+bean); /** * 把所有请求的参数都注入到user对象中 */ BeanUtils.populate(bean,value);//出现NoClassDefFoundError错误导入commons-collections3.x.jar,4.x不行 System.out.println("注入之后:"+bean); } catch (Exception e) { e.printStackTrace(); } return bean; } }
UserServlet 在web包下
package com.atguigu.web; import com.atguigu.pojo.User; import com.atguigu.service.UserService; import com.atguigu.service.impl.UserServiceImpl; import com.atguigu.utils.WebUtils; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class UserServlet extends BaseServlet { private UserService userService = new UserServiceImpl(); /** * 处理登录的功能 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1、获取请求的参数 String username = req.getParameter("username"); String password = req.getParameter("password"); //2、调用XxxService.xxx()处理业务 User loginUser = userService.login(new User(null, username, password, null)); //如果等于null,说明登录失败 if (loginUser==null){ // 把错误信息,和回显的表单项信息,保存到Request域中 req.setAttribute("msg","用户名或密码错误!"); req.setAttribute("username",username); // 跳回登录页面 req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp); }else { // 成功 // 跳到成功页面login_success.jsp req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req,resp); } } /** * 处理注册的功能 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 1、获取请求的参数 String username = req.getParameter("username"); String password = req.getParameter("password"); String email = req.getParameter("email"); String code = req.getParameter("code"); // Map<String, String[]> parameterMap = req.getParameterMap(); // for (Map.Entry<String, String[]> entry:parameterMap.entrySet()) { // System.out.println(entry.getKey()+"="+ Arrays.asList(entry.getValue())); // } // User user=new User(); // WebUtils.copyParamToBean(req,user);//注入赋值,与User类中setXxx对应 User user = WebUtils.copyParamToBean(req.getParameterMap(),new User());//注入赋值,与User类中setXxx对应 // 2、检查验证码是否正确 ===写死,要求验证码为:abcde if ("abcde".equalsIgnoreCase(code)){ // 3、检查用户名是否可用 if (userService.existUsername(username)){ System.out.println("用户名["+username+"]已存在!"); // 把回显信息保存到Request域中 req.setAttribute("msg","用户名已存在!!"); req.setAttribute("username",username); req.setAttribute("email",email); // 跳回注册页面 req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp); }else { // 可用 调用Service保存到数据库 userService.registUser(new User(null,username,password,email)); // 跳到注册成功束面regist_success.jsp req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req,resp); } }else { // 把回显信息保存到Request域中 req.setAttribute("msg","验证码错误!!"); req.setAttribute("username",username); req.setAttribute("email",email); System.out.println("验证码["+code+"]错误,"); req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp); } } }