jsp第十三周作业
1.第十二周上机作业(邮件功能)的控制层代码改用为servlet实现。
package com.jyp.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", "123456"); } 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.jyp.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.jyp.entity.Msg; public class MsgDao extends BaseDao { // 1,插入邮件 public void addMsg(Msg m) { Connection con = getConnection(); String sql = "insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,?,?,?)"; PreparedStatement ps = null; try { ps = con.prepareStatement(sql); ps.setString(1, m.getUsername()); ps.setString(2, m.getTitle()); ps.setString(3, m.getMsgcontent()); ps.setInt(4, 1); ps.setString(5, m.getSendto()); ps.setDate(6, new java.sql.Date(new Date().getTime()));// 系统当前时间 ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(con, ps, null); } } // 2.删除邮件 public void delMail(int id) { Connection conn = getConnection(); String sql = "delete from msg where msgid=?"; PreparedStatement ps = null; try { ps = conn.prepareStatement(sql); ps.setInt(1, id); 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.delMail(3); //} // 3.修改邮件状态 public void update(int id) { Connection con = getConnection(); String sql = "update msg set state='1' where msgid=?"; PreparedStatement ps = null; try { ps = con.prepareStatement(sql); ps.setInt(1, id); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(con, ps, null); } } // 4.按照接收者查询全部邮件 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 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")); list.add(m); } } catch (SQLException e) { e.printStackTrace(); }finally{ closeAll(con, ps, rs); } return list; } //5.实现阅读邮件功能 public Msg read(int id) { Connection con = getConnection(); String sql = "select msgid,username,sendto,title,msgcontent,msg_create_date from msg where msgid=?"; PreparedStatement ps = null; ResultSet rs = null; try { ps = con.prepareStatement(sql); ps.setInt(1, id); rs = ps.executeQuery(); while (rs.next()) { Msg m = new Msg(); m.setMsgid(rs.getInt("msgid")); m.setUsername(rs.getString("username")); m.setTitle(rs.getString("title")); m.setMsgcontent(rs.getString("msgcontent")); m.setSendto(rs.getString("sendto")); m.setMsg_create_date(rs.getDate("msg_create_date")); return m; } } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(con, ps, rs); } return null; } }
package com.jyp.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.jyp.entity.Users; public class UsersDao extends BaseDao { // 关于用户的增删改查 // 1.登录 public boolean login(String uname, String upwd) { boolean f = false; Connection con = getConnection(); String sql = "select * from users where uname=? and upwd=?"; PreparedStatement ps = null; ResultSet rs = null; try { ps = con.prepareStatement(sql); ps.setString(1, uname); ps.setString(2, upwd); rs = ps.executeQuery(); if (rs.next()) f = true; } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(con, ps, rs); } return f; } // 2.注册 public int register(String uname, String upwd) { Connection con = getConnection(); PreparedStatement ps = null; int x = 0; try { String sql = "insert into users(uname,upwd) values(?,?)"; ps = con.prepareStatement(sql); ps.setString(1, uname); ps.setString(2, upwd); x = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(con, ps, null); } return x; } }
package com.jyp.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.jyp.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; } }
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.jms.Session; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.jyp.dao.UsersDao; import com.jyp.entity.Users; @WebServlet("/dologin.do") public class DoLogin extends HttpServlet { /** * Constructor of the object. */ public DoLogin() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html,charset=utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String uname = request.getParameter("uname"); String password = request.getParameter("password"); Users ud = new Users(); HttpSession session = request.getSession(); PrintWriter out = response.getWriter(); if (ud.Login(uname, password)) { session.setAttribute("uname", uname); request.getRequestDispatcher("index.jsp").forward(request, response); } else { out.print("登录失败,即将调回登录页....."); response.setHeader("refresh", "2;url=login.jsp"); } } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here } }
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jyp.dao.UsersDao; public class DoRegister extends HttpServlet { /** * Constructor of the object. */ public DoRegister() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html,charset=utf-8"); PrintWriter out = response.getWriter(); request.setCharacterEncoding("utf-8"); String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); String rupwd = request.getParameter("rupwd"); if (upwd.equals(rupwd)) { UsersDao ud = new UsersDao(); ud.register(uname, upwd); out.print("注册成功5秒返回登录页!!!"); response.setHeader("refresh", "5;url=login.jsp"); } else { out.print("两次密码不一致,5秒后跳回注册页!!!"); response.setHeader("refresh", "5;url=zc.jsp"); } } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.jyp.entity.email; import com.jyp.dao.emailDao; @WebServlet("/dolook.do") public class DoLook extends HttpServlet { /** * Constructor of the object. */ public DoLook() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html,charset=utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String id = request.getParameter("id"); int idd = Integer.parseInt(id); emailDao e = new emailDao(); e.update(idd); email email = e.look(idd); HttpSession session=request.getSession(); session.setAttribute("email", email); request.getRequestDispatcher("look.jsp").forward(request, response); } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here } }
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/dodel.do") public class DoDel extends HttpServlet { /** * Constructor of the object. */ public DoDel() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html,charset=utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); com.jyp.dao.emailDao e=new com.jyp.dao.emailDao(); String id=request.getParameter("id"); int idd=Integer.parseInt(id); e.del(idd); request.getRequestDispatcher("main.jsp").forward(request, response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.jyp.dao.emailDao; import com.jyp.entity.email; public class DoWrite extends HttpServlet { /** * Constructor of the object. */ public DoWrite() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html,charset=utf-8"); PrintWriter out = response.getWriter(); HttpSession session=request .getSession(); request.setCharacterEncoding("utf-8"); String address=(String)session.getAttribute("uname"); String receiver=request.getParameter("receiver"); String title=request.getParameter("title"); String contents=request.getParameter("contents"); email e=new email(); e.setAddress(address); e.setReceiver(receiver); e.setTitle(title); e.setContents(contents); emailDao ed=new emailDao(); ed.addEmail(e); out.print("发送成功,3秒后跳回首页!!!"); response.setHeader("refresh", "3;url=main.jsp"); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jyp.dao.emailDao; import com.jyp.entity.email; public class Detail extends HttpServlet { /** * Constructor of the object. */ public Detail() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); String id=request.getParameter("id"); int uid=Integer.parseInt(id); emailDao ed=new emailDao(); ed.update(uid); email e=ed.lookEmail(uid); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
<%@ 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 'denglu.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> <script type="text/javascript"> function validate(){ if(loginForm.uname.value==""){ alert("账号不能为空!"); return; } if(loginForm.upwd.value==""){ alert("密码不能为空!"); return; } loginForm.submit(); } </script> <form name="loginForm" action="dologin.jsp" method="post"> 用户名:<input type="text" name="uname" value="zs"><br> 密码: <input type="password" name="upwd" value="zs"> <input type="button" value="登录" onClick="validate()"> </form> <a href="reg.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+"/"; %> <%@page import="com.jyp.entity.Msg"%> <%@page import="com.jyp.dao.MsgDao"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'main.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> <% MsgDao md = new MsgDao(); String uname = (String) session.getAttribute("uname"); List<Msg> list = md.getMailByReceiver(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><%=list.get(i).getUsername()%></td> <td><%=list.get(i).getTitle()%></td> <td> <% if (list.get(i).getState() == 1) { %> <img src="images/sms_unReaded.png" /> <% } else { %> <img src="images/sms_readed.png" /> <% } %> </td> <td><%=list.get(i).getMsg_create_date()%></td> <td><a href="write.jsp?reply=<%=list.get(i).getUsername()%>">回复</a> </td> <td><a href="del.jsp?id=<%=list.get(i).getMsgid()%>">删除</a> </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 'write.jsp' starting page</title> </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 rows="6" cols="20" name="content"></textarea> <br> <input type="submit" value="发送"> </form> </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%>"> </head> <body> <script type="text/javascript"> function dozhuce(){ if(loginform2.id.value==""){ alert("没有输入id"); return; } if(loginform2.uname.value==""){ alert("没有输入用户名"); return; } if(loginform2.upwd.value==""){ alert("没有输入密码"); return; } if(loginform2.upwd2.value==""){ alert("没有确认密码"); return; } loginform2.submit(); } </script> <form action="doregister.jsp" name="loginform2" method="post"> id: <input type="number" name="id"/> 用户名:<input type="text" value="zs" name="uname"/> 密码:<input type="password" value="11" name="upwd"/> 确认密码:<input type="password" value="11" name="upwd2"/> <input type="button" value="注册" onclick="dozhuce()"/> </form> </body> </html>
<%@page import="com.jyp.entity.Msg"%> <%@page import="com.jyp.dao.MsgDao"%> <%@page import="com.jyp.dao.UsersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% request.setCharacterEncoding("utf-8"); String uname=(String)session.getAttribute("uname");// 发件人在session中获取 String sendto=request.getParameter("sendto"); String title=request.getParameter("title"); String content=request.getParameter("content"); Msg m=new Msg(); m.setTitle(title); m.setMsgcontent(content); m.setUsername(uname); m.setSendto(sendto); MsgDao md=new MsgDao(); md.addMsg(m); out.print("发送成功,即将跳回首页....."); response.setHeader("refresh", "3;url=main.jsp"); %>