• 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/dinghaiyang", "root", "root");
                } 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;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import com.gd.entity.Msg;
    
    public class MsgDao extends BaseDao {
        // 发送,回复---insert操作
    
        // 邮件列表 --select * from msg where username=....
        public List<Msg> getMailByReceiver(String receiverName) {
            List<Msg> list = new ArrayList<Msg>();
    
            try {
                Connection con = getConnection();//获取连接
                String sql = "select * from msg where sendto=?";//编写sql语句
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setString(1, receiverName);////给sql问号赋值
                ResultSet rs = ps.executeQuery();//执行查询
                //处理查询结果
                while(rs.next()){
                    //循环读取rs结果集,每一行作为一个msg对象,放入list集合中
                    Msg msg=new Msg();
                    msg.setMsgid(rs.getInt("msgid"));
                    msg.setMsgcontent(rs.getString("msgcontent"));
                    msg.setMsg_create_date(rs.getDate("msg_create_date"));
                    msg.setSendto(rs.getString("sendto"));
                    msg.setState(rs.getInt("state"));
                    msg.setTitle(rs.getString("title"));
                    msg.setUsernname(rs.getString("username"));
                    list.add(msg);
                }
                closeAll(con, ps, rs);
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return list;
        }
    
    //    public static void main(String[] args) {
    //        MsgDao md=new MsgDao();
    //        List<Msg> list=md.getMailByReceiver("小白");
    //        System.out.println(list.size());
    //    }
        // 根据id查内容 select
        public Msg getMailById(int id){
            Msg msg=new Msg();
            try {
                
                Connection con = getConnection();//获取连接
                String sql = "select * from msg where id=?";//编写sql语句
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setInt(1, id);////给sql问号赋值
                ResultSet rs = ps.executeQuery();//执行查询
                //处理查询结果
                while(rs.next()){
                    //循环读取rs结果集,每一行作为一个msg对象,放入list集合中
                    
                    msg.setMsgid(rs.getInt("msgid"));
                    msg.setMsgcontent(rs.getString("msgcontent"));
                    msg.setMsg_create_date(rs.getDate("msg_create_date"));
                    msg.setSendto(rs.getString("sendto"));
                    msg.setState(rs.getInt("state"));
                    msg.setTitle(rs.getString("title"));
                    msg.setUsernname(rs.getString("username"));
                    
                }
                closeAll(con, ps, rs);
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return msg;
        }
    
        // 阅读状态改变,,,未读 已读 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;
    
            }
        }
    //    public static void main(String[] args) {
    //        UsersDao ud=new UsersDao();
    //        try {
    //            System.out.println(ud.login("tom", "456"));
    //        } catch (SQLException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
    //    }
    
    }
    <%@page import="com.gd.entity.Msg"%>
    <%@page import="com.gd.dao.MsgDao"%>
    <%@page import="com.gd.entity.Users"%>
    <%@ 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>
        欢迎页面!!!欢迎你!!!<%
        Users u = (Users) session.getAttribute("user");
        out.print(u.getUsername());
        MsgDao md=new MsgDao();
        List<Msg> list=md.getMailByReceiver(u.getUsername());
        out.print(list.size());
    %>
    <a href="">写邮件</a>
    <table border="1"  width="1000">
    <tr>
    <td>邮件id</td>
    <td>发件人</td>
    <td>标题</td>
    <td>收件人</td>
    <td>状态</td>
    <td>时间</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    
    <%
    for(int i=0;i<list.size();i++){
     %>
     <tr>
     <td><%=list.get(i).getMsgid() %></td>
    <td><%=list.get(i).getUsernname() %></td>
    <td><a href="detail.jsp?id=<%=list.get(i).getMsgid()%>"><%=list.get(i).getTitle()%></a></td>
    <td><%=list.get(i).getSendto() %></td>
    <td><% if(list.get(i).getState()==1){ %>
    <img src="images/sms_unReaded.png"></img>
    <%}else{ %>
    <img src="images/sms_readed.png"></img>
    <%} %>
    
    
    </td><!-- 0已读,1未读 -->
    <td><%=list.get(i).getMsg_create_date() %></td>
     <td><a href="">回复</a></td>
      <td><a href="">删除</a></td>
     
     </tr>
     
     
     
     <%} %>
    
    
    </talbe>
    
    
    </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="小白" /><Br>
    密码 :<input type="password" name="upwd" value="88888"/><br>
    
    <input type="submit" value="登录">
    
    </form>
      </body>
    </html>
    <%@page import="com.gd.entity.Users"%>
    <%@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)){        
        //登录成功,创建User对象,并放入session
            Users u=new Users();
            u.setUsername(uname);
            u.setPassword(upwd);
            session.setAttribute("user", u);
            request.getRequestDispatcher("main.jsp").forward(request, response);
        }
        else
            response.sendRedirect("index.jsp");
    %>

     

  • 相关阅读:
    UE4_简易AI_玩家死亡动画bug修复
    UE4_简易AI_Ai攻击
    pikachu-SSRF
    pikachu-XXE漏洞
    pikachu-PHP反序列化
    pikachu-Over permission(越权漏洞)
    pikachu-不安全的文件下载和上传
    pikachu-File Inclusion(文件包含漏洞)
    Web安全之RCF(远程命令,代码执行漏洞)
    Web安全之SQL Inject 2
  • 原文地址:https://www.cnblogs.com/DOCEAN/p/12882982.html
Copyright © 2020-2023  润新知