!!!代码放在最后
网页前端的设计:jsp编写,使用css美化(字体使用Google font,图标使用font awemome,颜色使用palettes|flat ui colors,谷歌可找到,个人觉得挺好用)
使用了简单的js进行表单验证用户名长度、密码、确认密码
项目结构
(1)建立一个名为spring的数据库,user_information的表,字段id、username、password
(2)前端jsp、css代码编写
(3)sqlMapConfig.xml的Mybatis总配置文件
(4)ApplicationContext.xml的Spring总配置文件
(5)编写User类 bean
(6)UserMapper接口和UserMapper.xml定义实现SQL语句findById、findByName、findByUser、insertUser方法
(7)UserService接口和UserServiceImpl实现类(实现addAccount和hasAccount方法)
(8)util工具包下的servlet类(LoginServlet和RegisterServlet)
配置文件:
(1)sqlMapConfig.xml
(2)UserMapper.xml(配置UserMapper接口实现)
insert into spring.user_information (username,password) values(#{username},#{password})
(3)ApplicationContext.xml(配置DataSource、sqlsessionFactory、Mapper实现把User交给Spring管理,数据库实现Mybatis由Spring帮我们完成)
前端网页代码(使用js来更换显示页面,login、register和简单的表单验证)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
login-register
Login my own account
Register new account
css
*{
font-family: "montserrat",sans-serif;
}
body{
margin: 0;
padding: 0;
background: #333;
}
.login-box{
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100vh;
background-image: linear-gradient(45deg,#9fbaa8,#31354c);
}
.login-form{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
color: white;
text-align: center;
}
.login-form h1{
font-family: 'Pacifico', cursive;
font-weight: 400;
font-size: 40px;
margin-top: 0;
}
.text{
display: block;
box-sizing: border-box;
width: 240px;
background: #ffffff28;
border: 1px solid white;
padding: 10px 20px;
color: white;
outline: none;
margin: 10px 0;
border-radius: 6px;
text-align: center;
}
.login-bt{
font-family: 'Pacifico', cursive;
width: 240px;
background: #c0392b;
border: 0;
color: white;
padding: 10px;
border-radius: 6px;
cursor: pointer;
}
.login-bt:hover{
background: #e74c3c;
}
.hide-bt1{
color: #000;
position: absolute;
top: 40px;
right: 40px;
cursor: pointer;
font-size: 24px;
}
.bt{
left: 50%;
position: absolute;
top: 50%;
transform: translate(-50%,-50%);
color: white;
border: 2px solid;
padding: 10px;
cursor: pointer;
}
.register{
left: 50%;
position: absolute;
top: 60%;
transform: translate(-50%,-50%);
color: white;
border: 2px solid;
padding: 10px;
cursor: pointer;
}
.showed_login{
left: 0%;
}
.register-box{
position: absolute;
top: 0;
left: -200%;
width: 100%;
height: 100vh;
background-image: linear-gradient(45deg,#9fbaa8,#31354c);
}
.register-form{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
color: white;
text-align: center;
}
.register-form h1{
font-family: 'Pacifico', cursive;
font-weight: 400;
font-size: 40px;
margin-top: 0;
}
.register-bt{
font-family: 'Pacifico', cursive;
width: 240px;
background: #c0392b;
border: 0;
color: white;
padding: 10px;
border-radius: 6px;
cursor: pointer;
}
.register-bt:hover{
background: #e74c3c;
}
.showed_register{
left: 0%;
}
.hide-bt2{
color: #000;
position: absolute;
top: 40px;
right: 40px;
cursor: pointer;
font-size: 24px;
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
注册失败
已经有该用户,请重新注册。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
登录成功
登录成功,欢迎<%=request.getParameter("username")%>。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
注册成功
注册成功,<%=request.getParameter("username")%>。
UserMapper接口
package com.dingxiang.mapper;
import com.dingxiang.bean.User;
public interface UserMapper {
User findById(Integer id)throws Exception;
User findByName(String username)throws Exception;
User findByUser(User user)throws Exception;
void insertUser(User user)throws Exception;
}
UserService接口
package com.dingxiang.service;
import com.dingxiang.bean.User;
public interface UserService {
Boolean hasAccount(String username,String password)throws Exception;
void addAccount(String username,String password)throws Exception;
}
UserServiceImpl实现类
package com.dingxiang.service;
import com.dingxiang.bean.User;
import com.dingxiang.mapper.UserMapper;
import javax.annotation.Resource;
public class UserServiceImpl implements UserService{
@Resource(type = UserMapper.class)
private UserMapper userMapper;
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public Boolean hasAccount(String username, String password) throws Exception {
User user=new User();
user.setUsername(username);
user.setPassword(password);
if(userMapper.findByUser(user)!=null){
return false;
}
return false;
}
@Override
public void addAccount(String username, String password) throws Exception {
User user=new User();
user.setUsername(username);
user.setPassword(password);
userMapper.insertUser(user);
}
}
LoginServlet
package com.dingxiang.utils;
import com.dingxiang.bean.User;
import com.dingxiang.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
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;
@WebServlet(
name = "loginServlet",
urlPatterns = {"/login.do"}
)
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID =1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
UserService userService= (UserService) ac.getBean("userService");
try {
if(userService.hasAccount(username,password)){
response.sendRedirect("sccuess.jsp");
}else{
response.sendRedirect("index.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
RegisterServlet
package com.dingxiang.utils;
import com.dingxiang.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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;
@WebServlet(
name = "registerServlet",
urlPatterns = {"/register.do"}
)
public class RegisterServlet extends HttpServlet {
private ApplicationContext ac=null;
private static final long serialVersionUID =1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doPost(request,response);
}
@Override
public void init() throws ServletException {
ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
UserService userService= (UserService) ac.getBean("userService");
try {
if(userService.hasAccount(username,password)){
request.getRequestDispatcher("chongfu.jsp").forward(request,response);
}else{
userService.addAccount(username,password);
response.sendRedirect("success_register.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
User类
package com.dingxiang.bean;
public class User {
private Integer id;
private String username;
private String password;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
}