• 第七个JSP作业-邮件


    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@page import="java.sql.SQLException"%>
    <%@page import="com.test.dao.BaseDao"%>
    <%@page import="com.test.entity.Msg"%>
    <%@page import="java.sql.DriverManager"%>
    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="java.sql.ResultSet"%>
    <%@page import="java.sql.Connection"%>
    <body>
        <%
            String uname = request.getParameter("uname");
            session.setAttribute("uname", uname);
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                String url = "jdbc:mysql://localhost:3306/users";
                String user = "root";
                String password = "admin";
                conn = DriverManager.getConnection(url, user, password);
                ps = conn.prepareStatement("select * from msg where username=?");
                ps.setString(1, uname);
                rs = ps.executeQuery();
        %>
        <table>
            <tr>
            <td>msgid</td>      
            <td>username</td>
                <td>title</td>
                <td>msgcontent</td>
                <td>state</td>
                <td>sendto</td>
                <td>msg_create_date</td>
            </tr>
            <%
                while (rs.next()) {
            %>
            <tr>
                <td><%=rs.getString("username")%></td>
                <td><%=rs.getString("title")%></td>
                <td><%=rs.getString("msgcontent")%></td>
    
                <td>
                    <%
                        if (rs.getString("state").equals("1")) {
                    %> <input type="button" value="点击查看"
                    onclick="window.location.href='show.jsp';" /> <%
         } else {
     %>
                    <div align="center">
                        <%
                            out.print("已查看");
                        %>
                    </div> <%
         }
     %> <%
    
         String title=rs.getString("title");
         session.setAttribute("title", title);
         
     %>
                </td>
                <td><%=rs.getString("sendto")%></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" 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" value="kitty" /><Br>
    密码 :<input type="password" name="upwd" value="123456"/><br>
    
    <input type="submit" value="登录">
    
    </form>
      </body>
    </html>
    

      

    <%@page import="com.test.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))
            request.getRequestDispatcher("main.jsp").forward(request, response);
        else
            response.sendRedirect("index.jsp");
    %>
    

      

    package com.test.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;
        }
    
    }
    

      

    package com.test.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.test.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();
            String sql = "select * from users where username=? and password=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                realse(rs, ps, conn);
                return true;
            } else {
                realse(rs, ps, conn);
                return false;
    
            }
        }
    }
    

      

    package com.test.dao;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class BaseDao {
            public Connection getConnection() {
                Connection conn = null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    conn = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/users", "root", "admin");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return conn;
            }
            public static void realse(ResultSet rs, PreparedStatement ps, Connection conn) {
                {
                    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;
                        }
                    }
                }
            }
        }
    

      

  • 相关阅读:
    Java IO流之缓冲流
    Java文件流之练习
    PHP使用mkdir创建多级目录的方法
    PHP中file_exists()判断中文文件名无效的解决方法
    如何用CURL将文件下载到本地指定文件夹
    解决Maximum execution time of 120 seconds exceeded
    批量插入数据insert into select示例
    PHP数据结构之五 栈的PHP的实现和栈的基本操作
    PHP数据结构之四 一元多项式的相加PHP单链实现
    PHP数据结构之三 线性表中的单链表的PHP实现
  • 原文地址:https://www.cnblogs.com/minmeishaonv/p/12813904.html
Copyright © 2020-2023  润新知