0308 登录
代码展示
新建一个login.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>Insert title here</title> </head> <body> <form action="/WEB02/LoginServlet" method="post"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="pwd" ><br> <input type="submit" value="登录"> </form> </body> </html>
新建一个LoginServlet类
public class LoginServlet extends HttpServlet { public void init() throws ServletException { //计数器 int sum=0; //获取ServletContext对象 ServletContext context=getServletContext(); //存入 context.setAttribute("sum", sum); } private UserService userService=new UserService(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取用户名和密码 String uname=request.getParameter("username"); String pwd=request.getParameter("pwd"); int count=userService.login(uname, pwd); if(count>0){ //登录成功取出计数器佳佳 int sum=(int)getServletContext().getAttribute("sum"); sum++; //存入计数器 getServletContext().setAttribute("sum", sum); response.getWriter().write("you are "+sum+" ok!"); }else{ response.getWriter().write("no"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
在service层中创建 userService
public class UserService { private UserDao userDao=new UserDao(); public int login(String uname,String pwd){ int count=0; try { count=userDao.login(uname, pwd); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return count; } }
在dao层中创建userdao
public class UserDao { public int login(String uname,String pwd) throws SQLException{ //获取链接 Connection conn=JDBCUtils.getConn(); //获取sql语句 String sql="select count(*) from user where uname=? and pwd=?"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, uname); pst.setString(2, pwd); //执行 ResultSet rs=pst.executeQuery(); //处理结果集 int count=0; while(rs.next()){ count=rs.getInt(1); } JDBCUtils.close(conn, pst, rs); return count; } }
运用到工具类JDBCUtils
public class JDBCUtils { //获取链接对象 public static Connection getConn(){ Connection conn=null; try { //注册驱动 Class.forName("com.mysql.jdbc.Driver"); //获得链接对象 String url="jdbc:mysql://localhost:3306/java1127?characterEncoding=utf-8"; String user="root"; String pwd="123456"; conn=DriverManager.getConnection(url,user,pwd); } catch (ClassNotFoundException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } //增删改释放资源 public static void close(Connection conn,PreparedStatement pst){ if(pst!=null){ try { pst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //查询释放资源 public static void close(Connection conn,PreparedStatement pst,ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(pst!=null){ try { pst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }