当前位置:   article > 正文

javaweb项目报告(吃货联盟)

javaweb项目报告

项目总结、收获和自我感觉

  •  ​​​​​​​ 项目主要分为两个部分:用户,商家(管理员)
  •   遗憾:
  1.   点赞和签到功能写的粗糙,没有较强的可行性
  2.    没有实现购物车和订单功能,项目的页面做的也不是特别好。
  •   运用的体系知识

            关键字:implement,extends,private等

            异常处理:try{...}catch(Exception e){...}

            基础语法:遍历,循环,正则表达式,javaweb的基础

            重点(自认为):通过form表单,实现与servlet的连接,servlet方法实现

             

  1. <form action="LoginServlet?method=login" method="post"></form>
  2. action出错/漏写 method="post"都会造成报错
  • 开始写项目时,所学的知识点比较零碎,借鉴其他项目,jsp、servlet、impl等部分混在一起,一头雾水,随着不断修改和项目的多次删除和重写,渐渐理解了其中的思路和规律。
  •  ​​​​​​​一路踉踉仓仓做出来的东西确实不是当初想的那么美好,很多东西没有实现。无论怎么说还是有一个东西出来了,也算是这十几天的结晶,还是得到了很多有意义的经验,相信以后我会做的更好,达到想要到项目需求。

项目主要功能:

1.录,注册

2. 用户                                                             商家

                                                      

项目分层:

项目代码

1.首页:

  1. body{
  2. /**
  3. 引入背景图片,设置尺寸
  4. */
  5. background: url(img/eat.gif);
  6. background-size: 800px 900px;
  7. }
  8. span{
  9. /*文本居中
  10. */
  11. text-align: center;
  12. font-size: 30px;
  13. }
  14. <body>
  15. <span><h1 >吃货联盟</h1></span>
  16. <a href="user/login.jsp"><h2>登录</h2></a>
  17. <a href="user/regist.jsp"><h2>注册</h2></a>
  18. <a href="user/re.jsp"><h3>用户使用</h3></a>
  19. <a href="manager/ch.jsp"><h3>商家后台管理</h3></a>
  20. <br/>
  21. <form action="<%=request.getContextPath()%>/logoutServlet" method = "post">
  22. <input type="submit" value="退出系统"/>
  23. </form>
  24. <br/>
  25. </body>

2.用户

User

  1. private int id;
  2. private String name;
  3. private String pwd;
  4. private String sex;
  5. private String home;
  6. private String tel;
  1. public class UserDaoimpl extends BaseDao<User> implements UserDao{
  2. @Override
  3. public User getUserByUserNameAndPassword(User user) {
  4. String sql="select * from user where name=? and pwd=?";
  5. User bean=this.getBean(sql, user.getName(),user.getPwd());
  6. return bean;
  7. }
  8. @Override
  9. public boolean registUser(User user) {
  10. String sql="insert into user(name,pwd) values(?,?)";
  11. //update>0成功
  12. int update=this.update(sql, user.getName(),user.getPwd());
  13. return update>0;
  14. }
  15. @Override
  16. public User getUserByUserName(User user) {
  17. String sql="select * from user where username=?";
  18. return getBean(sql,user.getName());
  19. }
  20. }
  1. public class UserServiceImpl implements UserService {
  2. private UserDao ud=new UserDaoimpl();
  3. @Override
  4. public User login(User user) {
  5. return ud.getUserByUserNameAndPassword(user);
  6. }
  7. @Override
  8. public boolean regist(User user) {
  9. return ud.registUser(user);
  10. }
  11. @Override
  12. public boolean checkUser(User user) {
  13. User byUserName=ud.getUserByUserName(user);
  14. return byUserName==null;
  15. }
  16. }

3.菜品

Dish

  1. private int id;
  2. private String name;
  3. private double price;
  4. private int sales;//销量
  1. public class DishDaoImpl extends BaseDao<Dish> implements DishDao{
  2. @Override
  3. public List<Dish> getAllDishs() {
  4. String sql="select id,name,price,sales from dish";
  5. //返回所有的图书
  6. return getBeanList(sql);
  7. }
  8. @Override
  9. public boolean addDish(Dish dish) {
  10. // TODO Auto-generated method stub
  11. String sql="insert into dish(id,name,price,sales) values(?,?,?,?)";
  12. int update=update(sql,dish.getId(),dish.getName(),dish.getPrice(),dish.getSales());
  13. //影响了多少行
  14. return update>0;
  15. }
  16. @Override
  17. public boolean delDish(Dish dish) {
  18. String sql="delete from dish where id=?";
  19. int update =update(sql, dish.getId());
  20. return update>0;
  21. }
  22. @Override
  23. public boolean updateDish(Dish dish) {
  24. String sql="update dish set name=?,price=?,sales=? where id=?";
  25. int update=update(sql,dish.getName(),dish.getPrice(),dish.getSales(),dish.getId());
  26. //影响了多少行
  27. return update>0;
  28. }
  29. @Override
  30. public Dish getDish(Dish dish) {
  31. String sql="select id,name,price,sales from dish where id=?";
  32. return getBean(sql,dish.getId());
  33. // return getBean(sql,dish.getId());)
  34. }
  35. @Override
  36. public int getTotalCount() {
  37. String sql="select count(*) from dish";
  38. Object object=getSingleValue(sql);
  39. int parseInt=0;
  40. try {
  41. parseInt=Integer.parseInt(object.toString());
  42. } catch (NumberFormatException e) {
  43. e.printStackTrace();
  44. }
  45. //)
  46. return parseInt;
  47. }
  48. @Override
  49. public int getTotalCountByPrice(double minPrice, double maxPrice) {
  50. String sql="select count(*) from dish where price between ? and ?";
  51. Object object=getSingleValue(sql, minPrice,maxPrice);
  52. int i=0;
  53. try {
  54. i=Integer.parseInt(object.toString());
  55. } catch (NumberFormatException e) {
  56. e.printStackTrace();
  57. }
  58. return i;
  59. }
  60. @Override
  61. public List<Dish> getPageByPrice(int index, int size, double minPrice, double maxPrice) {
  62. String sql="select id,name,price,sales from dish between ? and ? limit ?,?";
  63. return getBeanList(sql,minPrice,maxPrice,index,size);
  64. }
  65. }

4.登录

login.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>Insert title here</title>
  8. <style type="text/css">
  9. body{
  10. background: url(../img/login.gif);
  11. background-size: 1200px 700px;
  12. }
  13. h3{
  14. color:teal;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <h3>${msg }</h3>
  20. <form name="form1" action="LoginServlet?method=login" method="post">
  21. <table width="200" border="3">
  22. <tr>
  23. <td colspan="2"><h1>欢迎登录</h1></td>
  24. </tr>
  25. <tr>
  26. <td>用户名</td>
  27. <td><input type="text" name="username" size="10"></td>
  28. </tr>
  29. <tr>
  30. <td>密码</td>
  31. <td><input type="password" name="password" size="10"></td>
  32. </tr>
  33. <tr>
  34. <td colspan="2"><input type="submit" name="submit" value="登录"> <a
  35. href="regist.jsp">注册新用户</a></td>
  36. </tr>
  37. </table>
  38. </form>
  39. </body>
  40. </html>

LoginServlet

  1. package servlet;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import impl.UserServiceImpl;
  9. import user.User;
  10. import user.UserService;
  11. @WebServlet("/LoginServlet")
  12. public class LoginServlet extends BaseServlet {
  13. private static final long serialVersionUID = 1L;
  14. private UserService us=new UserServiceImpl();
  15. protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16. request.setCharacterEncoding("utf-8");
  17. response.setContentType("text/html;charset=utf-8");
  18. String username=request.getParameter("username");
  19. String password=request.getParameter("password");
  20. User user=us.login(new User(0, username, password, null, null,null));
  21. if(user==null) {
  22. //登陆失败 返回登陆页面 转发
  23. request.setAttribute("msg", "用户名密码错误");;
  24. request.getRequestDispatcher("/user/login.jsp").forward(request, response);
  25. }else {
  26. //登陆成功 返回成功页面 重定向
  27. response.sendRedirect(request.getContextPath()+"/user/login_success.jsp");
  28. }
  29. }
  30. }

5.注册

regist.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>Insert title here</title>
  8. <style type="text/css">
  9. .form{
  10. height: 420px;
  11. margin-top: 25px;
  12. margin: 25px auto 0;
  13. display: flex;
  14. justify-content: center;
  15. align-items: center;
  16. }
  17. span{
  18. /*文本居中
  19. */
  20. text-align: center;
  21. font-size: 30px;
  22. }
  23. body{
  24. background: url(../img/er2.gif);
  25. background-size: 1200px 700px;
  26. }
  27. .errorMsg{
  28. color: red;
  29. }
  30. </style>
  31. <script src="../script/jquery-1.12.4.js" type="text/javascript"></script>
  32. <script type="text/javascript">
  33. $(function(){
  34. $("#sub_tn").click(function(){
  35. // 验证用户名:必须由字母,数字下划线组成,并且长度为512
  36. //1 获取用户名输入框里的内容
  37. var usernameText = $("#username").val();
  38. //2 创建正则表达式对象
  39. var usernamePatt = /^\w{5,12}$/;
  40. //3 使用test方法验证
  41. if (!usernamePatt.test(usernameText)) {
  42. //4 提示用户结果
  43. $("span.errorMsg").text("用户名不合法!");
  44. return false;
  45. }
  46. // 验证密码:必须由字母,数字下划线组成,并且长度为512
  47. //1 获取用户名输入框里的内容
  48. var passwordText = $("#password").val();
  49. //2 创建正则表达式对象
  50. var passwordPatt = /^\w{5,12}$/;
  51. //3 使用test方法验证
  52. if (!passwordPatt.test(passwordText)) {
  53. //4 提示用户结果
  54. $("span.errorMsg").text("密码不合法!");
  55. return false;
  56. }
  57. // 验证确认密码:和密码相同
  58. //1 获取确认密码内容
  59. var repwdText = $("#repwd").val();
  60. //2 和密码相比较
  61. if (repwdText != passwordText) {
  62. //3 提示用户
  63. $("span.errorMsg").text("确认的密码和密码不一致!");
  64. return false;
  65. }
  66. //验证手机号是否正确
  67. var telText=$("#tel").val();
  68. var telPatt = /^(86)?((13\d{9})|(15[0,1,2,3,5,6,7,8,9]\d{8})|(18[0,5,6,7,8,9]\d{8}))$/;
  69. if(!telPatt.test(telText)){
  70. $("span.errorMsg").text("手机号码格式错误!");
  71. return false;
  72. }
  73. // 验证码:现在只需要验证用户已输入。因为还没讲到服务器。验证码生成。
  74. var codeText = $("#code").val();
  75. //去掉验证码前后空格
  76. // alert("去空格前:["+codeText+"]")
  77. codeText = $.trim(codeText);
  78. // alert("去空格后:["+codeText+"]")
  79. if (codeText == null || codeText == "") {
  80. //4 提示用户
  81. $("span.errorMsg").text("验证码不能为空!");
  82. return false;
  83. }
  84. // 去掉错误信息
  85. $("span.errorMsg").text("");
  86. })
  87. })
  88. </script>
  89. </head>
  90. <body>
  91. <div>
  92. <div id="l_content">
  93. <span class="login_word"><h1>欢迎注册</h1></span>
  94. <span class="errorMsg" >
  95. <h3>${msg==null?"请输入用户名":msg}</h3>
  96. </span>
  97. </div>
  98. <div class="form">
  99. <form action="RegistServlet?method=regist" method="post">
  100. 用户名称:
  101. <input name="username" type="text" id="username" placeholder="请输入用户名">(由字母,数字下划线组成,并且长度为512位)<br/>
  102. <br/>
  103. 用户密码:
  104. <input name="password" type="password" id="password" placeholder="请输入密码">(由字母,数字下划线组成,并且长度为512位)<br/>
  105. <br/>
  106. 确认密码:
  107. <input name="repwd" type="password" id="repwd" placeholder="请再次输入密码"><br/>
  108. <br/>
  109. 选择性别:
  110. <input id="man" type="radio" name="sex" value="男" checked>
  111. <input type="radio" id="women"name="sex" value="女" checked><br/>
  112. <br/>
  113. <label>用户地址:</label>
  114. <textarea rows="1" cols="17" placeholder="请输入地址"></textarea>
  115. <br/>
  116. <br/>
  117. 电话号码:
  118. <input name="tel" rows="1" cols="10" id="tel" placeholder="请输入电话号码"/>
  119. <br/>
  120. <br/>
  121. <td>
  122. <tr><label> 验证码:&nbsp&nbsp&nbsp</label></tr>
  123. <tr><input type="text" name="code" id="code" style="width:90px;height: 35px " placeholder="请输入验证码"/> </tr>
  124. <tr><img alt="" src="../img/code.bmp" style=" width:100px;height:40px;position: absolute" id="codeImg"></tr>
  125. </td>
  126. <br/>
  127. <br/>
  128. <input id="sub_tn" type="submit" value="注册" style="width: 60px;height: 30px">
  129. <input id="reset" type="reset" value="重置" style="width: 60px;height: 30px">
  130. </form>
  131. </div>
  132. </div>
  133. </body>
  134. </html>

RegistServlet

  1. package servlet;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import impl.UserServiceImpl;
  9. import user.User;
  10. import user.UserService;
  11. @WebServlet("/RegistServlet")
  12. public class RegistServlet extends BaseServlet {
  13. private static final long serialVersionUID = 1L;
  14. private UserService us=new UserServiceImpl();
  15. protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16. request.setCharacterEncoding("utf-8");
  17. response.setContentType("text/html;charset=utf-8");
  18. String username=request.getParameter("username");
  19. String password=request.getParameter("password");
  20. boolean b=us.regist(new User(0, username, password,null, null, null));
  21. if(b) {
  22. //注册成功 返回成功页面 重定向
  23. response.sendRedirect(request.getContextPath()+"/user/regist_success.jsp");
  24. }else {
  25. //注册失败 返回注册页面 转发
  26. request.setAttribute("msg", "该用户已存在");
  27. request.getRequestDispatcher("/user/regist.jsp").forward(request, response);
  28. }
  29. }
  30. }

6.filter

使用filter在进行操作时进行拦截,只有登录才能进行一系列操作

  1. public class LoginFilter implements Filter {
  2. @Override
  3. public void init(FilterConfig filterConfig) throws ServletException {
  4. // TODO Auto-generated method stub
  5. }
  6. @Override
  7. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  8. throws IOException, ServletException {
  9. System.out.println("filter已经启动");
  10. //验证用户是否登录,如果登录,放行,否则转到登陆页面
  11. HttpServletRequest req=(HttpServletRequest) request;
  12. HttpServletResponse res = (HttpServletResponse)response;
  13. /*String urlString=req.getRequestURI();
  14. if(urlString.endsWith("login.jsp")||urlString.endsWith("LoginServlet")) {
  15. chain.doFilter(request, response);
  16. return;
  17. }*/
  18. //获取已经登陆的用户
  19. User user=Webutils.getLoginUser(req);
  20. if(user==null) {
  21. request.setAttribute("msg", "此操作需要登录,请先登录");
  22. // req.getRequestDispatcher("/user/login.jsp").forward(request, response);
  23. res.sendRedirect(req.getContextPath()+"/user/login.jsp");
  24. }else {
  25. chain.doFilter(request, response);
  26. }
  27. }
  28. @Override
  29. public void destroy() {
  30. // TODO Auto-generated method stub
  31. }
  32. }

在web.xml中配置

  1. <filter>
  2. <filter-name>LoginFilter</filter-name>
  3. <filter-class>filter.LoginFilter</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>LoginFilter</filter-name>
  7. <url-pattern>(要拦截的)</url-pattern>
  8. </filter-mapping>

源码

请联系博主

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/191651
推荐阅读
相关标签