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" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@page import="java.sql.SQLException"%> <%@page import="com.gd.dao.BaseDao"%> <%@page import="com.gd.entity.Msg"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.Connection"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>doinsert</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String username=(String)session.getAttribute("userName"); String title = request.getParameter("title"); String msgcontent = request.getParameter("msgcontent"); int state=0; String sendto = request.getParameter("sendto"); String msg_create_date="2020-05-14"; try { // 1.加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/sxz", "root", "sxz990715"); // 3.编写SQL语句 String sql = "insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,?,?,?)"; PreparedStatement ps = con.prepareStatement(sql);// PS是用来执行SQL语句和给问号赋值的 ps.setString(1, username); ps.setString(2, title); ps.setString(3, msgcontent); ps.setInt(4, state); ps.setString(5, sendto); ps.setString(6, msg_create_date); // 4.执行SQL语句 ps.executeUpdate(); // 执行查询 // 5.处理查询结果(查询的时候需要) // 6.关闭连接 ps.close(); con.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } %> 发送成功 </br> <a href="main.jsp">【返回】 </body> </html>
<%@page import="com.gd.dao.UsersDao"%> <%@ 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(); if (ud.login(uname, upwd)){ session.setAttribute("userName", uname); 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 'insert.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="doinsert.jsp" method="post"> 收件人:<input name="sendto" type="text" size="150" /><br /> <br />      主题:<input name="title" type="text" size="150" /><br /> <br />      正文:<input name="msgcontent" type="text" size="150" /><br /> <input type="submit" value="发布"> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@page import="java.sql.SQLException"%> <%@page import="com.gd.dao.BaseDao"%> <%@page import="com.gd.entity.Msg"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.Connection"%> <% 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> <% String name=(String)session.getAttribute("userName"); session.setAttribute("userName", name); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); //加载驱动 String url = "jdbc:mysql://localhost:3306/sxz"; String user = "root"; String password = "sxz990715"; conn = DriverManager.getConnection(url, user, password); ps = conn.prepareStatement("select * from msg where sendto=?"); ps.setString(1, name); rs = ps.executeQuery(); %> 您好<%=name %>,欢迎您访问邮箱<br> <a href=""><form action="insert.jsp" method="post"> <input type="submit" value="写邮件"> </form></a> <table border="1"> <tr> <td>编辑</td> <td>发件人</td> <td>标题</td> <td>正文</td> <td>发送时间</td> </tr> <% while (rs.next()) { %> <tr> <td> <% if (rs.getString("state").equals("1")) { String msgid=rs.getString("msgid"); session.setAttribute("msgid", msgid); %> <input type="button" value="点击查看" onclick="window.location.href='show.jsp';" /> <% } else { %> <div align="center"> <% out.print("已查看"); %> </div> <% } %> </td> <td><%=rs.getString("username")%></td> <td><%=rs.getString("title")%></td> <td><%=rs.getString("msgcontent")%></td> <td><%=rs.getString("msg_create_date")%></td> </tr> <% } %> </table> <br> <% } catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { // ignore } rs = null; } } if (ps != null) { try { ps.close(); } catch (SQLException e) { // ignore } ps = null; } } if (conn != null) { try { conn.close(); } catch (SQLException e) { // ignore } rs = null; } } } %> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@page import="java.sql.SQLException"%> <%@page import="com.gd.dao.BaseDao"%> <%@page import="com.gd.entity.Msg"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.Connection"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>show</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String msgid = (String) session.getAttribute("msgid"); int id = Integer.valueOf(msgid); try { // 1.加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/sxz", "root", "sxz990715"); // 3.编写SQL语句 String sql1 = "select * from msg where msgid=?"; String sql2 = "update msg set state=0 where msgid=?"; PreparedStatement ps1 = con.prepareStatement(sql1);// PS是用来执行SQL语句和给问号赋值的 PreparedStatement ps2 = con.prepareStatement(sql2); ps1.setInt(1, id); ps2.setInt(1, id); // 4.执行SQL语句 ResultSet rs = ps1.executeQuery(); ps2.executeUpdate(); while (rs.next()) {// 读取下一行。第一次执行读取第一行 String username = rs.getString("username"); String title = rs.getString("title"); String msgcontent = rs.getString("msgcontent"); %> 发件人:<%=username%> </br>标题:<%=title%> </br>正文:<%=msgcontent%> <% } %> <% ps1.close(); ps2.close(); con.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } %> </br> <a href="select.jsp">【返回】 </body> </html>