package com.zoufang.servlet; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zoufang.bean.User; import com.zoufang.dao.UserDao; public class Register extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException, ServletException{ resp.setContentType("text/html;charset=uft-8"); String name=req.getParameter("name"); String pwd=req.getParameter("password"); UserDao uDao=new UserDao(); User user = null; try { user = uDao.findByName(name); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(user.getName()); System.out.println("判断user是否存在"); if(name.equals(user.getName())){ String msg=name+"该用户名已经使用!请重新注册!"; req.setAttribute("msg", msg); req.getRequestDispatcher("register.jsp").forward(req, resp); }else{ System.out.println("检验数据库没有该用户,存入用户信息!"); User user1=new User(); user1.setName(name); user1.setPassword(pwd); try { uDao.saveUser(user1); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } String msg=name+"注册成功,请重新登陆!"; req.setAttribute("msg", msg); req.getRequestDispatcher("login.jsp").forward(req, resp); } } public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException, ServletException{ doGet(req, resp); } }
1、bean层,创建一个对象
package com.zoufang.bean; import java.io.Serializable; public class User implements Serializable{ private String name; private String password; 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; } }
2、dao层连接数据库(此处为mysql)
package com.zoufang.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.zoufang.bean.User; public class UserDao { private static String driver = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/test"; private static String username = "zoufang"; private static String password = "123"; private static Connection connection = null; private static PreparedStatement ps = null; public Connection getConnection() throws ClassNotFoundException, SQLException{ Class.forName(driver); connection=DriverManager.getConnection(url, username, password); return connection; } public void closeConnection() throws SQLException{ if(connection!=null) connection.close(); if(ps!=null) ps.close(); } public void saveUser(User user) throws SQLException{ try { connection=getConnection(); String sql="insert into user values(?,?)"; ps=connection.prepareStatement(sql); ps.setString(1, user.getName()); ps.setString(2, user.getPassword()); ps.executeUpdate(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally{ closeConnection(); } } public User findByName(String name) throws SQLException{ ResultSet rs = null; User user=new User(); try { connection=getConnection(); System.out.println("开始读取数据"); String sql="select * from user where name='"+name+"'"; ps=connection.prepareStatement(sql); rs=ps.executeQuery(sql); while(rs.next()) { System.out.println(rs.getString(1)+"\t"+rs.getString(2)); user.setName(rs.getString(1)); user.setPassword(rs.getString(2)); return user; } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally{ closeConnection(); } return user; } public void deleteUser(String name) throws ClassNotFoundException, SQLException{ Connection connection=getConnection(); String sql="delete from user where name='"+name+"'"; ps=connection.prepareStatement(sql); ps.execute(sql); closeConnection(); } public void changUser(User user) throws SQLException, ClassNotFoundException{ Connection connection=getConnection(); String sql="update user set password='"+user.getPassword()+"' where name='"+user.getName()+"' "; ps=connection.prepareStatement(sql); ps.execute(sql); closeConnection(); } }
3、业务逻辑层
package com.zoufang.servlet; import java.io.IOException; import java.sql.SQLException; 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.zoufang.bean.User; import com.zoufang.dao.UserDao; public class Change extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ resp.setContentType("text/html;charset=uft-8"); HttpSession hs=req.getSession(); String name=(String) hs.getAttribute("1"); System.out.println("当前登陆用户名"+name); String password=(String) hs.getAttribute("2"); String password1=req.getParameter("password1");//新密码 String password2=req.getParameter("password2");//重新输入一次密码 if(password.equals(password1)){ String msg1="新输入的密码与旧密码一样,请重新输入"; req.setAttribute("msg1", msg1); req.getRequestDispatcher("change.jsp").forward(req,resp); }else if(!password2.equals(password1)){ String msg2="密码不一致!请重新输入!"; req.setAttribute("msg2", msg2); req.getRequestDispatcher("change.jsp").forward(req,resp); }else if(password2.equals(password1)){ System.out.println(",,,,,,,,,,,,,,,,,,,"); UserDao userDao=new UserDao(); User user=new User(); user.setName(name); user.setPassword(password1); try { userDao.changUser(user); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } String msg="密码修改成功,请重新登陆!"; req.setAttribute("msg", msg); req.getRequestDispatcher("login.jsp").forward(req,resp); } } public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{ doGet(req, resp); } }
package com.zoufang.servlet; import java.io.IOException; import java.sql.SQLException; 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.zoufang.bean.User; import com.zoufang.dao.UserDao; public class Delete extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ resp.setContentType("text/html;charset=uft-8"); HttpSession hs=req.getSession(); String name=(String) hs.getAttribute("1"); UserDao userDao=new UserDao(); try { userDao.deleteUser(name); } catch (ClassNotFoundException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } String msg="用户删除成功!"; req.setAttribute("msg", msg); req.getRequestDispatcher("login.jsp").forward(req, resp); } public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{ doGet(req, resp); } }
package com.zoufang.servlet; import java.io.IOException; import java.sql.SQLException; 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.zoufang.bean.User; import com.zoufang.dao.UserDao; public class Login extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ resp.setContentType("text/html;charset=uft-8"); String name=req.getParameter("name"); String pwd=req.getParameter("password"); HttpSession hs=req.getSession(); hs.setAttribute("1",name); hs.setAttribute("2", pwd); UserDao userDao =new UserDao(); User user = null; try { user = userDao.findByName(name); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(name!=null&&pwd!=null&&name.equals(user.getName())&&pwd.equals(user.getPassword())){ resp.sendRedirect("success.jsp"); }else{ String msg="你所输入的用户名不存在或者密码不正确,请重新输入!"; req.setAttribute("msg", msg); req.getRequestDispatcher("login.jsp").forward(req, resp); } } public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{ doGet(req, resp); } }
package com.zoufang.servlet; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zoufang.bean.User; import com.zoufang.dao.UserDao; public class Register extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException, ServletException{ resp.setContentType("text/html;charset=uft-8"); String name=req.getParameter("name"); String pwd=req.getParameter("password"); UserDao uDao=new UserDao(); User user = null; try { user = uDao.findByName(name); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(user.getName()); System.out.println("判断user是否存在"); if(name.equals(user.getName())){ String msg=name+"该用户名已经使用!请重新注册!"; req.setAttribute("msg", msg); req.getRequestDispatcher("register.jsp").forward(req, resp); }else{ System.out.println("检验数据库没有该用户,存入用户信息!"); User user1=new User(); user1.setName(name); user1.setPassword(pwd); try { uDao.saveUser(user1); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } String msg=name+"注册成功,请重新登陆!"; req.setAttribute("msg", msg); req.getRequestDispatcher("login.jsp").forward(req, resp); } } public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException, ServletException{ doGet(req, resp); } }
4、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="change" method="get"> <center> <table> <tr> <td><input type="text" value="请输入新的密码" name="password1"/><a >${msg1}</a></td> </tr> <tr> <td><input type="text" value="请重新输入一次密码" name="password2"/><a >${msg2}</a></td> </tr> <tr> <td><input type="submit" value="提交"/></td> </tr> </table> </center> </form> </body> </html>
<%@ 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> <center> <form action="delete" method="get"> 确定要删除此用户!</br> <input type="submit" value="确定"> </form> </center> </body> </html>
<%@ 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> <center> <a >${msg}</a> <form action="login" method="get"> <table border="1" cellspacing="0" > <tr> <td align="center" colspan="2">用户登录</td> </tr> <tr> <td>用户名:</td> <td><input type="text" name="name" ></td> </tr> <tr> <td>密 码:</td> <td><input type="text" name="password"></td> </tr> <tr> <td></td> <td> <input type="submit" value="提交"> <input type="button" onclick="window.location.href='register.jsp'" value="注册"/> </td> </tr> </table> </form> </center> </body> </html>
<%@ 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> <center> <a>欢迎注册!</a> <a>${msg}</a> <form action="register" method="get"> <table border="1" cellspacing="0"> <tr> <td align="center" colspan="2">用户登录</td> </tr> <tr> <td>用户名:</td> <td><input type="text" name="name" ></td> </tr> <tr> <td>密 码:</td> <td><input type="text" name="password"></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交"></td> </tr> </table> </form> </center> </body> </html>
<%@ 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> <center> <table border="1" cellspacing="1" style="width:200px;height:100px"> <tr> <td align="center" colspan="2">用户登录成功</td> </tr> <tr> <td align="center"> <input type="button" onclick="window.location.href='change.jsp'" value="修改"/> <input type="button" onclick="window.location.href='delete.jsp'" value="注销"/> </td> </tr> </table> </center> </body> </html> </body> </html>
5、web中的配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>LoginDemo</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>Login</servlet-name> <servlet-class>com.zoufang.servlet.Login</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <servlet> <servlet-name>Register</servlet-name> <servlet-class>com.zoufang.servlet.Register</servlet-class> </servlet> <servlet-mapping> <servlet-name>Register</servlet-name> <url-pattern>/register</url-pattern> </servlet-mapping> <servlet> <servlet-name>Delete</servlet-name> <servlet-class>com.zoufang.servlet.Delete</servlet-class> </servlet> <servlet-mapping> <servlet-name>Delete</servlet-name> <url-pattern>/delete</url-pattern> </servlet-mapping> <servlet> <servlet-name>Change</servlet-name> <servlet-class>com.zoufang.servlet.Change</servlet-class> </servlet> <servlet-mapping> <servlet-name>Change</servlet-name> <url-pattern>/change</url-pattern> </servlet-mapping> </web-app>
6、需要加入一个mysql驱动包
mysql-connector-java-5.1.7-bin.jar
经验:开始没有加这个包,数据库连接不上
时间: 2024-10-08 19:15:12