Session和DAO 用户登陆

来源:互联网 发布:淘宝运营怎么做 编辑:程序博客网 时间:2024/06/01 14:45

用户在html下输入用户名和密码,然后点击登陆,跳转到另外一个界面。



login.html


<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body><form action="MyLogin" method="post">Username:<input type="text" name="username"> <br/>Password:<input type="password" name="password"> <br/><input type="submit" value="Login"></form></body></html>


MyLogin.java


package test.demo1;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;/** * Servlet implementation class MyLogin */@WebServlet("/MyLogin")public class MyLogin extends HttpServlet {private static final long serialVersionUID = 1L;           /**     * @see HttpServlet#HttpServlet()     */    public MyLogin() {        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 stubString username=request.getParameter("username");String password=request.getParameter("password");UserDao dao=new UserDaoImpl();User u=dao.Login(username, password);if(u!=null){HttpSession session=request.getSession();session.setAttribute("UserSession", u);request.getRequestDispatcher("welcome.jsp").forward(request, response);}else {System.out.println("username or password error,please relogin");}}/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}

接下来就是对user的封装
user.java
package test.demo1;public class User {private int id;private String username;private String password;/** * @return the id */public int getId() {return id;}/** * @param id the id to set */public void setId(int id) {this.id = id;}/** * @return the username */public String getUsername() {return username;}/** * @param username the username to set */public void setUsername(String username) {this.username = username;}/** * @return the password */public String getPassword() {return password;}/** * @param password the password to set */public void setPassword(String password) {this.password = password;}}


userDao.java

package test.demo1;public interface UserDao {public User Login(String username,String password);}


userDaoimpl.java

package test.demo1;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import com.mysql.jdbc.Statement;public class UserDaoImpl implements UserDao{@Overridepublic User Login(String username, String password) {// TODO Auto-generated method stubConnection conn=DBUtil.open();String sql="select id,username,password from i where username=? and password=?";try {PreparedStatement pstmt=conn.prepareStatement(sql);pstmt.setString(1, username);pstmt.setString(2, password);ResultSet rs=pstmt.executeQuery();if(rs.next()){int id=rs.getInt(1);User u=new User();u.setId(id);u.setUsername(username);u.setPassword(password);return u;}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.close(conn);}return null;}}


welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!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=ISO-8859-1"><title>Insert title here</title></head><body>welcome,${UserSession.username}</body></html>


重要的DBUtil.java

package test.demo1;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DBUtil {private static String driver;private static String url;private static String username;private static String password;static {driver="com.mysql.jdbc.Driver";url="jdbc:mysql://localhost:3306/tree";username="root";password="wonshy123..";}public static Connection open(){try {Class.forName(driver);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {return DriverManager.getConnection(url,username,password);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}public static void close(Connection conn){if(conn!=null)try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


同时还需要将mysql-connector-java-5.1.38-bin.jar放在WebContent下的lib文件夹里,我一开始试了很多次都还是没对。后来把eclipse重启之后。。就对了。。。。果然。。。

0 0