spring mvc 模拟数据库实现注册 登录

举报
tea_year 发表于 2021/12/29 23:34:53 2021/12/29
【摘要】 model层代码,用户 package com.entity;import java.io.Serializable; public class User implements Serializable { //私有字段; private String username; private String pwd; public Stri...

model层代码,用户


  
  1. package com.entity;
  2. import java.io.Serializable;
  3. public class User implements Serializable {
  4. //私有字段;
  5. private String username;
  6. private String pwd;
  7. public String getUsername() {
  8. return username;
  9. }
  10. public void setUsername(String username) {
  11. this.username = username;
  12. }
  13. public String getPwd() {
  14. return pwd;
  15. }
  16. public void setPwd(String pwd) {
  17. this.pwd = pwd;
  18. }
  19. }


  
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'zhuce.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. </head>
  17. <body>
  18. 注册页面
  19. <form action="reg" method="post">
  20. 账户:<input type="text" name="username"/><br/>
  21. 密码:<input type="password" name="pwd"/><br/>
  22. <input type="submit" value="注册"/>
  23. </form>
  24. </body>
  25. </html>


  
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'login.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. </head>
  17. <body>
  18. 用户登录
  19. <form action="login" method="post">
  20. 账户:<input type="text" name="username"/><br/>
  21. 密码:<input type="password" name="pwd"/><br/>
  22. <input type="submit" value="登录"/>
  23. </form>
  24. </body>
  25. </html>


  
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ page isELIgnored="false" %>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11. <title>My JSP 'welcome.jsp' starting page</title>
  12. <meta http-equiv="pragma" content="no-cache">
  13. <meta http-equiv="cache-control" content="no-cache">
  14. <meta http-equiv="expires" content="0">
  15. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  16. <meta http-equiv="description" content="This is my page">
  17. </head>
  18. <body>
  19. 用户: ${user.username }<br/>
  20. 密码:${user.pwd }
  21. </body>
  22. </html>


  
  1. package com.controller;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.apache.commons.logging.Log;
  5. import org.apache.commons.logging.LogFactory;
  6. import org.springframework.stereotype.Controller;
  7. //import org.springframework.ui.Model;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11. import org.springframework.web.servlet.ModelAndView;
  12. import com.entity.User;
  13. //注解该类是一个控制器,可以同时接受多个请求动作;
  14. @Controller
  15. public class UserController {
  16. //静态List<User>集合,用来代替数据库来保存注册的用户信息;
  17. private static List<User>userList;
  18. //UserController类的构造器,初始化List<User>集合
  19. public UserController() {
  20. super();
  21. userList=new ArrayList<User>();
  22. }
  23. //静态的日志类;logFactory
  24. //该方法,请求.../reg,该方法支持Post请求
  25. @RequestMapping(value="/reg",method=RequestMethod.GET)
  26. public String regForm(){
  27. //跳转到注册页面;
  28. return "zhuce";
  29. }
  30. //该方法映射的请求为.../reg,支持Post请求
  31. @RequestMapping(value="/reg",method=RequestMethod.POST)
  32. public String register(
  33. @RequestParam("username")String username,
  34. @RequestParam("pwd")String pwd ){
  35. //创建User对象;
  36. User u=new User();
  37. u.setUsername(username);
  38. u.setPwd(pwd);
  39. //模拟数据存储到数据库user里面
  40. userList.add(u);
  41. //跳转到登录页面
  42. return "login";
  43. }
  44. //该方法映射的请求是.../login
  45. @RequestMapping("/login")
  46. public ModelAndView login(@RequestParam("username")String username,
  47. @RequestParam("pwd")String pwd,ModelAndView mv){
  48. //RequestParam将请求中的username和pwd参数赋值给username,pwd变量。
  49. //到集合中去查找下用户是否存在,此处用来模拟数据库验证。
  50. for(User user:userList){
  51. if(user.getUsername().equals(username)&&user.getPwd().equals(pwd)){
  52. mv.addObject("user",user);
  53. mv.setViewName("welcome");
  54. //mv.addAttribute("user",user);model传递的是requestScope
  55. return mv;
  56. }
  57. }
  58. return mv;
  59. }
  60. }


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <welcome-file-list>
  8. <welcome-file>index.jsp</welcome-file>
  9. </welcome-file-list>
  10. <servlet>
  11. <servlet-name>hello</servlet-name>
  12. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  13. <init-param>
  14. <param-name>contextConfigLocation</param-name>
  15. <param-value>WEB-INF/spring-servlet.xml</param-value>
  16. </init-param>
  17. <load-on-startup>1</load-on-startup>
  18. </servlet>
  19. <servlet-mapping>
  20. <servlet-name>hello</servlet-name>
  21. <url-pattern>/</url-pattern>
  22. </servlet-mapping>
  23. <filter>
  24. <filter-name>encodingFilter</filter-name>
  25. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  26. <init-param>
  27. <param-name>encoding</param-name>
  28. <param-value>UTF-8</param-value>
  29. </init-param>
  30. </filter>
  31. <filter-mapping>
  32. <filter-name>encodingFilter</filter-name>
  33. <url-pattern>/*</url-pattern>
  34. </filter-mapping>
  35. </web-app>


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:mvc="http://www.springframework.org/schema/mvc"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  12. http://www.springframework.org/schema/mvc
  13. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  14. <context:annotation-config></context:annotation-config>
  15. <context:component-scan base-package="com"></context:component-scan>
  16. <!-- ViewResolver -->
  17. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  18. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
  19. <property name="prefix" value="/"></property>
  20. <property name="suffix" value=".jsp"></property>
  21. </bean>
  22. <!-- <bean name="/hello" class="com.controller.HelloController"></bean>-->
  23. </beans>

在后续讲课中添加了删除 修改 查询功能,针对删除报错如下:java.util.ConcurrentModificationException异常.

对Vector、ArrayList在迭代的时候如果同时对其进行修改就会抛出java.util.ConcurrentModificationException异常。下面我们就来讨论以下这个异常出现的原因以及解决办法。

单线程环境下的解决办法

public  class  Test {
     public  static  void  main(String[] args)  {
         ArrayList<Integer> list = new  ArrayList<Integer>();
         list.add( 2 );
         Iterator<Integer> iterator = list.iterator();
         while (iterator.hasNext()){
             Integer integer = iterator.next();
             if (integer== 2 )
                 iterator.remove();   //注意这个地方
         }
     }
}

在多线程环境下的解决方法

1)在使用iterator迭代的时候使用synchronized或者Lock进行同步;

  2)使用并发容器CopyOnWriteArrayList代替ArrayList和Vector。


文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/70036826

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。