JSP第十一周作业


1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)

2.建model层
entity,dao包

3.登陆,注册,登陆后显示全部邮件

 1 package com.lwk.email.dao;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 
 8 import com.lwk.email.entity.User;
 9 import com.lwk.email.util.DbUtil;
10 
11 public class UserDao {
12     public void userReg(User user){
13         String sql = "insert into user(uname,password) value ('"+ user.getUname() +"','"+user.getPassword()+"')";
14         Connection con = DbUtil.getCon();
15         try {
16             PreparedStatement pred = con.prepareStatement(sql);
17 //            pred.setString(1, user.getUname());
18 //            pred.setString(2, user.getPassword());
19 //            pred.setInt(3, user.getAge());
20             pred.executeUpdate();
21             pred.close();
22         } catch (SQLException e) {
23             // TODO Auto-generated catch block
24             e.printStackTrace();
25         }
26         try {
27             con.close();
28         } catch (SQLException e) {
29             // TODO Auto-generated catch block
30             e.printStackTrace();
31         }
32     }
33     
34     public User getUserdentByName(String uname){
35         String sql = "select * from user where uname='"+uname+"'";
36         Connection con = null;
37         PreparedStatement pred = null;
38         ResultSet res = null;
39         con = DbUtil.getCon();
40         try {
41             pred = con.prepareStatement(sql);
42 //            pred.setString(1, uname);
43             res = pred.executeQuery();
44             User user = null;
45             if (res.next()) {
46                 user = new User(res.getString("uname"), res.getString("password"));
47             }
48             return user;
49         } catch (SQLException e) {
50             // TODO Auto-generated catch block
51             e.printStackTrace();
52         }finally{
53             DbUtil.closeAll(con, pred, res);
54         }
55         return null;
56     }
57 }
 1 package com.lwk.email.dao;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.util.ArrayList;
 8 import java.util.List;
 9 
10 import com.lwk.email.entity.Msg;
11 import com.lwk.email.util.DbUtil;
12 
13 public class MsgDao {
14 
15     public List<Msg> getMailByReceiver(String name) {
16         List<Msg> list = new ArrayList<Msg>();
17         Connection con = DbUtil.getCon();
18         String sql = "select * from msg where sendto=?";
19         PreparedStatement ps=null;
20         ResultSet rs=null;
21         try {
22             ps = con.prepareStatement(sql);
23             ps.setString(1, name);
24             rs = ps.executeQuery();
25             while (rs.next()) {
26                 Msg m = new Msg();
27                 m.setMsgid(rs.getInt("eid"));
28                 m.setUsername(rs.getString("uname"));
29                 m.setTitle(rs.getString("title"));
30                 m.setMsgcontent(rs.getString("msgcontent"));
31                 m.setState(rs.getInt("state"));
32                 m.setSendto(rs.getString("sendto"));
33                 m.setMsg_create_date(rs.getDate("msg_create_date"));
34                 list.add(m);
35             }
36         } catch (SQLException e) {
37             e.printStackTrace();
38         }finally{
39         }
40         return list;
41 
42     }
43 
44 }
 1 package com.lwk.email.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     static {
11         try {
12             Class.forName("com.mysql.jdbc.Driver");
13         } catch (ClassNotFoundException e) {
14             // TODO Auto-generated catch block
15             e.printStackTrace();
16         }
17     }
18 
19     public static Connection getCon() {
20         Connection con = null;
21         try {
22             con = DriverManager.getConnection(
23                     "jdbc:mysql://localhost:3306/email", "root", "123456");
24         } catch (SQLException e) {
25             // TODO Auto-generated catch block
26             e.printStackTrace();
27         }
28         return con;
29     }
30     
31     // 关闭连接
32     public static void closeAll(Connection con, PreparedStatement ps, ResultSet rs) {
33         try {
34             if (rs != null)
35                 rs.close();
36             if (ps != null)
37                 ps.close();
38             if (con != null)
39                 con.close();
40         } catch (SQLException e) {
41             // TODO Auto-generated catch block
42             e.printStackTrace();
43         }
44     }
45 }
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 
 3 <!DOCTYPE HTML>
 4 <html>
 5 <head>
 6 <title>登录</title>
 7 </head>
 8 <body>
 9     <form name="formName" action="dologin.jsp" method="post">
10         用户名:<input type="text" name="uname"><br> 密码:<input
11             type="password" name="password"><br> <input
12             type="button" value="登录" onclick="toDoLogin()"> <input
13             type="button" value="注册" onclick="toReg()">
14     </form>
15     <script>
16         function toReg() {
17             window.location.href = "reg.jsp";
18         }
19         function toDoLogin() {
20             if (formName.uname.value == "" || formName.password.value == "") {
21                 alert("用户名或密码不能为空");
22             }else{
23                 formName.submit();
24             }
25         }
26     </script>
27 </body>
28 </html>
 1 <%@page import="com.lwk.email.entity.User"%>
 2 <%@page import="com.lwk.email.dao.UserDao"%>
 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 4 
 5 <!DOCTYPE HTML>
 6 <html>
 7 <head>
 8 <title></title>
 9 </head>
10 <body>
11     <%
12         request.setCharacterEncoding("UTF-8");
13             response.setCharacterEncoding("UTF-8");
14             String uname = request.getParameter("uname");
15             String password = request.getParameter("password");
16             UserDao userdentDao = new UserDao();
17             User user = userdentDao.getUserdentByName(uname);
18             if (password.equals(user.getPassword())) {
19             session.setAttribute("uname", uname);
20         response.sendRedirect("index.jsp");
21             } else {
22         response.sendRedirect("login.jsp");
23             }
24     %>
25 </body>
26 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 
 3 <!DOCTYPE HTML>
 4 <html>
 5 <head>
 6 <title>注册</title>
 7 </head>
 8 <body>
 9     <form action="doreg.jsp" method="post">
10         用户名:<input type="text" name="uname"><br> 
11         密码:<input type="password" name="password"><br>
12         <input type="submit" value="提交"> 
13     </form>
14 </body>
15 </html>
 1 <%@page import="com.lwk.email.dao.UserDao"%>
 2 <%@page import="com.lwk.email.entity.User"%>
 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 4 
 5 <!DOCTYPE HTML>
 6 <html>
 7 <head>
 8 <title></title>
 9 </head>
10 <body>
11 <%
12     request.setCharacterEncoding("UTF-8");
13     response.setCharacterEncoding("UTF-8");
14     //接收表单传值
15     String uname = request.getParameter("uname");
16     String password = request.getParameter("password");
17     User user = new User(uname,password);
18     UserDao userDao = new UserDao();
19     userDao.userReg(user);
20     response.sendRedirect("login.jsp");
21  %>
22 </body>
23 </html>
 1 <%@page import="com.lwk.email.entity.Msg"%>
 2 <%@page import="com.lwk.email.dao.MsgDao"%>
 3 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 4 <!DOCTYPE HTML>
 5 <html>
 6 <head>
 7 <title>My JSP 'index.jsp' starting page</title>
 8 <style>
 9 table {
10     border: 2px solid black;
11     border-collapse: collapse;
12 }
13 table tr td{
14 border: 2px solid black
15 }
16 </style>
17 </head>
18 
19 <body>
20     <%
21         String uname = (String) session.getAttribute("uname");
22     %>
23     欢迎你<%
24         out.print(uname);
25     %>使用邮箱
26     <hr>
27     <table>
28         <tr>
29             <td>id</td>
30             <td>发件人</td>
31             <td>标题</td>
32             <td>内容</td>
33             <td>已读未读状态</td>
34             <td>收件人</td>
35             <td>发送时间</td>
36         </tr>
37         <%
38             MsgDao md = new MsgDao();
39             List<Msg> list = md.getMailByReceiver(uname);
40             for (Msg m : list) {
41         %>
42         <tr>
43             <td><%=m.getMsgid()%></td>
44             <td><%=m.getUsername()%></td>
45             <td><%=m.getTitle()%></td>
46             <td><%=m.getMsgcontent()%></td>
47             <td>
48                 <%
49                     int state = m.getState();
50                         if (state == 1) {
51                 %> <img src="image/yidu.png"> <%
52      ;
53          } else {
54  %> <img src="image/weidu.png"> <%
55      ;
56          }
57  %>
58             </td>
59             <td><%=m.getSendto()%></td>
60             <td><%=m.getMsg_create_date()%></td>
61         </tr>
62         <%
63             }
64         %>
65     </table>
66 </body>
67 </html>

 

posted @ 2022-05-15 13:07  Lwk36  阅读(14)  评论(0编辑  收藏  举报