zoukankan      html  css  js  c++  java
  • JSP第七次作业

    Msg.java

    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;
        }
        
    }

    User.java

    package com.gd.entity;
    
    public class User {
        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;
        }
    
    }

    BaseDao.java

    package com.gd.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");
                    // 2.建立连接
                    conn = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/jdbc", "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();
            }
        }
    
    }

    UserDao.java

    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();
    //        }
    //    }
    
    }

    index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        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" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <form action="dologin.jsp" method="post">
    用户名:<input type="text" name="uname" value="kitty" /><Br>
    密码 :<input type="password" name="upwd" value="777"/><br>
    
    <input type="submit" value="登录">
    
    </form>
    
    </body>
    </html>

    dologin.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@page import="com.gd.dao.UsersDao"%>
    <!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>Insert title here</title>
    </head>
    <%
        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");
        }
    %>
    </html>

    main.jsp

    <%@ 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>Insert title here</title>
    </head>
    <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/user";
                String user = "root";
                String password = "root";
                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>

     

     

     

  • 相关阅读:
    java Double数据类型比较大小
    java基础02-标识符和关键字
    java基础01-注释
    java程序运行机制
    面试准备之java异常体系
    双亲委派模型
    java类加载器有哪些?
    什么是字节码?采用字节码的好处是什么?
    如何实现一个ioc容器
    ConcurrentHashMap原理,jdk7和jdk8的区别
  • 原文地址:https://www.cnblogs.com/rongrui/p/12880502.html
Copyright © 2011-2022 走看看