总结:
这次三小时的代码编写,没有写出不同用户登录看到一个界面的不同部分,也就是说,没有完成登录部分,也没有完成公文浏览等功能
这两天再继续做吧。
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <div align="center"><h2>公文流转系统</h2></div> <div align="center"> <form action="Loginservlet?method=login" method="post" > <div> <label for="username">用户名</label> <input type="text" id="username" name="username"> </div> <div> <label for="password" >密码</label> <input type="password" id="password" name="password"> </div> <div> <input type="button" value="登录"> </div> </form> </div> </body> </html>
Loginservlet.java
package Loginservlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.official.util.DBUtil; import bean.Users; public class Loginservlet { @WebServlet("/Loginservlet") public class Servlet extends HttpServlet { private static final long serialVersionUID = 1L; public Servlet() { super(); } protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("login".equals(method)) { login(req, resp); } } public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String username = req.getParameter("username"); String password = req.getParameter("password"); Users user=new Users(); user.setUsername(username); user.setPassword(password); HttpServletResponse response = null; if(DBUtil.log_isExist(user)) { Cookie cookie=new Cookie("username", username); cookie.setPath("/"); cookie.setMaxAge(60*60*24); response.addCookie(cookie); Users users=DBUtil.getUserByUsername(user); int pid=users.getPermissionId(); Cookie cookie2=new Cookie("pid", Integer.toString(pid)); cookie2.setPath("/"); cookie2.setMaxAge(60*60*24); response.addCookie(cookie2); response.getWriter().write("yes"); } else { response.getWriter().write("no"); } } } }
Users.jsp
package bean; public class Users { private int id; private String username; private String password; private int permissionId; private String job; public int getId() { return id; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } 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; } public int getPermissionId() { return permissionId; } public void setPermissionId(int permissionId) { this.permissionId = permissionId; } public void setStatus(int status) { this.status = status; } private int status; public int getStatus() { return status; } }
permission.java
package bean; public class Permission { private int id; private int permission; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getPermission() { return permission; } public void setPermission(int permission) { this.permission = permission; } }
DBUtil.java
package com.official.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; //import org.apache.catalina.User; import bean.Doc; import bean.Permission; import bean.Users; public class DBUtil { private static final String connectionURL="jdbc:mysql://localhost:3306/user?serverTimezone=UTC"; private static final String username="root"; private static final String password="123"; public static Connection getConnection() { try { Class.forName("com.mysql.cj.jdbc.Driver"); return DriverManager.getConnection(connectionURL,username,password); } catch (Exception e) { // TODO: handle exception System.out.println("���ݿ�����ʧ��"); e.printStackTrace(); return null; } } public static boolean log_isExist(Users user) { Connection con=null; PreparedStatement pstmt=null; ResultSet rs=null; try { con=getConnection(); String sql_query="select * from users where username = '"+user.getUsername()+"' and password = '"+user.getPassword()+"' and status != 0"; System.out.println(sql_query); pstmt=con.prepareStatement(sql_query); rs=pstmt.executeQuery(); if(rs.next()==false) { System.out.println("�û������������"); return false; } else { System.out.println("�û�����������ȷ"); return true; } } catch (SQLException e) { System.out.println("δ����"); e.printStackTrace(); } finally { closeAll(con, pstmt, rs); } return false; } }