Build tables in the same database
DROP TABLE IF EXISTS ' admin ';
CREATE TABLE ' admin ' (
' AdminPassword ' varchar DEFAULT NULL,
' AdminName ' varchar (DEFAULT NULL)
) Engine=innodb DEFAULT Charset=utf8;
For the overall structure:
To use the package please own Baidu
Package Controller;import javax.servlet.http.HttpSession;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.servlet.modelandview;import Pojo. Admin;
Import service. Adminservice; @Controller
@RequestMapping (value= "/admin")
public class Admincontroller {
@Autowired
Private Adminservice Adminservice;
@RequestMapping (value= "/login.do")
Public Modelandview Login (admin admin, HttpSession session) {
Modelandview mv = new Modelandview ();
Login
Admin A = adminservice.login (admin);
Login Verification
if (A! = NULL) {//success
Session.setattribute ("admin", a);
Mv.setviewname ("/success.jsp");
} else {//failed
String msg = "User name or password error";
Mv.addobject ("msg", MSG);
Mv.setviewname ("/login.jsp");
}
return MV;
}
@RequestMapping (value= "/register.do")
Public Modelandview Register (admin admin, HttpSession session) {
Modelandview mv = new Modelandview ();
Login
int a = adminservice.register (admin);
if (a==1) {
Session.setattribute ("admin", admin);
Mv.setviewname ("/regsucce.jsp");
}else {
String msg = "Registration failed, please try again!";
Mv.addobject ("msg", MSG);
Mv.setviewname ("/reg.jsp");
}
return MV;
}
@RequestMapping (value= "/logout.do")
Public Modelandview Logout (HttpSession session) {
Modelandview mv = new Modelandview ();
Session.removeattribute ("admin");
Mv.setviewname ("/login.jsp");
return MV;
}
}
Package Mapper;import Pojo. Admin;public interface Adminmapper {
Login
Admin findadminbynameandpwd (admin admin);
Registered
int register (admin admin);
} <?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" >
<!--
Development using Mapper Proxy, namespace to match the full class name of the Mapper interface
-
<mapper namespace= "Mapper. Adminmapper ">
<select id= "findadminbynameandpwd" parametertype= "Pojo. Admin "resulttype=" admin ">
SELECT * from admin where adminname=#{adminname} and Adminpassword=#{adminpassword}
</select>
<insert id= "register" parametertype= "Pojo. Admin ">
<!--
Keyproperty: Where the primary key returned is stored in the Pojo object
Order= "After" executes after the insert operation finishes running
Resulttype: The type of primary key returned
-
<selectkey keyproperty= "id" order= "after" resulttype= "int" >
Select LAST_INSERT_ID ()
</selectKey>
Insert into admin (adminname,adminpassword) VALUES (#{adminname},#{adminpassword})
</insert>
</mapper> Package Service;import Pojo. Admin;public interface Adminservice {
Admin login (admin admin);
Registered
int register (admin admin);
} Package Service;import Mapper. Adminmapper;import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.service;import Pojo. Admin;
Import service. Adminservice;
@Service (value= "Adminservice")
public class Adminserviceimpl implements adminservice{
@Autowired
Private Adminmapper Adminmapper; @Override
Public Admin Login (admin admin) {
return adminmapper.findadminbynameandpwd (admin);
} @Override
public int Register (admin admin) {
return Adminmapper.register (admin);
}
}<?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:context= "Http://www.springframework.org/schema/context"
xmlns:p= "http://www.springframework.org/schema/p"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop.xsd ">
<!--load configuration files--
<context:property-placeholder location= "Classpath:db.properties"/>
<!--configuring data sources-
<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "Close" >
<property name= "Driverclass" value= "${jdbc.drivername}" ></property>
<property name= "Jdbcurl" value= "${jdbc.url}" ></property>
<property name= "user" value= "${jdbc.username}" ></property>
<property name= "Password" value= "${jdbc.password}" ></property>
<property name= "acquireincrement" value= "${acquireincrement}" ></property>
<property name= "initialpoolsize" value= "${initialpoolsize}" ></property>
<property name= "minpoolsize" value= "${minpoolsize}" ></property>
<property name= "maxpoolsize" value= "${maxpoolsize}" ></property>
</bean>
<!--configuration Sqlsessionfactory--
<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<property name= "configlocation" value= "Classpath:SqlMapConfig.xml" ></property>
<property name= "DataSource" ref= "DataSource" ></property>
</bean>
<!--
Bulk introduction of mapper using packet scanning, the class name can be used when the reference is scanned, the first letter lowercase
-
<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >
<!--Specifies the full path name of the package to be scanned, if there is more than one comma separated by the English state--
<property name= "Basepackage" value= "Mapper" ></property>
</bean>
<!--
Turn on annotation scanning to scan annotations under all service packs
-
<context:component-scan base-package= "Service" ></context:component-scan>
</beans>jdbc.drivername=com.mysql.jdbc.driver
Jdbc.url=jdbc:mysql://localhost:3306/admin
Jdbc.username= User Name
jdbc.password= Password
Acquireincrement=3
initialpoolsize=10
minpoolsize=2
maxpoolsize=10
Log4j.rootlogger=debug,stdout
Log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
log4j.appender.stdout.layout.conversionpattern=%5p [%t]-%m%n
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:mvc= "Http://www.springframework.org/schema/mvc"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:context= "Http://www.springframework.org/schema/context"
Xsi:schemalocation= "Http://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc.xsd
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context.xsd "> <!--configuration @controller annotation Scan
-
<context:component-scan base-package= "Controller" ></context:component-scan>
</beans> <?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= "Pojo"/>
</typeAliases>
</configuration>
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>Login</title>
<body>
${MSG}
<form action= "${pagecontext.request.contextpath}/admin/login.do" method= "POST" >
User name <input type= "text" name= "AdminName"/><br/>
Password <input type= "password" name= "AdminPassword"/><br/>
<input type= "Submit" value= "Login"/>
<a href= "${pagecontext.request.contextpath}/reg.jsp" > Registration </a>
</form>
</body>
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>register</title>
<script language= "javascript" type= "" >
function Regsubmit () {
var user = Document.reg.adminName.value;
var pwd = Document.reg.adminPassword.value;
if (user==null| | user== "") {
Alert ("Please fill in User name");
document.getElementById ("AdminName"). Focus;
}else if (pwd==null| | pwd== "") {
Alert ("Password cannot be null");
}else Document.reg.submit ();
}
</script>
<body>
${MSG}
<form name= "Reg" action= "${pagecontext.request.contextpath}/admin/register.do" method= "POST" >
User name <input type= "text" name= "AdminName"/><br/>
Password <input type= "password" name= "AdminPassword"/><br/>
<input type= "button" value= "register" onclick= "Regsubmit ()"/>
</form>
</body>
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
Congratulations, your registration is successful! <br/>
<a href= "${pagecontext.request.contextpath}/login.jsp" > Login </a>
</body>
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
Congratulations, Login Success!
Welcome ${sessionscope.admin.adminname}<br/>
Your current user information is:<br/>
User name: ${sessionscope.admin.adminname}
Password: ${sessionscope.admin.adminpassword}
<form action= "${pagecontext.request.contextpath}/admin/logout.do" method= "POST" >
<input type= "Submit" value= "Logout"/>
</form>
</body>
Javaweb Login SSM Framework