spring mvc 模拟数据库实现注册 登录
【摘要】
model层代码,用户
package com.entity;import java.io.Serializable; public class User implements Serializable { //私有字段; private String username; private String pwd; public Stri...
model层代码,用户
-
package com.entity;
-
import java.io.Serializable;
-
-
public class User implements Serializable {
-
//私有字段;
-
private String username;
-
private String pwd;
-
public String getUsername() {
-
return username;
-
}
-
public void setUsername(String username) {
-
this.username = username;
-
}
-
public String getPwd() {
-
return pwd;
-
}
-
public void setPwd(String pwd) {
-
this.pwd = pwd;
-
}
-
-
-
}
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
-
<%
-
String path = request.getContextPath();
-
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
-
%>
-
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<base href="<%=basePath%>">
-
-
<title>My JSP 'zhuce.jsp' starting page</title>
-
-
<meta http-equiv="pragma" content="no-cache">
-
<meta http-equiv="cache-control" content="no-cache">
-
<meta http-equiv="expires" content="0">
-
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
-
<meta http-equiv="description" content="This is my page">
-
</head>
-
-
<body>
-
注册页面
-
<form action="reg" method="post">
-
账户:<input type="text" name="username"/><br/>
-
密码:<input type="password" name="pwd"/><br/>
-
<input type="submit" value="注册"/>
-
</form>
-
</body>
-
</html>
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
-
<%
-
String path = request.getContextPath();
-
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
-
%>
-
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<base href="<%=basePath%>">
-
-
<title>My JSP 'login.jsp' starting page</title>
-
-
<meta http-equiv="pragma" content="no-cache">
-
<meta http-equiv="cache-control" content="no-cache">
-
<meta http-equiv="expires" content="0">
-
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
-
<meta http-equiv="description" content="This is my page">
-
</head>
-
-
<body>
-
用户登录
-
<form action="login" method="post">
-
账户:<input type="text" name="username"/><br/>
-
密码:<input type="password" name="pwd"/><br/>
-
<input type="submit" value="登录"/>
-
</form>
-
</body>
-
</html>
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
-
<%@ page isELIgnored="false" %>
-
<%
-
String path = request.getContextPath();
-
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
-
%>
-
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<base href="<%=basePath%>">
-
-
<title>My JSP 'welcome.jsp' starting page</title>
-
-
<meta http-equiv="pragma" content="no-cache">
-
<meta http-equiv="cache-control" content="no-cache">
-
<meta http-equiv="expires" content="0">
-
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
-
<meta http-equiv="description" content="This is my page">
-
</head>
-
-
<body>
-
用户: ${user.username }<br/>
-
密码:${user.pwd }
-
</body>
-
</html>
-
package com.controller;
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import org.apache.commons.logging.Log;
-
import org.apache.commons.logging.LogFactory;
-
import org.springframework.stereotype.Controller;
-
//import org.springframework.ui.Model;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RequestMethod;
-
import org.springframework.web.bind.annotation.RequestParam;
-
import org.springframework.web.servlet.ModelAndView;
-
-
import com.entity.User;
-
//注解该类是一个控制器,可以同时接受多个请求动作;
-
@Controller
-
public class UserController {
-
//静态List<User>集合,用来代替数据库来保存注册的用户信息;
-
private static List<User>userList;
-
//UserController类的构造器,初始化List<User>集合
-
public UserController() {
-
super();
-
userList=new ArrayList<User>();
-
}
-
//静态的日志类;logFactory
-
//该方法,请求.../reg,该方法支持Post请求
-
@RequestMapping(value="/reg",method=RequestMethod.GET)
-
public String regForm(){
-
//跳转到注册页面;
-
return "zhuce";
-
}
-
//该方法映射的请求为.../reg,支持Post请求
-
@RequestMapping(value="/reg",method=RequestMethod.POST)
-
public String register(
-
@RequestParam("username")String username,
-
@RequestParam("pwd")String pwd ){
-
//创建User对象;
-
User u=new User();
-
u.setUsername(username);
-
u.setPwd(pwd);
-
//模拟数据存储到数据库user里面
-
userList.add(u);
-
//跳转到登录页面
-
return "login";
-
}
-
//该方法映射的请求是.../login
-
@RequestMapping("/login")
-
public ModelAndView login(@RequestParam("username")String username,
-
@RequestParam("pwd")String pwd,ModelAndView mv){
-
//RequestParam将请求中的username和pwd参数赋值给username,pwd变量。
-
//到集合中去查找下用户是否存在,此处用来模拟数据库验证。
-
for(User user:userList){
-
if(user.getUsername().equals(username)&&user.getPwd().equals(pwd)){
-
mv.addObject("user",user);
-
mv.setViewName("welcome");
-
//mv.addAttribute("user",user);model传递的是requestScope
-
return mv;
-
}
-
}
-
return mv;
-
}
-
-
}
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app version="2.5"
-
xmlns="http://java.sun.com/xml/ns/javaee"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
-
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
-
<welcome-file-list>
-
<welcome-file>index.jsp</welcome-file>
-
</welcome-file-list>
-
<servlet>
-
<servlet-name>hello</servlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
-
<init-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>WEB-INF/spring-servlet.xml</param-value>
-
</init-param>
-
<load-on-startup>1</load-on-startup>
-
</servlet>
-
<servlet-mapping>
-
<servlet-name>hello</servlet-name>
-
<url-pattern>/</url-pattern>
-
</servlet-mapping>
-
-
<filter>
-
<filter-name>encodingFilter</filter-name>
-
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
-
<init-param>
-
<param-name>encoding</param-name>
-
<param-value>UTF-8</param-value>
-
</init-param>
-
</filter>
-
<filter-mapping>
-
<filter-name>encodingFilter</filter-name>
-
<url-pattern>/*</url-pattern>
-
</filter-mapping>
-
</web-app>
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans
-
xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xmlns:p="http://www.springframework.org/schema/p"
-
xmlns:context="http://www.springframework.org/schema/context"
-
xmlns:mvc="http://www.springframework.org/schema/mvc"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
-
http://www.springframework.org/schema/context
-
http://www.springframework.org/schema/context/spring-context-3.0.xsd
-
http://www.springframework.org/schema/mvc
-
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
<context:annotation-config></context:annotation-config>
-
<context:component-scan base-package="com"></context:component-scan>
-
<!-- ViewResolver -->
-
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
-
<property name="prefix" value="/"></property>
-
<property name="suffix" value=".jsp"></property>
-
</bean>
-
-
<!-- <bean name="/hello" class="com.controller.HelloController"></bean>-->
-
</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)