jsp第七次作业
package com.gd.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/sxz", "root", "sxz990715"); } 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.gd.dao; public class MsgDao { //发送,回复---insert操作 //邮件列表 --select * from msg where username=.... //根据标题查内容 select //阅读状态改变,,,未读 已读 update //删除邮件 delete }
package com.gd.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 { // 获取连接 Connection conn = getConnection(); // 编写sql语句 String sql = "select * from users where username=? and password=?"; // 执行sql语句 PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, uname); ps.setString(2, upwd); ResultSet rs = ps.executeQuery(); if (rs.next()) { closeAll(conn, ps, rs); return true; } else { closeAll(conn, ps, rs); return false; } } }
package com.gd.entity; public class Msg { int msgid; String usernname; String title; String msgcontent; int state; String sendto; String msg_create_date; public int getMsgid() { return msgid; } public void setMsgid(int msgid) { this.msgid = msgid; } public String getUsernname() { return usernname; } public void setUsernname(String usernname) { this.usernname = usernname; } 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 String getMsg_create_date() { return msg_create_date; } public void setMsg_create_date(String msg_create_date) { this.msg_create_date = msg_create_date; } }
package com.gd.entity; public class Users { String username; String password; String email; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
<%@page import="com.gd.dao.UsersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("uname"); String pwd = request.getParameter("upwd"); UsersDao ud = new UsersDao(); if (ud.login(name, pwd)) request.getRequestDispatcher("main.jsp").forward(request, response); else response.sendRedirect("index.jsp"); %>
<%@ 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> <form action="dologin.jsp" method="post"> 用户名:<input type="text" name="uname" /><Br> 密码 :<input type="password" name="upwd"/><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%>"> <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> 欢迎页面!!! </form> </body> </html> <body> This is my JSP page. <br> </body> </html>