第12周作业
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'student.jsp' starting page</title> </head> <% %> <body> <form action="login_link.jsp"> 姓名:<input type="text" name="name" /> <br>密码:<input type="password" name="pwd" /> <br><input type="submit" value="登录" /> </form> <form action="logon.jsp"> <input type="submit" value="注册" /> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title></title> </head> <%@ page import="dao.UsersDao"%> <% String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); String uname = new String(name.getBytes("ISO-8859-1"), "utf-8"); try { UsersDao usersdao = new UsersDao(); if (usersdao.login(uname, pwd)) { String n = (String) session.getAttribute("uname"); if (n == null) { session.setAttribute("uname", name); } request.getRequestDispatcher("mail.jsp").forward(request, response); } else { out.println("登陆失败!"); out.println("三秒后跳转到登录页面..."); response.setHeader("refresh", "3;url=login.jsp"); } } catch (Exception e) { out.println("异常!!"); out.println("三秒后跳转到登录页面..."); response.setHeader("refresh", "3;url=login.jsp"); } %> <body> <a href="login.jsp">返回</a> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'student.jsp' starting page</title> </head> <% %> <body> <form action="login_link.jsp"> 姓名:<input type="text" name="name" /> <br>密码:<input type="password" name="pwd" /> <br><input type="submit" value="登录" /> </form> <form action="logon.jsp"> <input type="submit" value="注册" /> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'slogon_link.jsp' starting page</title> </head> <%@ page import="dao.UsersDao"%> <% String name = request.getParameter("name"); String upwd = request.getParameter("pwd"); String uname=new String(name.getBytes("ISO-8859-1"),"utf-8"); int pwd = Integer.parseInt(upwd); UsersDao usersdao=new UsersDao(); if (usersdao.logon(uname, pwd) != -1) { out.println("注册成功!"); out.println("三秒后跳转到登录页..."); response.setHeader("refresh", "3;url=login.jsp"); } else { out.println("注册失败!"); out.println("三秒后跳转到登录页..."); response.setHeader("refresh", "3;url=login.jsp"); } %> <body> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title></title> <%@ page import="dao.MsgDao"%> <%@ page import="entity.Msg"%> <style type="text/css"> table { border: 1px black solid; width: 500px; } </style> </head> <body> <% request.setCharacterEncoding("utf-8"); String name = (String) session.getAttribute("uname"); String uname = new String(name.getBytes("ISO-8859-1"), "utf-8"); %> <% out.println(uname); %>的邮箱:<a href="write.jsp">写邮箱</a> <br> <% MsgDao m = new MsgDao(); List<Msg> list = m.getMailByReceiver(uname); %> <table> <tr> <td>发件人</td> <td>主题</td> <td>状态</td> <td>时间</td> </tr> <% for (int i = 0; i < list.size(); i++) { %> <tr> <td><%=list.get(i).getUsername()%></td> <td><a href="chakan.jsp?id=<%=list.get(i).getMsgid() %>"><%=list.get(i).getTitle()%></a></td> <td> <% if (list.get(i).getState() == 0) { %><img src="image/weidu.png" /> <% } else { %> <img src="image/yidu.png" /> <% } %> </td> <td><%=list.get(i).getMsg_create_date()%></td> <td> <form action="delete.jsp"> <input type="hidden" value="<%=list.get(i).getMsgid() %>" name="Msgid"> <input type="submit" value="删除" > </form> </td> <td><a href="write.jsp?reply=<%=list.get(i).getUsername() %>">回复</a></td> </tr> <% } %> </table> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title></title> </head> <body> <form action="write_link.jsp"> 收件人:<input type="text" name="sendto" value="<%=request.getParameter("reply")%>"><br> 主题:<input type="text" name="title"><br> 内容: <textarea rows="6" cols="20" name="content"></textarea> <br> <input type="submit" value="发送"> <a href="mail.jsp">返回</a> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title></title> </head> <%@page import="dao.MsgDao"%> <%@page import="entity.Msg"%> <body> <% request.setCharacterEncoding("utf-8"); Msg m = new Msg(); String Uname = (String) session.getAttribute("uname"); String Sendto = request.getParameter("sendto"); String Title = request.getParameter("title"); String Content = request.getParameter("content"); String uname = new String(Uname.getBytes("ISO-8859-1"), "utf-8"); String sendto = new String(Sendto.getBytes("ISO-8859-1"), "utf-8"); String title = new String(Title.getBytes("ISO-8859-1"), "utf-8"); String content = new String(Content.getBytes("ISO-8859-1"), "utf-8"); m.setUsername(uname); m.setSendto(sendto); m.setTitle(title); m.setMsgcontent(content); MsgDao md = new MsgDao(); if (md.addMsg(m) != -1) { out.println("添加成功!"); out.println("三秒后跳转到mail页面..."); response.setHeader("refresh", "3;url=mail.jsp"); } else { out.println("添加失败!"); out.println("三秒后跳转到mail页面..."); response.setHeader("refresh", "3;url=mail.jsp"); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'delete.jsp' starting page</title> </head> <%@page import="dao.MsgDao"%> <body> <% try{ String msgid = request.getParameter("Msgid"); int msgId = Integer.parseInt(msgid); MsgDao md = new MsgDao(); if (md.deleteMsg(msgId) != -1) { out.println("删除成功!"); out.println("1秒后跳转到mail页面..."); response.setHeader("refresh", "1;url=mail.jsp"); }else{ out.println("删除失败!"); out.println("三秒后跳转到mail页面..."); response.setHeader("refresh", "3;url=mail.jsp"); } }catch(Exception e){ e.printStackTrace(); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title></title> </head> <%@ page import="dao.MsgDao"%> <body> <% String Id = request.getParameter("id"); int msgid = Integer.parseInt(Id); MsgDao md = new MsgDao(); md.xiugai(msgid); out.println(md.chakan(msgid)); %> <br> <a href="mail.jsp">返回</a> </body> </html>
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class BaseDao { //获取连接 protected Connection getConnection(){ Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test", "root", "root"); } 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 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 entity.Msg; public class MsgDao extends BaseDao{ //关于邮件的增删改查 //添加邮件(写邮件,回复邮件都调用,邮件状态为0,时间为系统当前时间) public int addMsg(Msg m){ int t=-1; PreparedStatement ps=null; Connection con=getConnection(); String sql="insert into msg (username,title,msgcontent,state,sendto,msg_create_date) value(?,?,?,0,?,?)"; try { ps=con.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())); t=ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(con, ps, null); } return t; } //根据id删除邮件 public int deleteMsg(int msgId){ int t=-1; PreparedStatement ps=null; Connection con=getConnection(); String sql="delete from msg where msgid=?"; try { ps=con.prepareStatement(sql); ps.setInt(1, msgId); t=ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(con, ps, null); } return t; } //根据id修改邮件状态(统一改成1) public Msg chakan(int msgid){ Msg m=null; Connection con=getConnection(); ResultSet rs=null; PreparedStatement ps=null; String sql="select * from msg where msgid=?"; try { ps=con.prepareStatement(sql); ps.setInt(1, msgid); rs=ps.executeQuery(); while(rs.next()){ m=new Msg(); m.setMsgid(rs.getInt("msgid")); m.setUsername(rs.getString("username")); m.setTitle(rs.getString("title")); m.setMsgcontent(rs.getString("msgcontent")); m.setState(rs.getInt("state")); m.setSendto(rs.getString("sendto")); m.setMsg_create_date(rs.getDate("msg_create_date")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(con, ps, rs); } return m; } public int xiugai(int msgid){ int t=-1; Connection con=getConnection(); PreparedStatement ps=null; String sql="update msg set state=1 where msgid=?"; try { ps=con.prepareStatement(sql); ps.setInt(1, msgid); t=ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(con, ps, null); } return t; } //根据收件人查看全部邮件 public List<Msg> getMailByReceiver(String name){ List<Msg> list=new ArrayList<Msg>(); Connection con=getConnection(); String sql="select * from msg where username=?"; 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; } public static void main(String[] args) { MsgDao m=new MsgDao(); Msg mm=new Msg(); System.out.println(m.chakan(19)); } }
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class UsersDao extends BaseDao{ //关于用户的增删改查,用到什么功能鞋什么 //登陆 public boolean login(String name,String pwd){ boolean f=false; Connection conn=getConnection(); String sql="select * from users where uname=? and upwd=?"; PreparedStatement ps; try { ps = conn.prepareStatement(sql); ps.setString(1, name);//第一个?赋值为name ps.setString(2, pwd); ResultSet rs=ps.executeQuery(); if(rs.next())//查到结果了 f=true; closeAll(conn, ps, rs); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return f; } //注册 public int logon(String uname, int upwd) { Connection conn = getConnection(); PreparedStatement ps = null; int r = -1; try { String sql = "insert into users(uname,upwd) values(?,?)"; // 2个占位符 // 4.执行SQL语句 ps = conn.prepareStatement(sql); ps.setString(1, uname); ps.setInt(2, upwd); r = ps.executeUpdate();// 增删改都用这个 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(conn, ps, null); } return r; } }
package 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; @Override public String toString() { return "Msg [msgid=" + msgid + ", username=" + username + ", title=" + title + ", msgcontent=" + msgcontent + ", state=" + state + ", sendto=" + sendto + ", msg_create_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 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; } }