公文流转系统(未完成)
本篇博客说明有限时间内我对该系统的部分构建情况。
数据库连接代码:DBUtil_u.java
1 package util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 public class DBUtil_u { 10 public static String db_url = "jdbc:mysql://localhost:3306/office?useSSL=false"; 11 public static String db_user = "root"; 12 public static String db_pass = "Inazuma"; 13 14 public static Connection getConn () { 15 Connection conn = null; 16 17 try { 18 Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动 19 conn = DriverManager.getConnection(db_url, db_user, db_pass); 20 } catch (Exception e) { 21 e.printStackTrace(); 22 } 23 24 return conn; 25 } 26 27 28 public static void close (Statement state, Connection conn) { 29 if (state != null) { 30 try { 31 state.close(); 32 } catch (SQLException e) { 33 e.printStackTrace(); 34 } 35 } 36 37 if (conn != null) { 38 try { 39 conn.close(); 40 } catch (SQLException e) { 41 e.printStackTrace(); 42 } 43 } 44 } 45 46 public static void close (ResultSet rs, Statement state, Connection conn) { 47 if (rs != null) { 48 try { 49 rs.close(); 50 } catch (SQLException e) { 51 e.printStackTrace(); 52 } 53 } 54 55 if (state != null) { 56 try { 57 state.close(); 58 } catch (SQLException e) { 59 e.printStackTrace(); 60 } 61 } 62 63 if (conn != null) { 64 try { 65 conn.close(); 66 } catch (SQLException e) { 67 e.printStackTrace(); 68 } 69 } 70 } 71 72 }
后台Servlet:OfficeServlet.java
1 package Servlet; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import javax.servlet.http.HttpServlet; 9 10 import Dao.LoginDao; 11 @WebServlet("/OfficeServlet") 12 public class OfficeServlet extends HttpServlet{ 13 private static final long serialVersionUID = 1L; 14 15 LoginDao login_dao=new LoginDao(); 16 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 17 req.setCharacterEncoding("utf-8"); 18 String method = req.getParameter("method"); 19 if ("login".equals(method)) { 20 login(req,resp); 21 }else if("create".equals(method)) { 22 23 } 24 } 25 26 public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { 27 req.setCharacterEncoding("UTF-8"); 28 String user=req.getParameter("username"); 29 String pass=req.getParameter("password"); 30 31 int result=login_dao.select(user, pass); 32 if(result==0) { 33 req.setAttribute("message", "登录失败"); 34 req.getRequestDispatcher("login.jsp").forward(req,resp); 35 }else if(result==1) { 36 req.setAttribute("message", "登录成功"); 37 req.getRequestDispatcher("department.jsp").forward(req,resp); 38 }else if(result==2) { 39 req.setAttribute("message", "登录成功"); 40 req.getRequestDispatcher("officer.jsp").forward(req,resp); 41 } 42 } 43 44 public void create(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { 45 req.setCharacterEncoding("UTF-8"); 46 String path=req.getParameter("path"); 47 String word=req.getParameter("content"); 48 } 49 }
根据method传值实现不同的功能,目前只完成了登录界面的功能。下面附上Dao包代码和jsp代码一并说明。
实现数据库操作的代码(Dao包):LoginDao.java
1 package Dao; 2 3 import java.sql.Connection; 4 import java.sql.ResultSet; 5 import java.sql.Statement; 6 import util.DBUtil_u; 7 8 public class LoginDao { 9 public int select(String username,String password) { 10 int result=0; 11 Connection conn=DBUtil_u.getConn(); 12 Statement state=null; 13 String sql="select * from user"; 14 ResultSet res=null; 15 try { 16 state=conn.createStatement(); 17 res=state.executeQuery(sql); 18 while(res.next()) { 19 String JudgeUsername=res.getString("username"); 20 String JudgePassword=res.getString("password"); 21 int id=res.getInt("id"); 22 if(JudgeUsername.equals(username)&&JudgePassword.equals(password)) { 23 result=id; 24 break; 25 } 26 } 27 }catch(Exception e) { 28 e.printStackTrace(); 29 }finally { 30 DBUtil_u.close(res, state, conn); 31 } 32 return result; 33 } 34 }
利用OfficeServlet抓取登录页面中文本框的值,传入这里实行数据库的遍历操作,数据库中已经存入了一些用户名和密码,核实存在则将其id赋值给result,最后返回result值。在OfficeServlet中根据result值判断是否登录成功,若登录成功则根据其数值跳转至不同的操作页面。
数据库(user):存储用户名和密码
登录设置页面:login.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>登录</title> 8 <style type="text/css"> 9 table{ 10 margin:auto; 11 } 12 </style> 13 </head> 14 <% 15 Object message=request.getAttribute("message"); 16 if(message!=null&&!"".equals(message)){ 17 %> 18 <script type="text/javascript"> 19 alert("<%=request.getAttribute("message")%>"); 20 </script> 21 <%} %> 22 <body style="text-align: center;"> 23 <form action="OfficeServlet?method=login"method="post"> 24 <h2>登录界面</h2> 25 <table border="0"> 26 <tr> 27 <th>用户名:</th><td><input type="text"name="username"style="width:135px"></td> 28 </tr> 29 <tr> 30 <th>密码:</th><td><input type="password"name="password"style="width:135px"></td> 31 </tr> 32 </table> 33 <button type="submit">登录</button> 34 </form> 35 </body> 36 </html>
仅提供登录界面窗口,后期将修改代码进行界面美化。
部门功能页面:department.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>部门</title> 8 </head> 9 <% 10 Object message=request.getAttribute("message"); 11 if(message!=null&&!"".equals(message)){ 12 %> 13 <script type="text/javascript"> 14 alert("<%=request.getAttribute("message")%>"); 15 </script> 16 <%} %> 17 <body style="text-align: center;"> 18 <h2>部门功能</h2> 19 <a href="Create.jsp">公文拟制</a><br> 20 <a href="Receive.jsp">签收公文</a><br> 21 <a href="Scan.jsp">浏览公文</a><br> 22 <a href="login.jsp">返回登录界面</a> 23 </body> 24 </html>
利用超链接实现页面跳转,其中公文拟制,签收公文,浏览公文的JSP页面尚未构建完成。
数据库(test):存储文件上传时间,文件路径,文件名。
未完成的Create.jsp(公文拟制):
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>公文拟制</title> 8 <style type="text/css"> 9 table{ 10 margin:auto; 11 } 12 </style> 13 </head> 14 <% 15 Object message=request.getAttribute("message"); 16 if(message!=null&&!"".equals(message)){ 17 %> 18 <script type="text/javascript"> 19 alert("<%=request.getAttribute("message")%>"); 20 </script> 21 <%} %> 22 <body style="text-align: center;"> 23 <h2>编辑公文</h2> 24 <form action="OfficeServlet?method=create"method="post"> 25 <table> 26 <tr> 27 <th>文件名:</th><td><input type="text"name="path"><td> 28 </tr> 29 <tr> 30 <td></td><td><b>文件保存在E:\PATH文件夹下</b></td> 31 </tr> 32 <tr> 33 <th>编写文件内容:</th><td><TextArea name="content" rows="7" cols="40"></TextArea></td> 34 </tr> 35 </table> 36 <button type="submit">提交</button> 37 </form> 38 </body> 39 </html>
思路说明:
1.初步构想:在“text”文本框中写入文件名,由于指定了文件存储路径,在后台处理时根据其文件名构建其文件路径,一并存入test数据库中,浏览时按照题目要求将文件名作为超链接,点击时根据文件名获取相应的文件路径,最后在页面中读取显示即可。
数据库中time已经设置时间戳,自动记录时间,查找时可根据查询条件进行遍历查找。
2.发现:在查找资料时查找到<input type="file">上传文件操作,后期整改时会改用这个思路。