JSP11
1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)
2.建model层
entity,dao包
3.登陆,注册,登陆后显示全部邮件
5个页面 login,dologin,reg,doreg,main
1 package com.gxl.email.dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 8 import com.gxl.email.entity.User; 9 import com.gxl.email.util.DbUtil; 10 11 public class UserDao { 12 public void userReg(User user){ 13 String sql = "insert into user(uname,password) value ('"+ user.getUname() +"','"+user.getPassword()+"')"; 14 Connection con = DbUtil.getCon(); 15 try { 16 PreparedStatement pred = con.prepareStatement(sql); 17 // pred.setString(1, user.getUname()); 18 // pred.setString(2, user.getPassword()); 19 // pred.setInt(3, user.getAge()); 20 pred.executeUpdate(); 21 pred.close(); 22 } catch (SQLException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } 26 try { 27 con.close(); 28 } catch (SQLException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } 32 } 33 34 public User getUserdentByName(String uname){ 35 String sql = "select * from user where uname='"+uname+"'"; 36 Connection con = null; 37 PreparedStatement pred = null; 38 ResultSet res = null; 39 con = DbUtil.getCon(); 40 try { 41 pred = con.prepareStatement(sql); 42 // pred.setString(1, uname); 43 res = pred.executeQuery(); 44 User user = null; 45 if (res.next()) { 46 user = new User(res.getString("uname"), res.getString("password")); 47 } 48 return user; 49 } catch (SQLException e) { 50 // TODO Auto-generated catch block 51 e.printStackTrace(); 52 }finally{ 53 DbUtil.closeAll(con, pred, res); 54 } 55 return null; 56 } 57 }
package com.gxl.email.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 com.gxl.email.entity.Msg; import com.gxl.email.util.DbUtil; public class MsgDao { public List<Msg> getMailByReceiver(String name) { List<Msg> list = new ArrayList<Msg>(); Connection con = DbUtil.getCon(); 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("eid")); m.setUsername(rs.getString("uname")); 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{ } return list; } }
package com.gxl.email.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DbUtil { static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getCon() { Connection con = null; try { con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/email", "root", "123456"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } public static 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) { // TODO Auto-generated catch block e.printStackTrace(); } } }
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 3 <!DOCTYPE HTML> 4 <html> 5 <head> 6 <title>登录</title> 7 </head> 8 <body> 9 <form name="formName" action="dologin.jsp" method="post"> 10 用户名:<input type="text" name="uname"><br> 密码:<input 11 type="password" name="password"><br> <input 12 type="button" value="登录" onclick="toDoLogin()"> <input 13 type="button" value="注册" onclick="toReg()"> 14 </form> 15 <script> 16 function toReg() { 17 window.location.href = "reg.jsp"; 18 } 19 function toDoLogin() { 20 if (formName.uname.value == "" || formName.password.value == "") { 21 alert("用户名或密码不能为空"); 22 }else{ 23 formName.submit(); 24 } 25 } 26 </script> 27 </body> 28 </html>
<%@page import="com.gxl.email.entity.User"%> <%@page import="com.gxl.email.dao.UserDao"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <title></title> </head> <body> <% request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); String uname = request.getParameter("uname"); String password = request.getParameter("password"); UserDao userdentDao = new UserDao(); User user = userdentDao.getUserdentByName(uname); if (password.equals(user.getPassword())) { session.setAttribute("uname", uname); response.sendRedirect("index.jsp"); } else { response.sendRedirect("login.jsp"); } %> </body> </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 3 <!DOCTYPE HTML> 4 <html> 5 <head> 6 <title>注册</title> 7 </head> 8 <body> 9 <form action="doreg.jsp" method="post"> 10 用户名:<input type="text" name="uname"><br> 11 密码:<input type="password" name="password"><br> 12 <input type="submit" value="提交"> 13 </form> 14 </body> 15 </html>
1 <%@page import="com.wck.email.dao.UserDao"%> 2 <%@page import="com.wck.email.entity.User"%> 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 4 5 <!DOCTYPE HTML> 6 <html> 7 <head> 8 <title></title> 9 </head> 10 <body> 11 <% 12 request.setCharacterEncoding("UTF-8"); 13 response.setCharacterEncoding("UTF-8"); 14 //接收表单传值 15 String uname = request.getParameter("uname"); 16 String password = request.getParameter("password"); 17 User user = new User(uname,password); 18 UserDao userDao = new UserDao(); 19 userDao.userReg(user); 20 response.sendRedirect("login.jsp"); 21 %> 22 </body> 23 </html>
1 <%@page import="com.gxl.email.entity.Msg"%> 2 <%@page import="com.gxl.email.dao.MsgDao"%> 3 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 4 <!DOCTYPE HTML> 5 <html> 6 <head> 7 <title>My JSP 'index.jsp' starting page</title> 8 <style> 9 table { 10 border: 2px solid black; 11 border-collapse: collapse; 12 } 13 table tr td{ 14 border: 2px solid black 15 } 16 </style> 17 </head> 18 19 <body> 20 <% 21 String uname = (String) session.getAttribute("uname"); 22 %> 23 欢迎您<% 24 out.print(uname); 25 %>使用邮箱 26 <hr> 27 <table> 28 <tr> 29 <td>id</td> 30 <td>发件人</td> 31 <td>标题</td> 32 <td>内容</td> 33 <td>已读未读状态</td> 34 <td>收件人</td> 35 <td>发送时间</td> 36 </tr> 37 <% 38 MsgDao md = new MsgDao(); 39 List<Msg> list = md.getMailByReceiver(uname); 40 for (Msg m : list) { 41 %> 42 <tr> 43 <td><%=m.getMsgid()%></td> 44 <td><%=m.getUsername()%></td> 45 <td><%=m.getTitle()%></td> 46 <td><%=m.getMsgcontent()%></td> 47 <td> 48 <% 49 int state = m.getState(); 50 if (state == 1) { 51 %> <img src="image/yidu.png"> <% 52 ; 53 } else { 54 %> <img src="image/weidu.png"> <% 55 ; 56 } 57 %> 58 </td> 59 <td><%=m.getSendto()%></td> 60 <td><%=m.getMsg_create_date()%></td> 61 </tr> 62 <% 63 } 64 %> 65 </table> 66 </body> 67 </html>