直接上代码,喜欢的请点赞!
pom.xml
web.xml
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/DB
jdbc.username=
jdbc.password=
jdbc.maxActive=20
jdbc.maxIdle=20
jdbc.maxWait=9000
dataSource.xml
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-3.0.xsd">
springmvc.xml
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-3.0.xsd">
User.java
package com.dong.pojo;
import java.util.Date;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public void setId(String string) {
// TODO Auto-generated method stub
}
public void setRegtime(Date date) {
// TODO Auto-generated method stub
}
}
UserDao.java
package com.dong.dao;
import com.dong.pojo.User;
public interface UserDao {
public void addUser(User user);
public boolean getUser(User user);
public boolean getUsername(User user);
}
UserDaoImpl
package com.dong.dao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.dong.pojo.User;
@Repository
public class UserDaoImpl implements UserDao{
@Autowired
public JdbcTemplate jdbcTemplate;
@Override
public void addUser(User user) {
String sql="insert into user(username,password) values(?,?)";
jdbcTemplate.update(sql,new Object[]{user.getUsername(),user.getPassword()});
}
@Override
public boolean getUser(User user) {
boolean flag=false;
String sql="select 1 from user where username=? and password=?";
String str=jdbcTemplate.queryForObject(sql, new Object[]{user.getUsername(),user.getPassword()},java.lang.String.class);
if(str.equals("1")){
flag=true;
}
return flag;
}
@Override
public boolean getUsername(User user) {
boolean flag=false;
String sql="select '1' from user where username='"+user.getUsername()+"'";
try{
List
for (String str2 : str) {
if(str2.equals("1")){
flag=true;
}
}
}catch(EmptyResultDataAccessException e){
return flag;
}
return flag;
}
}
UserService.java
package com.dong.service;
import com.dong.pojo.User;
public interface UserService{
public void addUser(User user);
public boolean getUser(User user);
public boolean getUsername(User user);
}
UserServiceImpl.java
package com.dong.service;RegisterController.java
package com.dong.controller;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dong.pojo.User;
import com.dong.service.UserService;
@Controller
public class RegisterController {
@Autowired
private UserService userService;
//注册用户
@RequestMapping("/register")
public String userReg(HttpServletRequest req){
User user =new User();
String username=(String) req.getParameter("username");
String password=(String) req.getParameter("password");
user.setUsername(username);
user.setPassword(password);
userService.addUser(user);
req.setAttribute("username", user.getUsername());
req.setAttribute("password", user.getPassword());
return "/login";
}
//处理用户名唯一性的判断
@RequestMapping("/usernameJudge")
public void usernameJudge(HttpServletRequest req,HttpServletResponse res) {
res.setContentType("application/json");
PrintWriter out=null;
try {
out=res.getWriter();
User user=new User();
user.setUsername(req.getParameter("username"));
boolean falg=userService.getUsername(user);
String json="{\"result\":\""+falg+"\"}";
out.write(json);
} catch (Exception e) {
e.printStackTrace();
out.flush();
out.close();
}
}
}
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>