JSP第十一周作业
1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)
2.建model层
entity,dao包
3.登陆,注册,登陆后显示全部邮件
package psw; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Base { //获取连接 protected Connection getConnection(){ Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test", "root", "501002"); } 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 psw; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.annotation.PreDestroy; import com.nn.entity.msg; import com.sun.org.apache.regexp.internal.recompile; public class msgDao extends BaseDao{ public List<msg> listAll(String username) throws SQLException{ List<msg> list =new ArrayList<msg>(); Connection conn =getConnection(); String sql="select * from msg where username=?"; PreparedStatement pred =null; ResultSet rest=null; try { pred=conn.prepareStatement(sql); pred.setString(1, username); rest=pred.executeQuery(); while (rest.next()) { msg msg=new msg(); msg.setMsgid(rest.getInt("msgid")); msg.setUsername(rest.getString("username")); msg.setTitle(rest.getString("title")); msg.setMsgcontent(rest.getString("msgcontent")); msg.setState(rest.getInt("state")); msg.setSendto(rest.getString("sendto")); msg.setMsg_create_date(rest.getDate("msg_create_date")); list.add(msg); } } catch (Exception e) { // TODO: handle exception } finally{ closeAll(conn, pred, rest); } return list; } } package psw; 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 uname,String upwd) throws SQLException{ boolean f= false; Connection conn =getConnection(); PreparedStatement pred=null; ResultSet rest=null; String sql="select * from users where uname=? and upwd=?"; try { pred =conn.prepareStatement(sql); pred.setString(1, uname); pred.setString(2, upwd); rest =pred.executeQuery(); while(rest.next()){ f=true; } } catch (Exception e) { // TODO: handle exception }finally{ closeAll(conn, pred, rest); } return f; } public boolean id(Integer id) throws SQLException{ boolean f= false; Connection conn =getConnection(); PreparedStatement pred=null; ResultSet rest=null; String sql="select * from users where id=?"; try { pred =conn.prepareStatement(sql); pred.setInt(1, id); rest =pred.executeQuery(); while(rest.next()){ f=true; } } catch (Exception e) { // TODO: handle exception }finally{ closeAll(conn, pred, rest); } return f; } public void zhuce(Integer id,String uname,String upwd){ Connection conn =getConnection(); PreparedStatement pred=null; try { String sql="insert into users(id,uname,upwd) values(?,?,?)"; pred =conn.prepareStatement(sql); pred.setInt(1, id); pred.setString(2, uname); pred.setString(3, upwd); pred.executeUpdate(); } catch (Exception e) { // TODO: handle exception } finally{ closeAll(conn, pred, null); } } } package psw; import java.sql.Date; public class msg { private Integer msgid; private String username; private String title; private String msgcontent; private Integer state; private String sendto; private Date msg_create_date; public Integer getMsgid() { return msgid; } public void setMsgid(Integer 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 Integer getState() { return state; } public void setState(Integer 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 psw; public class users { private Integer id; private String uname; private String upwd; public Integer getId() { return id; } public void setId(Integer 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 psw; public class users { private Integer id; private String uname; private String upwd; public Integer getId() { return id; } public void setId(Integer 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 language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <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="dozhuce.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.nn.Dao.usersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <% String uid=request.getParameter("id"); Integer id=Integer.parseInt(uid); String uname=request.getParameter("uname"); String upwd =request.getParameter("upwd"); String upwd2 =request.getParameter("upwd2"); usersDao usersDao=new usersDao(); if(upwd.equals(upwd2)){ if(usersDao.id(id)){ out.print("已经有这个id了,即将跳回注册页....."); response.setHeader("refresh", "5;url=zhuce.jsp"); } else{ usersDao.zhuce(id, uname, upwd); out.print("注册成功,即将跳回登录页....."); response.setHeader("refresh", "5;url=login.jsp"); } } else{ out.print("两次密码不一致,即将跳回注册页....."); response.setHeader("refresh", "5;url=zhuce.jsp"); } %> </body> </html> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <script type="text/javascript"> function dologin(){ if(loginform.uname.value==""){ alert("没有输入用户名"); return; } if(loginform.upwd.value==""){ alert("没有输入密码"); return; } loginform.submit(); } </script> <form action="dologin.jsp" name="loginform" method="post"> 用户名:<input type="text" value="zs" name="uname"/> 密码:<input type="password" value="11" name="upwd"/> <input type="button" value="登录" onclick="dologin()"/> <a href="zhuce.jsp">没有账号?立即注册</a> </form> </body> </html> <%@page import="com.lrp.entity.Email"%> <%@page import="com.lrp.dao.EmailDao"%> <%@page import="com.lrp.dao.UserDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML> <html> <head> <base href="<%=basePath%>"> <title></title> </head> <body> <% String uname = (String) session.getAttribute("uname"); EmailDao dao = new EmailDao(); List<Email> list = dao.getAll(uname); %> 欢迎<%=uname%> <br> <br> <table border="1px" cellspacing="0" cellpadding="30px"> <thead></thead> <tr> <th>发件人</th> <th>标题</th> <th>时间</th> <th>状态</th> </tr> <tbody></tbody> <% for (int i = 0; i < list.size(); i++) { %> <tr> <td><%out.print(list.get(i).getFjr().toString()); %></td> <td><%out.print(list.get(i).getTitle().toString()); %></td> <td><%out.print(list.get(i).getTime().toString()); %></td> <td><%if(list.get(i).getZt()==0){ out.print("<img src='img/w.png'></img>"); }else{ out.print("<img src='img/y.png'></img>"); }%></td> </tr> <% } %> </table> </body> </html> <%@page import="sun.security.jgss.LoginConfigImpl"%> <%@page import="com.nn.Dao.usersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <% String uname = request.getParameter("uname"); String upwd =request.getParameter("upwd"); usersDao usersDao=new usersDao(); if(usersDao.login(uname, upwd)){ session.setAttribute("uname", uname); request.getRequestDispatcher("zhuye.jsp").forward(request, response); } else{ out.print("登录失败,5s后跳转登录页面"); response.setHeader("refresh", "5;url=login.jsp"); } %> </body> </html>