登录html
< form action= "/tomcat-demo2/loginDemo" method= "get" >
< div class = "inputBox" >
< input type= "text" name= "name" placeholder= "姓名" >
< / div>
< div class = "inputBox" >
< input type= "password" name= "password" placeholder= "密码" >
< / div>
< div class = "inputBox" >
< input type= "submit" value= "登录" >
< / div>
< / form>
登录servlet
@WebServlet ( "/loginDemo" )
public class LoginDemo extends HttpServlet {
@Override
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
String name= new String ( request. getParameter ( "name" ) . getBytes ( StandardCharsets . ISO_8859_1) , StandardCharsets . UTF_8) ;
String password= new String ( request. getParameter ( "password" ) . getBytes ( StandardCharsets . ISO_8859_1) , StandardCharsets . UTF_8) ;
response. setContentType ( "text/html;charset=utf-8" ) ;
String resource = "mybatis-config.xml" ;
InputStream inputStream = Resources . getResourceAsStream ( resource) ;
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ( ) . build ( inputStream) ;
SqlSession sqlSession = sqlSessionFactory. openSession ( ) ;
UserMapper usermapper = sqlSession. getMapper ( UserMapper . class ) ;
User user= usermapper. select ( name, password) ;
sqlSession. close ( ) ;
PrintWriter writer = response. getWriter ( ) ;
if ( user== null ) {
writer. write ( "登录失败~" ) ;
System . out. println ( "falure" ) ;
} else {
writer. write ( "登录成功~" ) ;
System . out. println ( "ok" ) ;
}
}
@Override
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
this . doGet ( request, response) ;
}
}
注册html
< form action= "/tomcat-demo2/registerDemo" method= "get" >
< div class = "inputBox" >
< input type= "text" name= "name" placeholder= "姓名" >
< / div>
< div class = "inputBox" >
< input type= "password" name= "password" placeholder= "密码" >
< / div>
< div class = "inputBox" >
< input type= "submit" value= "注册" >
< / div>
< / form>
注册servlet
@WebServlet ( "/registerDemo" )
public class registerDemo extends HttpServlet {
@Override
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
String name = new String ( request. getParameter ( "name" ) . getBytes ( StandardCharsets . ISO_8859_1) , StandardCharsets . UTF_8) ;
String password = new String ( request. getParameter ( "password" ) . getBytes ( StandardCharsets . ISO_8859_1) , StandardCharsets . UTF_8) ;
response. setContentType ( "text/html;charset=utf-8" ) ;
User user= new User ( ) ;
user. setName ( name) ;
user. setPassword ( password) ;
String resource = "mybatis-config.xml" ;
InputStream inputStream = Resources . getResourceAsStream ( resource) ;
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ( ) . build ( inputStream) ;
SqlSession sqlSession = sqlSessionFactory. openSession ( ) ;
UserMapper usermapper = sqlSession. getMapper ( UserMapper . class ) ;
User user1= usermapper. selectByName ( name) ;
if ( user1== null ) {
usermapper. add ( user) ;
sqlSession. commit ( ) ;
sqlSession. close ( ) ;
} else {
response. getWriter ( ) . write ( "用户名已存在" ) ;
}
}
@Override
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
this . doGet ( request, response) ;
}
}
pojo类
public class User {
private int id;
private String name;
private String password;
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public String getPassword ( ) {
return password;
}
public void setPassword ( String password) {
this . password = password;
}
}
mapper和mapperxml
public interface UserMapper {
@Select ( "select * from stu_info where name=#{name} and password=#{password}" )
User select ( @Param ( "name" ) String name, @Param ( "password" ) String password) ;
@Select ( "select * from stu_info where name=#{name}" )
User selectByName ( @Param ( "name" ) String name) ;
@Insert ( "insert into stu_info values(null, #{name},#{password})" )
void add ( User user) ;
}
< ? xml version= "1.0" encoding= "UTF-8" ? >
< ! DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace= "com.liyin.mapper.UserMapper" >
< resultMap id= "userMapper" type= "User" >
< result column= "password" property= "password" / >
< / resultMap>
< / mapper>
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> tomcat- demo2< / artifactId>
< version> 1.0 - SNAPSHOT< / version>
< packaging> war< / packaging>
< name> tomcat- demo2 Maven Webapp < / name>
< ! -- FIXME change it to the project's website -- >
< url> http: / / www. example. com< / 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> javax. servlet< / groupId>
< artifactId> javax. servlet- api< / artifactId>
< version> 3.1 .0 < / version>
< scope> provided< / scope>
< / dependency>
< dependency>
< groupId> javax. servlet< / groupId>
< artifactId> jstl< / artifactId>
< version> 1.2 < / version>
< scope> provided< / scope>
< / dependency>
< dependency>
< groupId> commons- io< / groupId>
< artifactId> commons- io< / artifactId>
< version> 2.6 < / version>
< / dependency>
< dependency>
< groupId> org. mybatis< / groupId>
< artifactId> mybatis< / artifactId>
< version> 3.5 .5 < / version>
< / dependency>
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql- connector- java< / artifactId>
< version> 5.1 .37 < / version>
< / dependency>
< ! -- junit单元测试坐标-- >
< dependency>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.13 < / version>
< / dependency>
< ! -- https: / / mvnrepository. com/ artifact/ ch. qos. logback/ logback- core -- >
< dependency>
< groupId> ch. qos. logback< / groupId>
< artifactId> logback- core< / artifactId>
< version> 1.3 .0 - alpha10< / version>
< / dependency>
< / dependencies>
< build>
< plugins>
< plugin>
< groupId> org. apache. tomcat. maven< / groupId>
< artifactId> tomcat7- maven- plugin< / artifactId>
< version> 2.2 < / version>
< / plugin>
< / plugins>
< / build>
< / project>
mybatis.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>
< typeAliases>
< package name = "com.liyin.pojo" / >
< / typeAliases>
< environments default = "development" >
< environment id= "development" >
< transactionManager type= "JDBC" / >
< dataSource type= "POOLED" >
< ! -- 数据库连接信息-- >
< property name= "driver" value= "com.mysql.jdbc.Driver" / >
< property name= "url" value= "jdbc:mysql:///myemployees?SSL=false" / >
< property name= "username" value= "root" / >
< property name= "password" value= "root" / >
< / dataSource>
< / environment>
< / environments>
< ! -- 加载sql映射文件-- >
< mappers>
< ! -- < mapper resource= "com/tiger/mapper/UserMapper.xml" / > -- >
< ! -- Mapper 代理方式 包扫描-- >
< package name = "com.liyin.mapper" / >
< / mappers>
< / configuration>