软工自学第一天
做了一个登陆,实现简单的增删改查(没写完)
1 <%-- 2 Created by IntelliJ IDEA. 3 User: Daisy 4 Date: 2019/11/15 5 Time: 19:31 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 12 <title>登陆</title> 13 <link rel="stylesheet" href="css/style.css"> 14 </head> 15 <body style="background:url('image/background.jpg')"> 16 <h1 style="font-family:微软雅黑;margin-top:130px;color:white;text-align:center;">河北金力集团公文流转系统</h1> 17 <div id="loginbox"> 18 <div> 19 <form action="Login" style="text-align: center;" method="post"> 20 <input type="text" class="logininput" name="username" style="margin-top:90px;background-image:url('image/person.jpg')" placeholder="用户名" required><br /> 21 <input type="password" class="logininput" name="password" style="margin-top:10px;background-image:url('image/password.jpg')" placeholder="密码" required><br /> 22 <div class="logininput" style="margin-left:auto;margin-right:auto;margin-top:20px;text-align:center"> 23 <input class="loginbutton" value="登陆" style="margin-right: 17px;" type="submit"> 24 <!-- <a href="register.jsp"><button class="loginbutton" id="registerbutton" style="margin-right: 24px;" type="button">注册</button></a> 25 --></div><br /> 26 </form> 27 </div> 28 </div> 29 </body> 30 </html>
连接数据库
1 package com.hbkj.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 public class DBUtil { 10 11 public static Connection getConnection() { 12 try { 13 //1.加载驱动 14 Class.forName("com.mysql.cj.jdbc.Driver").newInstance(); 15 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { 16 // TODO Auto-generated catch block 17 e.printStackTrace(); 18 } 19 String user = "root"; 20 String password = "123456"; 21 String url = "jdbc:mysql://localhost:3306/hebeikeji?useUnicode=ture&characterEncoding=utf8&serverTimezone=GMT"; 22 Connection connection = null; 23 try { 24 //2.创建连接对象 25 connection = DriverManager.getConnection(url,user,password); 26 } catch (SQLException e) { 27 // TODO Auto-generated catch block 28 System.out.println("数据库连接失败!"); 29 e.printStackTrace(); 30 } 31 32 return connection; 33 } 34 35 //3.关闭连接 36 public static void close(Connection connection ) { 37 try { 38 if (connection != null) { 39 connection.close(); 40 } 41 42 } catch (SQLException e) { 43 // TODO Auto-generated catch block 44 e.printStackTrace(); 45 } 46 } 47 public static void close(PreparedStatement preparedStatement ) { 48 try { 49 if (preparedStatement != null) { 50 preparedStatement.close(); 51 } 52 53 } catch (SQLException e) { 54 // TODO Auto-generated catch block 55 e.printStackTrace(); 56 } 57 } 58 public static void close(ResultSet resultSet ) { 59 try { 60 if (resultSet != null) { 61 resultSet.close(); 62 } 63 64 } catch (SQLException e) { 65 // TODO Auto-generated catch block 66 e.printStackTrace(); 67 } 68 } 69 }
登陆servlet
1 package com.hbkj.servlet; 2 3 import com.hbkj.dao.PlatformDao; 4 import com.hbkj.dao.UserDao; 5 import com.hbkj.model.Platform; 6 import com.hbkj.model.User; 7 8 import javax.servlet.http.HttpSession; 9 import java.io.File; 10 import java.io.IOException; 11 import java.io.PrintWriter; 12 13 public class LoginServlet extends javax.servlet.http.HttpServlet { 14 15 //HTML框架 16 private String html_head = "<html>\n" + 17 "<head>\n" + 18 " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" + 19 " <link rel=\"stylesheet\" href=\"layui/css/layui.css\">\n" + 20 " <script type=\"javascript\" src=\"layui/layui.js\" ></script>\n" + 21 " <title>Title</title>\n" + 22 "</head>\n" + 23 "<body>\n"; 24 private String html_tail = "</body>\n" + 25 "</html>"; 26 27 protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { 28 29 String username = request.getParameter("username"); 30 String password = request.getParameter("password"); 31 UserDao userDao = new UserDao(); 32 PlatformDao platformDao = new PlatformDao(); 33 User user = userDao.loadUser(username,password); 34 if (user == null) 35 { 36 PrintWriter writer = response.getWriter(); 37 writer.println(html_head + "<script>alert('用户名或密码错误');history.go(-1);</script>" + html_tail); 38 }else { 39 Platform platform = platformDao.loadPlatformByUserid(user.getId()); 40 HttpSession session = request.getSession(); 41 session.setAttribute("user",user); 42 request.setAttribute("platform",platform); 43 request.getRequestDispatcher("index.jsp").forward(request,response); 44 } 45 } 46 47 protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { 48 String absolute = getServletConfig().getServletContext().getRealPath("/"); 49 File china_json = new File(absolute + "js\\china.js"); 50 51 request.setAttribute("err_msg","非法请求,请以正规方式登陆系统"); 52 request.getRequestDispatcher("error.jsp").forward(request,response); 53 } 54 }