首先创建一个用户表,用来保存用户名 密码,并插入几条数据
mysql> desc user;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(20) | YES | | NULL | |
| password | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
创建语句,注意一定要设置password的长度超过41,因为插入数据时我们使用password函数,生成长度为41的字符串,
例如:
mysql> select password('aasas');
+-------------------------------------------+
| password('aasas') |
+-------------------------------------------+
| *462034BF6D04324F93F1F6CA352B23079A165972 |
+-------------------------------------------+
1 row in set (0.00 sec)
mysql> select length( password('aasas'));
+----------------------------+
| length( password('aasas')) |
+----------------------------+
| 41 |
+----------------------------+
1 row in set (1.76 sec)
CREATE TABLE `user` (
`id`int(11) NOT NULLAUTO_INCREMENT,
`username`varchar(20) DEFAULT NULL,
`password`varchar(50) DEFAULT NULL,PRIMARY KEY(`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
插入数据:
mysql> insert into user(username,password) values('root',password('root'));
查看一下插入结果:此时密码已经变成一串字符串.
mysql> select * from user where username='root';
+----+----------+-------------------------------------------+
| id | username | password |
+----+----------+-------------------------------------------+
| 4 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+----+----------+-------------------------------------------+
1 row in set (0.00 sec)
2.创建一个java web project,新建一个名为user的类,使用jdbc链接数据库,把user表的数据拿出来和输入的用户名密码对比,并添加validate方法
如果用户名密码正确,返回true。之后注意将Mysql JDBC的driver copy到lib目录下,否则无法运行
packagecom.data;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;public classuser {private staticString userName;private staticString password;publicString getUserName() {returnuserName;
}public voidsetUserName(String userName) {this.userName =userName;
}publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}public static booleanvalidate()
{
String url= "jdbc:mysql://localhost:3306/study";
String user= "root";
String dbpassword= "3edc4rfv";boolean result = false;try{
String driver="com.mysql.jdbc.Driver";
Class.forName(driver);
Connection con=DriverManager.getConnection(url,user, dbpassword);if(con==null){
System.out.println("can't open DBConnection");
}
String sql= "select * from user where username=? and password=password(?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, userName);
pstmt.setString(2, password);
System.out.println(sql);
ResultSet rs=pstmt.executeQuery();if(rs.next())
{
System.out.println("PASS");
result=true;
}else{
System.out.println("FAIl");
result=false;
}
pstmt.close();
rs.close();
con.close();
}catch(Exception e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnresult;
}
}
并新建一个Servlet,包名com.web,class name login.java,这里主要使用post方法,所以在dopost方法添加代码
将login.java 修改为这样:
packagecom.web;importjava.io.IOException;importjava.io.PrintWriter;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.util.ArrayList;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.data.book;importcom.data.user;public class login extendsHttpServlet {/*** Constructor of the object.*/
publiclogin() {super();
}/*** Destruction of the servlet.
*/
public voiddestroy() {super.destroy(); //Just puts "destroy" string in log//Put your code here
}/*** The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
*@paramrequest the request send by the client to the server
*@paramresponse the response send by the server to the client
*@throwsServletException if an error occurred
*@throwsIOException if an error occurred*/
public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {//Add some codes
doPost(request,response);
}/*** The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
*@paramrequest the request send by the client to the server
*@paramresponse the response send by the server to the client
*@throwsServletException if an error occurred
*@throwsIOException if an error occurred*/
public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {
request.getParameter("username");
request.getParameter("password");
request.getSession().setAttribute("user", request.getParameter("username")); //将user放在Attribute中
user user1=newuser();
user1.setUserName(request.getParameter("username"));
user1.setPassword(request.getParameter("password"));
System.out.println(request.getParameter("username")+request.getParameter("password"));if(user1.validate())
{request.getRequestDispatcher("welcome.jsp").forward(request, response); //校验用户名密码正确,跳转到welcome.jsp
}else{
request.getRequestDispatcher("index.jsp").forward(request, response); //校验用户名密码不正确,跳转到index.jsp
}
}/*** Initialization of the servlet.
*
*@throwsServletException if an error occurs*/
public void init() throwsServletException {//Put your code here
}
}
此时web.xml代码如下。可以使用login.do访问这个Servlet
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
login Page
login Page
login
com.web.login
login
/login.do
index.jsp
3.在jsp页面添加相应的action,在index.jsp的body标签之间添加,会生出登录界面,用户名 密码
username
password
4.添加welcome.jsp
在welcome.jsp body标签添加
Welcome
5.加载工程