jsp第十一周作业
1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)
2.建model层
entity,dao包
3.登陆,注册,登陆后显示全部邮件
package aa; 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", "123456", "cai123"); } 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 aa; 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; 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 aa; 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 aa.Msg; public class MsgDao extends BaseDao{ //插入邮件 //删除邮件 //修改邮件状态 //按照接收者查询全部邮件 public List<Msg> getMailByReceiver(String name){ List<Msg> list=new ArrayList<Msg>(); Connection conn=getConnection(); String sql="select * from msg where sendto=?"; PreparedStatement ps; ResultSet rs; try { ps = conn.prepareStatement(sql); ps.setString(1, name); rs=ps.executeQuery(); while(rs.next()){ 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); } closeAll(conn, ps, rs); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }
package aa;
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 aa;
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);
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 register(String uname,String upwd){
Connection conn=getConnection();
int i=0;
PreparedStatement ps=null;
try{
String sql="insert into users(uname,upwd) values(?,?)";
ps=conn.prepareStatement(sql);
ps.setString(1, uname);
ps.setString(2, upwd);
i=ps.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
closeAll(conn,ps,null);
}
return i;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> </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="zz.jsp" method="post"> 用户名:<input type="text" name="uname"><br> 密码: <input type="password" name="upwd"> <input type="button" value="登录" onClick="validate()"> <a href="MyJsp.jsp">注册</a> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@page import="aa.UsersDao"%> <%@page import="aa.MsgDao"%> <% 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> <title>My JSP 'z.jsp' starting page</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String uname=request.getParameter("uname"); String upwd=request.getParameter("upwd"); UsersDao ud=new UsersDao(); MsgDao md=new MsgDao(); if(ud.register(uname, upwd)>0){ session.setAttribute("uname", uname); request.getRequestDispatcher("dengli.jsp").forward(request,response); }else{ out.print("注册失败"); response.setHeader("refresh","5;url=MyJsp.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> <title>My JSP 'MyJsp.jsp' starting page</title> </head> <body> <script type="text/javascript"> function validate(){ if(registerForm.uname.value==""){ alert("账号不能为空!"); return; } if(registerForm.upwd.value==""){ alert("密码不能为空!"); return; } registerForm.submit(); } </script> <form name="registerForm" action="z.jsp" method="post"> 用户名:<input type="text" name="uname"><br> 密 码: <input type="password" name="upwd"> <br> <input type="submit" value="注册"> <a href="denglu.jsp">登录</a> </form> </body> </html>
<%@page import="aa.UsersDao"%> <%@page import="aa.MsgDao"%> <%@ 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(); MsgDao md=new MsgDao(); if(ud.login(uname, upwd)){ session.setAttribute("uname", uname); request.getRequestDispatcher("main.jsp").forward(request, response); }else{ out.print("登陆失败,即将跳回登陆页....."); response.setHeader("refresh", "5;url=dengli.jsp"); } %>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@page import="aa.MsgDao"%> <%@page import="aa.Msg"%> <% 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> <title>My JSP 'main.jsp' starting page</title> </head> <body> <% String uname = (String) session.getAttribute("uname"); %> 欢迎你<% out.print(uname); %> <table border="1px solid black"> <tr> <td>id</td> <td>发件人</td> <td>标题</td> <td>内容</td> <td>已读未读状态</td> <td>收件人</td> <td>发送时间</td> </tr> <% MsgDao md = new MsgDao(); List<Msg> list = md.getMailByReceiver(uname); for (Msg m : list) { %> <tr> <td><%=m.getMsgid()%></td> <td><%=m.getUsername()%></td> <td><%=m.getTitle()%></td> <td><%=m.getMsgcontent()%></td> <td> <% int state = m.getState(); if (state == 1) { %> <img src="images/yidu.png"> <% ; } else { %> <img src="images/weidu.png"> <% ; } %> </td> <td><%=m.getSendto() %></td> <td><%=m.getMsg_create_date()%></td> </tr> <% } %> </table> </body> </html>