十三周作业
package com.cqq.servlet; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.ResultSet; public class BaseDao { //获取连接 protected Connection getConnection(){ Connection conn=null; try{ Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","lin0520..."); }catch(Exception e){ e.printStackTrace(); } return conn; } // 关闭连接 protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){ try{ if(rs!=null) rs.close(); if(ps!=null) ps.close(); if(con!=null) con.close(); }catch(SQLException e){ e.printStackTrace(); } } }
package com.gd.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.gd.entity.msg; public class msgDao extends BaseDao{ //关于邮件的增删改查 //添加邮件(写邮件,回复邮件都调用,邮件状态为1(未读),时间为系统当前时间) public void addMsg(msg m){ Connection conn=getConnection(); String sql="insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,1,?,?)"; PreparedStatement ps=null; try { ps=conn.prepareStatement(sql); ps.setString(1, m.getUsername()); ps.setString(2, m.getTitle()); ps.setString(3, m.getMsgcontent()); ps.setString(4, m.getSendto()); ps.setDate(5, new java.sql.Date(new Date().getTime())); ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(conn, ps, null); } } public static void main(String[] args) { msgDao md=new msgDao(); md.delMsg(4); } //根据id删除邮件 public void delMsg(int id){ Connection con=getConnection(); String sql="delete from msg where msgid="+id; PreparedStatement ps=null; try { ps=con.prepareStatement(sql); ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(con, ps, null); } } //根据id修改邮件状态(统一改成1) //根据收件人查看全部邮件 public List<msg> getMailByReceiver(String name){ List<msg> list=new ArrayList<msg>(); Connection con=getConnection(); String sql="select * from msg where sendto=?"; PreparedStatement ps=null; ResultSet rs=null; try { ps = con.prepareStatement(sql); ps.setString(1, name); rs=ps.executeQuery(); while(rs.next()){ //每读取一行,创建一个msg对象,对象放到集合中 msg m=new msg(); m.setMsgid(rs.getInt(1)); m.setUsername(rs.getString(2)); m.setTitle(rs.getString(3)); m.setMsgcontent(rs.getString(4)); m.setState(rs.getInt(5)); m.setSendto(rs.getString(6)); m.setMsg_create_date(rs.getDate(7)); list.add(m); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(con, ps, rs); } return list; } }
package com.gd.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.gd.entity.users; public class usersDao extends BaseDao{ //关于用户的增删改查 public boolean denglu(String uname,String upwd){ boolean f=false; Connection conn=getConnection(); String sql=" select * from users where unmae=? and upwd=?"; PreparedStatement ps = null; ResultSet rs= null; try { ps= conn.prepareStatement(sql); ps.setString(1,uname); ps.setString(2,upwd); rs=ps.executeQuery(); if(rs.next()) f=true; } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); } finally{ closeAll(conn,ps,rs); } return f; } public void addUser(users u){ } }
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; } }
package com.gd.entity; public class users { int id; String uname; String upwd; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } }
<%@page import="com.gd.dao.msgDao"%> <%@page import="com.gd.entity.users"%> <%@page import="com.gd.dao.usersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); int mid=Integer.parseInt(request.getParameter("mid")); MsgDao md=new MsgDao(); md.delMsg(mid); request.getRequestDispatcher("main.jsp").forward(request, response); %>
<%@page import="com.gd.dao.usersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); String uname=request.getParameter("uname"); String upwd=request.getParameter("upwd"); usersDao ud=new usersDao(); if(ud.login(uname, upwd)){ session.setAttribute("uname", uname); request.getRequestDispatcher("main.jsp").forward(request, response); }else{ response.sendRedirect("no.jsp"); } %>
<%@page import="com.gd.entity.users"%> <%@page import="com.gd.dao.usersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); String uname=request.getParameter("username"); String upwd=request.getParameter("password"); String email=request.getParameter("email"); usersDao ud=new usersDao(); users u=new users(uname,upwd,email); ud.register(u); response.sendRedirect("login.jsp"); %>
<%@page import="com.gd.dao.msgDao"%> <%@page import="com.gd.entity.msg"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); String title = request.getParameter("title"); String content = request.getParameter("content"); String receiver = request.getParameter("sendto"); String sender = (String) session.getAttribute("uname"); msg m = new msg(); m.setSendto(receiver); m.setUname(sender); m.setTitle(title); m.setMsgcontent(content); msgDao md = new msgDao(); md.writeMsg(m); request.getRequestDispatcher("main.jsp").forward(request, response); %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="dologin.jsp" method="post"> 用户名:<input type="text" name="uname" value="ls" /><Br> 密码 :<input type="password" name="upwd" value="456"/><br> <input type="submit" value="登录"> <a href="register.jsp" >注册</a> </form> </body> </html>
<%@page import="com.gd.entity.msg"%> <%@page import="com.gd.dao.msgDao"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> </head> <body> <% msgDao md = new msgDao(); String uname = (String) session.getAttribute("uname"); List<msg> list = md.getReceive(uname); %> 欢迎你<%=uname%> <a href="write.jsp">写邮件</a> <table border="1"> <tr> <td>标题 </td> <td>是否已读</td> <td>发件人</td> <td>时间</td> <td>操作</td> <td>操作</td> </tr> <%for(int i=0;i<list.size();i++){ %> <tr> <td><a href="readMsg.jsp?mid=<%=list.get(i).getMsgid()%>"><%=list.get(i).getTitle() %></a></td> <td><%int x=list.get(i).getState(); if(x==0){%> <img src="images/sms_readed.png"> <%}else{ %> <img src="images/sms_unReaded.png"> <%} %> </td> <td><%=list.get(i).getUname() %></td> <td><%=list.get(i).getMsg_create_date() %></td> <td><a href="dodel.jsp?mid=<%=list.get(i).getMsgid()%>">删除</a></td> <td><a href="write.jsp?reply=<%=list.get(i).getUname()%>">回复</td> </tr> <%} %> </table> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 登录失败。 </body> </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>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <head> <title>学士后 在线短信平台</title> <link type="text/css" rel="stylesheet" href="css/sms.css" /> <script type="text/javascript" src="scripts/jquery.js"></script> </head> <script type="text/javascript"> function check(){ var username = document.getElementById("username"); var password = document.getElementById("password"); var affirm = document.getElementById("affirm"); var email = document.getElementById("email"); if(username.value == ""){ alert("用户名不能为空!"); return false; }else if(password.value == ""){ alert("密码不能为空!"); return false; }else if(password.value != affirm.value){ alert("两次密码不同!"); return false; }else if(email.value == ""){ alert("邮箱不能为空!"); return false; } return true; } </script> <body> <div id="regTitle" class="png"></div> <div id="regForm" class="userForm png"> <form action="doreg.jsp" method="post"> <dl> <dt>用 户 名:</dt> <dd><input type="text" name="username" /></dd> <dt>密 码:</dt> <dd><input type="password" name="password" /></dd> <dt>确认密码:</dt> <dd><input type="password" name="affirm" /></dd> <dt>邮 箱:</dt> <dd><input type="text" name="email" /></dd> </dl> <div class="buttons"> <input type="submit" name="register" value="注册 " /><input type="reset" name="reset" value="重置 " /> </div> <div class="goback"><a href="index.jsp" class="png">返回登录页</a></div> </form> </div> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 注册成功,<a href="login.jsp">返回到登录页面</a> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="dowrite.jsp" method="post"> 收件人:<input type="text" name="sendto" value="<%=request.getParameter("reply") %>" ><br> 主题:<input type="text" name="title"><br> 内容:<textarea name="content"></textarea> <input type="submit" value="发送"> </form> </body> </html>