-
导入项目
- 使用maven导入项目的pom.xml文件即可
-
技术选型
- 项目开始做之前要先确定需要使用的技术
-
从MVC三层分析
-
Web
- servlet
- html
- 客户界面会有大量访问, 所以使用静态界面会好一些
- 实现前后端分离
- 管理员界面再使用jsp
- html要使用异步的数据传输方式
- html页面无法接收服务器发来的存储在域(request application等)中的数据, 但是可以以异步的方式收发数据.
- filter
- beanUtils
- jackson
-
Service
- javamail
- redis
- jedis
-
Dao
- mysql
- druid
- jdbcTemplate
- 提交数据时, 将表格里的数据快速序列化为json数据
- 使用表单的jquery对象调用serialize()方法即可
- 前提是类必须实现Serializable接口
- 使用表单的jquery对象调用serialize()方法即可
- servlet中, 要想让函数终止必须使用return语句
-
验证邮箱
- 代码不需要看懂, 会用就行了
-
package cn.lq.travel.util; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; /** * 发邮件工具类 */ public final class MailUtils { private static final String USER = "931722548@qq.com"; // 发件人称号,同邮箱地址 private static final String PASSWORD = "vmcijjsftlsabfai"; // 如果是qq邮箱可以使户端授权码,或者登录密码 /** * * @param to 收件人邮箱 * @param text 邮件正文 * @param title 标题 */ /* 发送验证信息的邮件 */ public static boolean sendMail(String to, String text, String title){ try { final Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", "smtp.qq.com"); // 发件人的账号 props.put("mail.user", USER); //发件人的密码 props.put("mail.password", PASSWORD); // 构建授权信息,用于进行SMTP进行身份验证 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // 用户名、密码 String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; // 使用环境属性和授权信息,创建邮件会话 Session mailSession = Session.getInstance(props, authenticator); // 创建邮件消息 MimeMessage message = new MimeMessage(mailSession); // 设置发件人 String username = props.getProperty("mail.user"); InternetAddress form = new InternetAddress(username); message.setFrom(form); // 设置收件人 InternetAddress toAddress = new InternetAddress(to); message.setRecipient(Message.RecipientType.TO, toAddress); // 设置邮件标题 message.setSubject(title); // 设置邮件的内容体 message.setContent(text, "text/html;charset=UTF-8"); // 发送邮件 Transport.send(message); return true; }catch (Exception e){ e.printStackTrace(); } return false; } public static void main(String[] args) throws Exception { // 做测试用 MailUtils.sendMail("yiqian0519@icloud.com","你好,这是一封测试邮件,无需回复。","测试邮件"); System.out.println("发送成功"); } }
-
Uuid
- 可以生成一个几乎全球唯一的字符串(也有可能出现重复的, 几率很小)
-
package cn.lq.travel.util; import java.util.UUID; /** * 产生UUID随机字符串工具类 */ public final class UuidUtil { private UuidUtil() { } public static String getUuid() { return UUID.randomUUID().toString().replace("-", ""); } /** * 测试 */ public static void main(String[] args) { System.out.println(UuidUtil.getUuid()); System.out.println(UuidUtil.getUuid()); System.out.println(UuidUtil.getUuid()); System.out.println(UuidUtil.getUuid()); } }
-
登录
- 一次登录要验证4样东西
- 验证码是否正确
- 用户名和密码是否正确
- 用户是否激活
- 一次登录要验证4样东西
-
退出登录
- 退出登录要销毁整个session, 而不仅仅是删除session中存储的user数据
-
request.getSession().invalidate();
-
优化servlet
-
概念
-
减少servlet的数量
-
思路: 用以前的方法访问自己写的servlet时候, 后先调用继承自父类的service方法进行方法的分发(具体执行doPost还是doGet). 现在的思路是, 我重写service方法, 自己写分发的程序, 具体步骤:
-
写一个ServiceBase继承HttpServlet, 重写里面的service方法
-
使用反射调用方法
-
-
再写一个ServletUser继承ServiceBase, 写具体的方法实现(add find login方法)
-
-
-
package cn.lq.travel.web.servlet; import com.fasterxml.jackson.databind.ObjectMapper; 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.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /* author: lq time: 2020-04-08 10:39 */ @WebServlet("/ServletBase") public class ServletBase extends HttpServlet { /** * 自己来写方法的分发 * * @param req * @param resp * @throws ServletException * @throws IOException */ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取访问的uri String uri = req.getRequestURI(); //获取方法名字 //使用lastIndexOf String methodName = uri.substring(uri.lastIndexOf("/") + 1); try { //获取方法 Method method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); //执行方法 method.invoke(this, req, resp); } catch (NoSuchMethodException e) {//没有对应的方法 System.out.println("没有这样的方法!"); } catch (IllegalAccessException e) {//如果访问到受保护或者私有的方法时(protected private)就会报这个异常 e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } /** * 将数据序列化为json后直接返回给客户端 * @param obj * @param response * @throws IOException */ public void writeValue(Object obj, HttpServletResponse response) throws IOException { response.setContentType("application/json;charset=utf-8"); new ObjectMapper().writeValue(response.getOutputStream(), obj); } /** * 将数据序列化为json字符串并返回 * @param obj * @return * @throws IOException */ public String writeValueAsString(Object obj, HttpServletResponse response) throws IOException { response.setContentType("application/json;charset=utf-8"); return new ObjectMapper().writeValueAsString(obj); } }
-
package cn.lq.travel.web.servlet; import cn.lq.travel.domain.ResultInfo; import cn.lq.travel.domain.User; import cn.lq.travel.service.impl.ServiceUSerImpl; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Map; /* author: lq time: 2020-04-08 10:40 */ @WebServlet("/user/*") public class ServletUser extends ServletBase { private ServiceUSerImpl service = new ServiceUSerImpl(); /** * 激活用户 * * @param request * @param response * @throws ServletException * @throws IOException */ public void active(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String code = request.getParameter("code"); if (code != null) { boolean flag = service.activeUser(code); String msg = ""; if (flag) { //激活成功 msg = "激活成功, 请<a href='login.html'>登录</a>"; } else { //激活失败 msg = "激活失败, 请联系管理员!"; } response.setContentType("text/html;charser=utf-8"); response.getWriter().write(msg); } } /** * 退出登录 * * @param request * @param response * @throws ServletException * @throws IOException */ public void exit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getSession().invalidate(); response.sendRedirect(request.getContextPath() + "/login.html"); } /** * 查找用户 * * @param request * @param response * @throws ServletException * @throws IOException */ public void findUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { writeValue((User) request.getSession().getAttribute("user"), response); } /** * 用户登录 * * @param request * @param response * @throws ServletException * @throws IOException */ public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { User user = new User(); Map<String, String[]> map = request.getParameterMap(); ResultInfo resultInfo = new ResultInfo(); //暂时先不判断验证码 //判断验证码 String check = request.getParameter("check"); HttpSession session = request.getSession(); String checkCode_server = (String) session.getAttribute("CHECKCODE_SERVER"); session.removeAttribute("CHECKCODE_SERVER"); if (check == null || !check.equalsIgnoreCase(checkCode_server)) { //验证码不正确 resultInfo.setFlag(false); resultInfo.setErrorMsg("验证码不正确!"); writeValue(resultInfo,response); return; } //判断用户名密码 try { BeanUtils.populate(user, map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } User user1 = service.login(user); if (user1 == null) { resultInfo.setFlag(false); resultInfo.setErrorMsg("用户名或密码错误!"); } //判断是否激活 if (user1 != null && !"Y".equals(user1.getStatus())) { //用户未激活 resultInfo.setFlag(false); resultInfo.setErrorMsg("用户未激活!"); } //登陆成功 if (user1 != null && "Y".equals(user1.getStatus())) { resultInfo.setFlag(true); request.getSession().setAttribute("user", user1); } writeValue(resultInfo,response); } /** * 用户注册 * * @param request * @param response * @throws ServletException * @throws IOException */ public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map<String, String[]> map = request.getParameterMap(); String check = request.getParameter("check"); HttpSession session = request.getSession(); String checkCode_server = (String) session.getAttribute("CHECKCODE_SERVER"); session.removeAttribute("CHECKCODE_SERVER"); ResultInfo info = new ResultInfo(); if (checkCode_server == null || !checkCode_server.equalsIgnoreCase(check)) { //验证码错误 info.setFlag(false); info.setErrorMsg("验证码错误!"); writeValue(info,response); return; } User user = new User(); try { BeanUtils.populate(user, map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } boolean flag = service.register(user); if (flag) { //注册成功 info.setFlag(true); } else { //注册失败 info.setFlag(false); info.setErrorMsg("用户名已经存在!"); } writeValue(info,response); } }
-
可以将序列化json的代码提取出来
-
/** * 将数据序列化为json后直接返回给客户端 * @param obj * @param response * @throws IOException */ public void writeValue(Object obj, HttpServletResponse response) throws IOException { response.setContentType("application/json;charset=utf-8"); new ObjectMapper().writeValue(response.getOutputStream(), obj); } /** * 将数据序列化为json字符串并返回 * @param obj * @return * @throws IOException */ public String writeValueAsString(Object obj, HttpServletResponse response) throws IOException { response.setContentType("application/json;charset=utf-8"); return new ObjectMapper().writeValueAsString(obj); }
-
-
-
redis
-
查询sortset中的分数
-
代码: Set<Tuple> categories = jedis.zrangeWithScores("category", 0, -1);
- tuple
- 使用getElement()和getScore()方法获取值
-
-
-
location.search: 可以获取访问当前页面时传递的参数
- 返回的是从问号开始之后的字符串
-
分页
-
服务器向浏览器传输的数据
- pageBean
- int totalCount
- int totalPage
- int currentPage
- int pageSize
- List<T> list
- pageBean
-
浏览器向服务器传输数据
- currentPage
- pageSize
- cid
-
-
任何一个方法, 在接收参数之前都要进行参数的合法性检验
-
封装pageBean
-
private int totalCount;//总记录数 private int totalPage;//总页数 private int currentPage;//当前页号 private int pageSize;//页面大小 private List<T> list;//页面数据
-
-
只有jsp页面才可以使用el表达式, html页面是没办法使用el表达式的
- jsp 使用el循环
- html 使用html()方法
-
分页的js代码
-
-
$(function () { //获取cid var cid = location.search.split("=")[1]; //异步请求数据 //第一次请求页面 load(cid); }) function load(cid, currentPage) { $.post("route/findPage", {cid: cid, currentPage: currentPage}, function (pageBean) { //编写回调函数 $("#span_totalPage").html(pageBean.totalPage) $("#span_totalCount").html(pageBean.totalCount) //编写分页条 var number = pageBean.currentPage - 1; if (number == 0) { number = 1 } //首页 下一页 var lis = '<li onclick="javascript:load(' + cid + ',1)"><a href="javascript:load(' + cid + ',1)">首页</a></li>\n' + '<li class="threeword" onclick="javascript:load(' + cid + ',' + number + ')"><a href="javascript:load(' + cid + ',' + number + ')">上一页</a></li>' //中间页 var begin var end if (pageBean.totalPage < 10) { begin = 1; end = pageBean.totalPage } else { begin = pageBean.currentPage - 5 end = pageBean.currentPage + 4 if (begin < 1) { begin = 1 end = 10 } if (end > pageBean.totalPage) { end = pageBean.totalPage begin = pageBean.totalPage - 9 } } for (var i = begin; i <= end; i++) { if (i == pageBean.currentPage) { lis += '<li class="curPage" onclick="javascript:load(' + cid + ',' + i + ')"><a href="javascript:load(' + cid + ',' + i + ')">' + i + '</a></li>' } else { lis += '<li onclick="javascript:load(' + cid + ',' + i + ')"><a href="javascript:load(' + cid + ',' + i + ')">' + i + '</a></li>' } } //尾页 上一页 number = pageBean.currentPage + 1; if (number == pageBean.totalPage + 1) { number = pageBean.totalPage } lis += '<li onclick="javascript:load(' + cid + ',' + number + ')" class="threeword"><a href="javascript:load(' + cid + ',' + number + ');">下一页</a></li>\n' + '<li onclick="javascript:load(' + cid + ',' + pageBean.totalPage + ')" class="threeword"><a href="javascript:load(' + cid + ',' + pageBean.totalPage + ');">末页</a></li> $("#ul_page").html(lis) //填写页面数据 var lis_route = '' for (var i = 0; i < pageBean.list.length; i++) { var route = pageBean.list[i] lis_route += ' <li>\n' + ' <div class="img"><img style="width: 299px;" src="' + route.rimage + '" alt=""></div>\n' + ' <div class="text1">\n' + ' <p>' + route.rname + '</p>\n' + ' <br/>\n' + ' <p>' + route.routeIntroduce + '</p>\n' + ' </div>\n' + ' <div class="price">\n' + ' <p class="price_num">\n' + ' <span>¥</span>\n' + ' <span>' + route.price + '</span>\n' + ' <span>起</span>\n' + ' </p>\n' + ' <p><a href="route_detail.html">查看详情</a></p>\n' + ' </div>\n' + ' </li>' } $("#ul_route").html(lis_route) }) window.scrollTo(0, 310) }
-
-
从url中获取到的中文数据要进行url解码
-
------------
-
-
get请求方式时出现乱码
-
使用tomcat7时, get中的中文数据并不会自动转码, 到tomcat8才会自动转码
-
---------------------------
-