注册登录(Servlet)
1.编写数据库
create table YhEmail(
id int primary key auto_increment,
name varchar(20),
password INT(10)
);
select * from YhEmail;
2.数据库连接
public class DbutilYhEmail {
public Connection con=null;
ResultSet res=null;
PreparedStatement prd=null;
public Statement sta=null;
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
try {
con= DriverManager.getConnection(
"jdbc:mysql://localhost:3306/***?characterEncoding=UTF-8"
,"***"
,"***");
System.out.println("链接数据库成功");
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return con;
}
这里的***用自己的用户密码还有表;
3.编写测试类:
public class emTest {
public static void main(String[] args) {
DbutilYhEmail test=new DbutilYhEmail();
test.getConnection();
}
}
4.用户名密码封装:
public class YhEmail {
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;
}
}
5.在DbutilYhEmail里面写入录入查找用户与判断登录类
public List<YhEmail> getAll(){//查询
List<YhEmail> list=new ArrayList<YhEmail>();
con=this.getConnection();
try {
prd=con.prepareStatement("select * from YhEmail");
res=prd.executeQuery();
while(res.next()) {
YhEmail test=new YhEmail();
test.setId(res.getInt(1));
test.setName(res.getString(2));
test.setPassword(res.getString(3));
list.add(test);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
prd.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}return list;
}
6.编写简单的登录界面
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>邮箱</title>
<script type="text/javascript">
var boo=false;
function changepic(){
if(boo==false){
document.getElementById("pic").src="tou2.png";//获得指定元素user的值,并赋给name
boo=true;
}
else{
document.getElementById("pic").src="tou1.png";//获得指定元素user的值,并赋给name
boo=false;
}
}
</script>
<style type="text/css">
*{ margin: 0;
padding: 0;
}
body{background: #999;
}
#top{width: 100%;
height: 400px;
background: #009688;
text-align: center;}
h1{padding-top: 100px;}
#login{width: 600px;
height: 200px;
background: #fff;
margin: 120px auto;
padding: 40px 60px;
line-height: 60px;
}
#login input{width: 300px;
height: 30px;
padding-left: 30px;
border-radius: 5px;
border: 1px solid #ccc;
}
#login button{
width: 100px;
height: 30px;
background:#009688;
font-size: 1.2em;
margin-right: 40px;
border-radius: 10px;
border: 1px solid #ccc;
position:relative;
left:-50px;}
#login #user{background: url(../images/username.png) no-repeat 4px;}
#login #pass{background: url(../images/password.png) no-repeat 4px;}
#top #pic{position: relative;
top: -480px;
}
#lo button{
width: 100px;
height: 30px;
background:#009688;
font-size: 1.2em;
margin-right: 40px;
border-radius: 10px;
border: 1px solid #ccc;
position: relative;
left: 770px;
top:23px;
}
</style>
</head>
<body>
<div id="top">
<h1>登录界面</h1>
<div id="login">
<form action="loginServlet128" method="post" >
账号:<input id="user" name="name"/><br/>
密码:<input type="password" id="pass" name="password" onclick="changepic()"/><br/>
<button type="submit">登录</button>
</form>
</div>
<img src="tou1.png" id="pic" />
</div>
<div id="lo">
<form id="zc" action="yhlr.jsp">
<button type="submit" >注册</button>
</form>
</div>
</body>
</html>
7.用户录入jsp:
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册</title>
</head>
<body>
<form action="YhEmailInsert" method="post">
<input type="text" placeholder="账户" name="name"/></br>
<input type="text" placeholder="密码" name="password"/></br>
<button type="submit">添加</button>
</form>
</body>
</html>
8.Servlet部分:
对用户名以及密码录入数据库为后期判断登录做准备
package EmailServlet;
import java.io.IOException;
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 javax.servlet.http.HttpSession;
import Email128.DbutilYhEmail;
/**
* Servlet implementation class YhEmailInsert
*/
@WebServlet("/YhEmailInsert")
public class YhEmailInsert extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public YhEmailInsert() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DbutilYhEmail bean=new DbutilYhEmail();
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String password=request.getParameter("password");
bean.YhEmailInsert(name, password);
response.sendRedirect("index.jsp");
}
}
9.为登录用户名密码是否准确做判断
package EmailServlet;
import java.io.IOException;
import java.io.PrintWriter;
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 javax.servlet.http.HttpSession;
import Email128.DbutilYhEmail;
/**
* Servlet implementation class loginServlet128
*/
@WebServlet("/loginServlet128")
public class loginServlet128 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public loginServlet128() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DbutilYhEmail bean=new DbutilYhEmail();
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String password=request.getParameter("password");
HttpSession session = request.getSession();
session.setAttribute("name", name);
bean.YhEmailInsert(name, password);
System.out.println("验证成功");
response.sendRedirect("first.jsp");
}
}
10.结果展示: