文章目录 userMapper.java User.java DButil LoginServlet.java RegisterServlet.java TestServlet.java mybatis-config.xml failure.html success.html regist.html pom.xml
userMapper.java
package com. xd. web. mapper ; import com. xd. web. pojo. User ;
import org. apache. ibatis. annotations. Insert ;
import org. apache. ibatis. annotations. Param ;
import org. apache. ibatis. annotations. Select ; import java. util. List ; public interface UserMapper { @Select ( "select user_name from user1 limit 1" ) public String findUserName ( ) ; @Select ( "select * from user1 where user_name = #{name}" ) public List < User > ifExist ( @Param ( "name" ) String userName) ; @Insert ( "insert into user1 (user_name,user_pwd) values(#{name},#{pwd})" ) public Integer register ( @Param ( "name" ) String userName, @Param ( "pwd" ) String userPwd) ; @Select ( "select * from user1 where user_name = #{name} and user_pwd = #{pwd}" ) public List < User > login ( @Param ( "name" ) String userName, @Param ( "pwd" ) String userPwd) ;
}
User.java
package com. xd. web. pojo ; import java. io. Serializable ; public class User implements Serializable { private String userName; private String userPwd; public User ( String userName, String userPwd) { this . userName = userName; this . userPwd = userPwd; } public User ( ) { } public String getUserName ( ) { return userName; } public void setUserName ( String userName) { this . userName = userName; } public String getUserPwd ( ) { return userPwd; } public void setUserPwd ( String userPwd) { this . userPwd = userPwd; }
}
DButil
package com. xd. web. utils ; import org. apache. ibatis. io. Resources ;
import org. apache. ibatis. session. SqlSession ;
import org. apache. ibatis. session. SqlSessionFactory ;
import org. apache. ibatis. session. SqlSessionFactoryBuilder ; import java. io. IOException ;
import java. io. InputStream ; public class DButil { private static SqlSessionFactory sqlSessionFactory = null ; private static SqlSessionFactory sqlSessionFactoryTest = null ; static {
try { InputStream inputStream = Resources . getResourceAsStream ( "mybatis-config.xml" ) ; sqlSessionFactory = new SqlSessionFactoryBuilder ( ) . build ( inputStream) ;
} catch ( IOException e) { throw new RuntimeException ( e) ; } } public static SqlSession getSqlSession ( ) { return sqlSessionFactory. openSession ( ) ; } public static void close ( SqlSession session) { session. close ( ) ; } }
LoginServlet.java
package com. xd. web ; import com. xd. web. mapper. UserMapper ;
import com. xd. web. pojo. User ;
import com. xd. web. utils. DButil ; 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 ;
import java. util. Arrays ;
import java. util. List ;
import java. util. Map ;
@WebServlet ( "/login" )
public class LoginServlet extends HttpServlet {
@Override protected void doPost ( HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException { this . doGet ( req, resp) ; } @Override protected void doGet ( HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException {
String username = req. getParameter ( "username" ) ; String password = req. getParameter ( "password" ) ; System . out. println ( username+ ":" + password) ;
UserMapper mapper = DButil . getSqlSession ( ) . getMapper ( UserMapper . class ) ; List < User > login = mapper. login ( username, password) ; System . out. println ( login) ; if ( login!= null && login. size ( ) > 0 ) { System . out. println ( "走到succ" ) ; req. getRequestDispatcher ( "/login/success.html" ) . forward ( req, resp) ; } else { System . out. println ( "走到else" ) ; req. getRequestDispatcher ( "/forwardtest" ) . forward ( req, resp) ; resp. getWriter ( ) . println ( "登录失败" ) ; } }
}
RegisterServlet.java
package com. xd. web ; import com. xd. web. mapper. UserMapper ;
import com. xd. web. pojo. User ;
import com. xd. web. utils. DButil ;
import org. apache. ibatis. session. SqlSession ; 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 ;
import java. util. List ; @WebServlet ( "/regist" )
public class RegisterServlet extends HttpServlet { @Override protected void doGet ( HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException { String username = req. getParameter ( "username" ) ; String password = req. getParameter ( "password" ) ; SqlSession sqlSession = DButil . getSqlSession ( ) ; UserMapper mapper = sqlSession. getMapper ( UserMapper . class ) ; List < User > ifExist = mapper. ifExist ( username) ; System . out. println ( ifExist) ; if ( ifExist != null && ifExist. size ( ) == 0 ) { System . out. println ( "if" ) ; Integer i = mapper. register ( username, password) ; System . out. println ( i) ; sqlSession. commit ( ) ; req. getRequestDispatcher ( "/login" ) . forward ( req, resp) ; } } @Override protected void doPost ( HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException { this . doGet ( req, resp) ; }
}
TestServlet.java
package com. xd. web ; 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 ( "/forwardtest" )
public class TestServlet extends HttpServlet { @Override protected void doGet ( HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException { System . out. println ( req. getContextPath ( ) ) ; resp. sendRedirect ( req. getContextPath ( ) + "/login/failure.html" ) ; } @Override protected void doPost ( HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException { System . out. println ( req. getContextPath ( ) ) ; System . out. println ( "post" ) ; }
}
user_id int 主键
user_name varchar 30
user_pwd varchar 50
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
< configuration> < environments default = " development" > < environment id = " development" > < transactionManager type = " JDBC" /> < dataSource type = " POOLED" > < property name = " driver" value = " com.mysql.cj.jdbc.Driver" /> < property name = " url" value = " jdbc:mysql://localhost:3306/dict" /> < property name = " username" value = " root" /> < property name = " password" value = " 123456" /> </ dataSource> </ environment> </ environments> < mappers> < mapper class = " com.xd.web.mapper.UserMapper" /> </ mappers>
</ configuration>
failure.html
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < title> Login Failed</ title>
</ head>
< body>
< h1> Login fail</ h1>
< p> Invalid username or password. Please try again.</ p>
失败
</ body>
</ html>
success.html
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < title> Login success</ title>
</ head>
< body>
< h1> Login success</ h1>
< p> Invalid username or password. Please try again.</ p>
成功
</ body>
</ html>
regist.html
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < title> 注册页面</ title> < link rel = " stylesheet" href = " styles.css" >
</ head>
< body>
< div class = " container" > < h2> 用户登录</ h2> < form action = " registerServlet" method = " post" > < div class = " form-group" > < label for = " username" > 用户名:</ label> < input type = " text" id = " username" name = " username" required > </ div> < div class = " form-group" > < label for = " password" > 密码:</ label> < input type = " password" id = " password" name = " password" required > </ div> < div class = " form-group" > < label for = " confirmPassword" > 确认密码:</ label> < input type = " password" id = " confirmPassword" name = " confirmPassword" required > </ div> < button type = " submit" > 注册</ button> </ form>
</ div>
</ body>
</ html>
pom.xml
<?xml version="1.0" encoding="UTF-8"?> < project xmlns = " http://maven.apache.org/POM/4.0.0" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion> 4.0.0</ modelVersion> < groupId> org.example</ groupId> < artifactId> login_register</ artifactId> < version> 1.0-SNAPSHOT</ version> < packaging> war</ packaging> < name> login_register Maven Webapp</ name> < url> http://maven.apache.org</ url> < properties> < project.build.sourceEncoding> UTF-8</ project.build.sourceEncoding> < maven.compiler.source> 1.7</ maven.compiler.source> < maven.compiler.target> 1.7</ maven.compiler.target> </ properties> < dependencies> < dependency> < groupId> junit</ groupId> < artifactId> junit</ artifactId> < version> 4.11</ version> < scope> test</ scope> </ dependency> < dependency> < groupId> org.mybatis</ groupId> < artifactId> mybatis</ artifactId> < version> 3.5.15</ version> </ dependency> < dependency> < groupId> com.mysql</ groupId> < artifactId> mysql-connector-j</ artifactId> < version> 8.0.31</ version> </ dependency> < dependency> < groupId> javax.servlet</ groupId> < artifactId> javax.servlet-api</ artifactId> < version> 3.1.0</ version> < scope> provided</ scope> </ dependency> </ dependencies> < build> < finalName> login_register</ finalName> </ build>
</ project>