数据库准备—创建db_login数据库 t_user表
1、创建web工程
2、创建用户model user.java
package com.gxy.model;
public class User {
private int id;
private String userName;
private String password;
public User() {
super();
}
public User(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3、创建util包 Dbutil.java
package com.gxy.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class Dbutil {
private String dbUrl ="jdbc:mysql://localhost:3306/db_login";
private String jdbcName="com.mysql.jdbc.Driver";
private String dbUserName="root";
private String dbpassword="123456";
public Connection getcon() throws Exception{
Class.forName(jdbcName);
Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbpassword);
return con;
}
public void closeCon(Connection con) throws Exception{
con.close();
}
}
4、创建dao包 UserDao.java
package com.gxy.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.gxy.model.User;
public class UserDao {
public User login(Connection con,User user) throws Exception{
User resultUser=null;
String sql="select * from t_user where userName=? and passWord=?";
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1, user.getUserName());
pst.setString(2,user.getPassword());
ResultSet rs=pst.executeQuery();
if(rs.next()){
resultUser=new User();
resultUser.setUserName(rs.getString("userName"));
resultUser.setPassword(rs.getString("passWord"));
}
return resultUser;
}
}
5、创建servlet包 loginServlet
package com.gxy.servlet;
import java.io.IOException;
import java.sql.Connection;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.gxy.dao.UserDao;
import com.gxy.model.User;
import com.gxy.util.Dbutil;
public class loginServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
Dbutil dbutil=new Dbutil();
UserDao userDao=new UserDao();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String userName=req.getParameter("userName");
String passWord=req.getParameter("passWord");
Connection con=null;
try {
User user=new User(userName,passWord);
con=dbutil.getcon();
User resultUser=userDao.login(con, user);
if(resultUser==null){
System.out.println("no");
}else{
HttpSession session=req.getSession();
session.setAttribute("userName", resultUser.getUserName());
session.setAttribute("passWord", resultUser.getPassword());
resp.sendRedirect("target.jsp");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
6、用户登录界面 login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="login" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" id="userName" name="userName"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" id="passWord" name="passWord"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交" ></td>
</tr>
</table>
</form>
</body>
</html>
跳转界面 target.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'target.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>用户名:<%=session.getAttribute("userName") %></h1>
<h1>密码:<%=session.getAttribute("passWord") %></h1>
</body>
</html>
7、运行结果