• 欢迎光临~

jsp11

开发技术 开发技术 2022-05-29 次浏览
第十二周上机作业(邮件功能)的控制层代码改用为servlet实现。
 1 package com.gd.dao;
 2 import java.sql.Connection;
 3 import java.sql.DriverManager;
 4 import java.sql.PreparedStatement;
 5 import java.sql.SQLException;
 6 import java.sql.ResultSet;
 7 
 8 
 9 
10 
11 public class BaseDao {
12     //获取连接
13     protected Connection getConnection(){
14         Connection conn=null;
15         try{
16             Class.forName("com.mysql.jdbc.Driver");
17             conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","lin0520...");
18             
19         }catch(Exception e){
20             e.printStackTrace();
21         }
22         return conn;
23 
24 }
25 
26 
27 // 关闭连接
28     protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){
29         try{
30             if(rs!=null)
31                 rs.close();
32             if(ps!=null)
33                 ps.close();
34             if(con!=null)
35                 con.close();            
36         }catch(SQLException e){
37             e.printStackTrace();
38         }
39     }
40 }
  1 package com.gd.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.Date;
  9 import java.util.List;
 10 
 11 import com.gd.entity.msg;
 12 
 13 public class msgDao extends BaseDao{
 14     //关于邮件的增删改查    
 15     //添加邮件(写邮件,回复邮件都调用,邮件状态为1(未读),时间为系统当前时间)
 16     
 17     public void addMsg(msg m){
 18         Connection conn=getConnection();
 19         String sql="insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,1,?,?)";
 20         PreparedStatement ps=null;
 21         try {
 22              ps=conn.prepareStatement(sql);
 23              ps.setString(1, m.getUsername());
 24              ps.setString(2, m.getTitle());
 25              ps.setString(3, m.getMsgcontent());
 26              ps.setString(4, m.getSendto());
 27              ps.setDate(5, new java.sql.Date(new Date().getTime()));
 28              ps.executeUpdate();
 29         } catch (SQLException e) {
 30             // TODO Auto-generated catch block
 31             e.printStackTrace();
 32         }finally{
 33             closeAll(conn, ps, null);
 34         }        
 35     }
 36     public static void main(String[] args) {
 37         msgDao md=new msgDao();
 38         md.delMsg(4);
 39     }    
 40     
 41     //根据id删除邮件
 42     public void delMsg(int id){
 43         Connection con=getConnection();
 44         String sql="delete from msg where msgid="+id;
 45         PreparedStatement ps=null;
 46         try {
 47             ps=con.prepareStatement(sql);
 48             ps.executeUpdate();
 49         } catch (SQLException e) {
 50             // TODO Auto-generated catch block
 51             e.printStackTrace();
 52         }finally{
 53             closeAll(con, ps, null);
 54         }
 55         
 56     }
 57     
 58     
 59     
 60     
 61     //根据id修改邮件状态(统一改成1)
 62     
 63     
 64     //根据收件人查看全部邮件
 65     public List<msg> getMailByReceiver(String name){
 66         List<msg> list=new ArrayList<msg>();
 67         Connection con=getConnection();
 68         String sql="select * from msg where sendto=?";
 69         PreparedStatement ps=null;
 70         ResultSet rs=null;
 71         try {
 72             ps = con.prepareStatement(sql);
 73             ps.setString(1, name);
 74              rs=ps.executeQuery();
 75             while(rs.next()){
 76                 //每读取一行,创建一个msg对象,对象放到集合中
 77                 msg m=new msg();
 78                 m.setMsgid(rs.getInt(1));
 79                 m.setUsername(rs.getString(2));
 80                 m.setTitle(rs.getString(3));
 81                 m.setMsgcontent(rs.getString(4));
 82                 m.setState(rs.getInt(5));
 83                 m.setSendto(rs.getString(6));
 84                 m.setMsg_create_date(rs.getDate(7));
 85                 list.add(m);                
 86             }            
 87             
 88         } catch (SQLException e) {
 89             // TODO Auto-generated catch block
 90             e.printStackTrace();
 91         }finally{
 92             closeAll(con, ps, rs);
 93         }        
 94         return list;
 95     }
 96     
 97     
 98     
 99     
100 
101 }
 1 package com.gd.dao;
 2 import java.sql.Connection;
 3 import java.sql.PreparedStatement;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 
 7 import com.gd.entity.users;
 8 public class usersDao extends BaseDao{
 9 //关于用户的增删改查
10     public boolean denglu(String uname,String upwd){
11         boolean f=false;
12         Connection conn=getConnection();
13         String sql=" select * from users where unmae=? and upwd=?";
14         PreparedStatement ps = null;
15         ResultSet rs= null;
16         try {
17         ps=    conn.prepareStatement(sql);
18         ps.setString(1,uname);
19         ps.setString(2,upwd);
20         rs=ps.executeQuery();
21         if(rs.next())
22             f=true;
23         } catch (SQLException e) {
24             // TODO: handle exception
25             e.printStackTrace();
26         } finally{
27             closeAll(conn,ps,rs);
28         }
29         return f;                
30     }
31     
32     public void addUser(users u){
33         
34         
35         
36     }
37 }
package com.gd.entity;

import java.util.Date;

public class msg {
    private int msgid;
    private String username;
    private String title;
    private String msgcontent;
    private int state;
    private String sendto;
    private Date msg_create_date;
    public int getMsgid() {
        return msgid;
    }
    public void setMsgid(int msgid) {
        this.msgid = msgid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getMsgcontent() {
        return msgcontent;
    }
    public void setMsgcontent(String msgcontent) {
        this.msgcontent = msgcontent;
    }
    public int getState() {
        return state;
    }
    public void setState(int state) {
        this.state = state;
    }
    public String getSendto() {
        return sendto;
    }
    public void setSendto(String sendto) {
        this.sendto = sendto;
    }
    public Date getMsg_create_date() {
        return msg_create_date;
    }
    public void setMsg_create_date(Date msg_create_date) {
        this.msg_create_date = msg_create_date;
    }

}
 1 package com.gd.entity;
 2 
 3 public class users {
 4     int id;
 5     String uname;
 6     String upwd;
 7     public int getId() {
 8         return id;
 9     }
10     public void setId(int id) {
11         this.id = id;
12     }
13     public String getUname() {
14         return uname;
15     }
16     public void setUname(String uname) {
17         this.uname = uname;
18     }
19     public String getUpwd() {
20         return upwd;
21     }
22     public void setUpwd(String upwd) {
23         this.upwd = upwd;
24     }
25 
26 }
 1 <%@page import="com.gd.dao.msgDao"%>
 2 <%@page import="com.gd.entity.users"%>
 3 <%@page import="com.gd.dao.usersDao"%>
 4 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 5 <%
 6     request.setCharacterEncoding("utf-8");
 7     int mid=Integer.parseInt(request.getParameter("mid"));
 8     MsgDao md=new MsgDao();
 9     md.delMsg(mid);
10     request.getRequestDispatcher("main.jsp").forward(request, response);
11 
12 
13 %>
 1 <%@page import="com.gd.dao.usersDao"%>
 2 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 3 <%
 4     request.setCharacterEncoding("utf-8");
 5     String uname=request.getParameter("uname");    
 6     String upwd=request.getParameter("upwd");
 7     usersDao ud=new usersDao();
 8     if(ud.login(uname, upwd)){
 9         session.setAttribute("uname", uname);
10         request.getRequestDispatcher("main.jsp").forward(request, response);
11     }else{
12         response.sendRedirect("no.jsp");
13     }
14 
15 
16 %>
 1 <%@page import="com.gd.entity.users"%>
 2 <%@page import="com.gd.dao.usersDao"%>
 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 4 <%
 5     request.setCharacterEncoding("utf-8");
 6     String uname=request.getParameter("username");    
 7     String upwd=request.getParameter("password");
 8     String email=request.getParameter("email");
 9     
10     usersDao ud=new usersDao();
11     users u=new users(uname,upwd,email);
12     ud.register(u);
13     response.sendRedirect("login.jsp");
14 
15 
16 %>
 1 <%@page import="com.gd.dao.msgDao"%>
 2 <%@page import="com.gd.entity.msg"%>
 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 4 <%
 5     request.setCharacterEncoding("utf-8");
 6     String title = request.getParameter("title");
 7     String content = request.getParameter("content");
 8     String receiver = request.getParameter("sendto");
 9     String sender = (String) session.getAttribute("uname");
10 
11     msg m = new msg();
12     m.setSendto(receiver);
13     m.setUname(sender);
14     m.setTitle(title);
15     m.setMsgcontent(content);
16 
17     msgDao md = new msgDao();
18     md.writeMsg(m);
19     request.getRequestDispatcher("main.jsp").forward(request, response);
20 %>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'login.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26 <form action="dologin.jsp" method="post">
27 用户名:<input type="text" name="uname" value="ls" /><Br>
28 密码 :<input type="password" name="upwd" value="456"/><br>
29 
30 <input type="submit" value="登录">
31 <a href="register.jsp" >注册</a>
32 
33 </form>
34   </body>
35 </html>
 1 <%@page import="com.gd.entity.msg"%>
 2 <%@page import="com.gd.dao.msgDao"%>
 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 4 <%
 5     String path = request.getContextPath();
 6     String basePath = request.getScheme() + "://"
 7             + request.getServerName() + ":" + request.getServerPort()
 8             + path + "/";
 9 %>
10 
11 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
12 <html>
13 <head>
14 <base href="<%=basePath%>">
15 
16 <title>My JSP 'login.jsp' starting page</title>
17 
18 
19 
20 </head>
21 
22 <body>
23     <%
24         msgDao md = new msgDao();
25         String uname = (String) session.getAttribute("uname");
26         List<msg> list = md.getReceive(uname);
27     %>
28     欢迎你<%=uname%> <a href="write.jsp">写邮件</a>
29     <table border="1">
30     <tr>
31     
32     <td>标题    </td>
33     
34     <td>是否已读</td>
35     <td>发件人</td>
36     <td>时间</td>    
37     <td>操作</td>
38     <td>操作</td>
39     </tr>
40 <%for(int i=0;i<list.size();i++){ %>    
41     <tr>
42     
43     <td><a href="readMsg.jsp?mid=<%=list.get(i).getMsgid()%>"><%=list.get(i).getTitle() %></a></td>
44     
45     <td><%int x=list.get(i).getState();
46             if(x==0){%>
47             <img src="images/sms_readed.png">
48             <%}else{ %>
49             <img src="images/sms_unReaded.png">
50             <%} %>
51     </td>
52     <td><%=list.get(i).getUname() %></td>
53     <td><%=list.get(i).getMsg_create_date() %></td>
54     <td><a href="dodel.jsp?mid=<%=list.get(i).getMsgid()%>">删除</a></td>
55     <td><a href="write.jsp?reply=<%=list.get(i).getUname()%>">回复</td>
56     
57     
58     </tr>
59     
60 <%} %>
61     
62     
63     </table>
64 
65 
66 
67 </body>
68 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'login.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26 登录失败。
27   </body>
28 </html>
<%@page import="com.gd.entity.msg"%>
<%@page import="com.gd.dao.msgDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
  <head>    
    <title>读短消息</title>        
  </head>
    <body>
        <div id="main">
            <div class="mainbox">
                <div class="title readMessage png"></div>
                <div class="menu">
                    <span>当前用户:<a href="main.jsp">${sessionScope.loginuser}</a></span>
                    <span><a href="UserServlet?action=findUsers">发短消息</a></span>
                    <span><a href="UserServlet?action=logout">退出</a></span>
                </div>
                <div class="content">
                    <div class="message">
                        <%
                        int mid=Integer.parseInt(request.getParameter("mid"));
                        msgDao md=new msgDao();
                        msg m=md.getMsgById(mid);
                        md.updateMsg(mid);
                         %>
                         
                         发件人<%=m.getUname() %><br>
                         发件时间<%=m.getMsg_create_date() %><br>
                         标题:<%=m.getTitle() %><br>
                         详情:<%=m.getMsgcontent() %><br>
                            </div>
                       <a href="write.jsp?reply=<%=m.getUname()%>">回复</a>
                       <a href="main.jsp">返回首页</a>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <head>
 9 
10 <title>学士后 在线短信平台</title>
11 <link type="text/css" rel="stylesheet" href="css/sms.css" />
12 <script type="text/javascript" src="scripts/jquery.js"></script>
13 </head>
14 <script type="text/javascript">
15     function check(){
16         var username = document.getElementById("username");
17         var password = document.getElementById("password");
18         var affirm = document.getElementById("affirm");
19         var email = document.getElementById("email");
20         if(username.value == ""){
21             alert("用户名不能为空!");
22             return false;
23         }else if(password.value == ""){
24             alert("密码不能为空!");
25             return false;
26         }else if(password.value != affirm.value){
27             alert("两次密码不同!");
28             return false;
29         }else if(email.value == ""){
30             alert("邮箱不能为空!");
31             return false;
32         }
33         return true;
34     }
35 </script>
36 <body>
37 <div id="regTitle" class="png"></div>
38 <div id="regForm" class="userForm png">
39 
40     <form action="doreg.jsp"  method="post">
41         <dl>
42           
43             <dt>用 户 名:</dt>
44             <dd><input type="text" name="username" /></dd>
45             <dt>密  码:</dt>
46             <dd><input type="password" name="password" /></dd>
47             <dt>确认密码:</dt>
48             <dd><input type="password" name="affirm" /></dd>
49             <dt>邮  箱:</dt>
50             <dd><input type="text" name="email" /></dd>
51         </dl>
52         <div class="buttons">
53             <input  type="submit" name="register" value="注册 " /><input  type="reset" name="reset" value="重置 " />
54         </div>
55         <div class="goback"><a href="index.jsp" class="png">返回登录页</a></div>
56     </form>
57 </div>
58 </body>
59 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'login.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26 注册成功,<a href="login.jsp">返回到登录页面</a>
27   </body>
28 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'login.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26 <form action="dowrite.jsp"  method="post">
27 收件人:<input type="text" name="sendto" value="<%=request.getParameter("reply") %>" ><br>
28 主题:<input type="text" name="title"><br>
29 内容:<textarea name="content"></textarea>
30 <input type="submit" value="发送">
31 
32 
33 </form>
34 
35 
36   </body>
37 </html>

jsp11

 

程序员灯塔
转载请注明原文链接:jsp11
喜欢 (0)