jsp第十一周作业
1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)
2.建model层
entity,dao包
3.登陆,注册,登陆后显示全部邮件
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.List; import javax.annotation.PreDestroy; import com.jyp.entity.Msg; import com.sun.org.apache.regexp.internal.recompile; public class MsgDao extends BaseDao{ public List<Msg> getMsgByReceiver(String uname) { List<Msg> list=new ArrayList<Msg>(); Connection con=getConnection(); String sql="select * from email where receiver=?"; PreparedStatement ps=null; ResultSet rs=null; try{ ps=con.prepareStatement(sql); ps.setString(1,uname); rs=ps.executeQuery(); while(rs.next()){ Msg e=new Msg(); e.setId(rs.getInt(1)); e.setAddress(rs.getString(2)); e.setReceiver(rs.getString(3)); e.setTitle(rs.getString(4)); e.setContents(rs.getString(5)); e.setTime(rs.getDate(6)); e.setState(rs.getInt(7)); list.add(e); } }catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ closeAll(con, ps, rs); } return list; } }
package com.jyp.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 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 com.jyp.entity; import java.sql.Date; public class Msg { private int id; private String address; private String receiver; private String title; private String contents; private Date time; private int state; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getReceiver() { return receiver; } public void setReceiver(String receiver) { this.receiver = receiver; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContents() { return contents; } public void setContents(String contents) { this.contents = contents; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } public int getState() { return state; } public void setState(int state) { this.state = state; } }
package com.jyp.entity; public class Users { private int id; private String uname; private 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; } public Users() { super(); } }
<%@ 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 'index.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"><br> 密码: <input type="password" name="upwd"> <input type="button" value="登录" onClick="validate()"> <a href="register.jsp">注册</a> </form></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.dao.UsersDao"%> <%@page import="com.jyp.dao.MsgDao"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <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("main.jsp").forward(request, response); } else{ out.print("登录失败,5s后跳转登录页面"); response.setHeader("refresh", "5;url=index.jsp"); } %> </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 'register.jsp' starting page</title> </head> <body> <script type="text/javascript"> function zcButton() { if (loginForm.uname.value == "") { alert("用户名不能为空"); return; } if (loginForm.upwd.value == "") { alert("密码不能为空"); return; } if (loginForm.rupwd.value == "") { alert("未确认密码"); return; } loginForm.submit(); } </script> <form action="doregister.jsp" name="loginForm" method="post"> 请输入用户名:<input type="text" name="uname" /><br> 请输入密码:<input type="password" name="upwd" /><br> 确认密码:<input type="password" name="rupwd" /><br> <input type="button" value="注册" onclick="zcButton()" /><br> <a href="index.jsp">返回登录</a> </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+"/"; %> <%@page import="com.jyp.dao.UsersDao"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <body> <% 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=index.jsp"); } else { out.print("两次密码不一致,5秒后跳回注册页!!!"); response.setHeader("refresh", "5;url=register.jsp"); } %> </body> </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.dao.MsgDao"%> <%@page import="com.jyp.entity.Msg"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% request.setCharacterEncoding("utf-8"); String uname = (String)session.getAttribute("uname"); MsgDao ed=new MsgDao(); List<Msg> list=ed.getMsgByReceiver(uname); %> 欢迎你,<%=uname%><br> <table width="800px" style="border: 1px solid pink;"> <tr> <td>发件人</td> <td>主题</td> <td>状态</td> <td>时间</td> </tr> <% if (list.size() == 0) { out.print("还没有人给您写文件哦!!!"); } else { for(int i=0;i<list.size();i++){ %> <tr> <td> <%
out.print(list.get(i).getAddress()); %> </td> <td> <% out.print(list.get(i).getTitle()); %> </td> <td> <% if (list.get(i).getState() ==0){ out.print("<img src='unread.png'></img>"); } else { out.print("<img src='read.png'/>"); } %> </td> <td><%out.print(list.get(i).getTime()); %></td> </tr> <%}}%> </table> </body> </html>