其他分享
首页 > 其他分享> > 第十三周

第十三周

作者:互联网

package com.entity;

import java.util.Date;

public class Email {
    private Integer id;
    private String fa;
    private String shou;
    private String title;
    private Date time;
    private String zhuang;
    private String nei;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getFa() {
        return fa;
    }
    public void setFa(String fa) {
        this.fa = fa;
    }
    public String getShou() {
        return shou;
    }
    public void setShou(String shou) {
        this.shou = shou;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public Date getTime() {
        return time;
    }
    public void setTime(Date time) {
        this.time = time;
    }
    public String getZhuang() {
        return zhuang;
    }
    public void setZhuang(String zhuang) {
        this.zhuang = zhuang;
    }
    public String getNei() {
        return nei;
    }
    public void setNei(String nei) {
        this.nei = nei;
    }
    @Override
    public String toString() {
        return "Email [id=" + id + ", fa=" + fa + ", shou=" + shou + ", title="
                + title + ", time=" + time + ", zhuang=" + zhuang + ", nei="
                + nei + "]";
    }
    public Email(Integer id, String fa, String shou, String title, Date time,
            String zhuang, String nei) {
        super();
        this.id = id;
        this.fa = fa;
        this.shou = shou;
        this.title = title;
        this.time = time;
        this.zhuang = zhuang;
        this.nei = nei;
    }
    public Email() {
        super();
    }
    
    
    
}

  

package com.entity;

public class User {
    private Integer id;
    private String uname;
    private String password;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public User() {
        super();
    }
    public User(Integer id, String uname, String password) {
        super();
        this.id = id;
        this.uname = uname;
        this.password = password;
    }
}

  

package com.jdbc2;

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 con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }
    //关闭数据库
    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.jdbc2;

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.entity.Email;

public class EmailDao extends BaseDao{
    public List<Email> getAll(String uname){
        List<Email>list=new ArrayList<Email>();
        Connection con=getConnection();
        PreparedStatement pred=null;
        ResultSet resultSet=null;
        String sql="select * from email where shou=?";
        try {
            pred=con.prepareStatement(sql);
            pred.setString(1, uname);
            resultSet=pred.executeQuery();
            while(resultSet.next()){
                Email email=new Email();
                email.setId(resultSet.getInt(1));
                email.setFa(resultSet.getString(2));
                email.setShou(resultSet.getString(3));
                email.setTitle(resultSet.getString(4));
                email.setNei(resultSet.getString(5));
                email.setTime(resultSet.getDate(6));
                email.setZhuang(resultSet.getString(7));
                list.add(email);
            }
        } catch (SQLException e) {
            
            e.printStackTrace();
        }
        return list;
    }
    public void addEmail(Email e) {
        Connection con = getConnection();
        String sql = "insert into email(fa,shou,title,nei,time,zhuang) values(?,?,?,?,?,'0')";
        PreparedStatement pred = null;
        try {
            pred = con.prepareStatement(sql);
            pred.setString(1, e.getFa());
            pred.setString(2, e.getShou());
            pred.setString(3, e.getTitle());
            pred.setString(4, e.getNei());
            pred.setDate(5, new java.sql.Date(new Date().getTime()));
            pred.executeUpdate();
        } catch (SQLException e1) {
            e1.printStackTrace();
        } finally {
            closeAll(con, pred, null);
        }
    }

    public void del(int id) {
        Connection con = getConnection();
        String sql = "delete from email where id=?";
        PreparedStatement pred = null;
        try {
            pred = con.prepareStatement(sql);
            pred.setInt(1, id);
            pred.executeUpdate();
        } catch (SQLException e1) {
            e1.printStackTrace();
        } finally {
            closeAll(con, pred, null);
        }
    }

    public void update(int id) {
        Connection con = getConnection();
        String sql = "update  email set zhuang='1' where id=?";
        PreparedStatement pred = null;
        try {
            pred = con.prepareStatement(sql);
            pred.setInt(1, id);
            pred.executeUpdate();
        } catch (SQLException e1) {
            e1.printStackTrace();
        } finally {
            closeAll(con, pred, null);
        }
    }

    public Email look(int id) {
        Connection con = getConnection();
        String sql = "select id,fa,shou,title,nei,time from email where id=?";
        PreparedStatement pred = null;
        ResultSet resultSet = null;
        try {
            pred = con.prepareStatement(sql);
            pred.setInt(1, id);
            resultSet = pred.executeQuery();
            while (resultSet.next()) {
                Email email = new Email();
                email.setId(resultSet.getInt(1));
                email.setFa(resultSet.getString(2));
                email.setShou(resultSet.getString(3));
                email.setTitle(resultSet.getString(4));
                email.setNei(resultSet.getString(5));
                email.setTime(resultSet.getDate(6));
                return email;
            }

        } catch (SQLException e1) {
            e1.printStackTrace();
        } finally {
            closeAll(con, pred, resultSet);
        }
        return null;
    }

}

  

package com.jdbc2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao extends BaseDao{
    public int Register(String uname,String password){
        int i=-1;
        PreparedStatement pred=null;
        Connection con=getConnection();
        String sql="insert into user(uname,password)values(?,?)";
        try {
            pred= con.prepareStatement(sql);
            pred.setString(1, uname);
            pred.setString(2, password);
            i=pred.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(con, pred, null);
        }
        return i;
    }
    public boolean Login(String uname,String password){
        boolean f=false;
        PreparedStatement pred=null;
        ResultSet resultSet=null;
        Connection con=getConnection();
        String sql="select * from user where uname=? and password=?";
        try {
            pred=con.prepareStatement(sql);
            pred.setString(1, uname);
            pred.setString(2, password);
            resultSet=pred.executeQuery();
            while(resultSet.next()){
                f=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return f;
    }
    
}

  

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/dodel.do")
public class DoDel extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public DoDel() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);


    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html,charset=utf-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        com.jdbc2.EmailDao e=new com.jdbc2.EmailDao();
        String id=request.getParameter("id");
        int idd=Integer.parseInt(id);
        e.del(idd);
        request.getRequestDispatcher("main.jsp").forward(request, response);
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

  

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jdbc2.UserDao;



@WebServlet("/dologin.do")
public class DoLogin extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public DoLogin() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to
     * post.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html,charset=utf-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String password = request.getParameter("password");
        UserDao ud = new UserDao();
        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();
        if (ud.Login(uname, password)) {
            session.setAttribute("uname", uname);
            request.getRequestDispatcher("index.jsp").forward(request, response);
        } else {
            out.print("登录失败,即将调回登录页.....");
            response.setHeader("refresh", "2;url=login.jsp");
        }
    }

    /**
     * Initialization of the servlet. <br>
     * 
     * @throws ServletException
     *             if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

  

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.entity.Email;
import com.jdbc2.EmailDao;

@WebServlet("/dolook.do")
public class DoLook extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public DoLook() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to
     * post.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html,charset=utf-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        int idd = Integer.parseInt(id);
        EmailDao e = new EmailDao();
        e.update(idd);
        Email email = e.look(idd);
        HttpSession session=request.getSession();
        session.setAttribute("email", email);
        request.getRequestDispatcher("look.jsp").forward(request, response);
    }

    /**
     * Initialization of the servlet. <br>
     * 
     * @throws ServletException
     *             if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

  

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jdbc2.UserDao;

@WebServlet("/doregister.do")
public class DoRegister extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public DoRegister() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html,charset=utf-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String password = request.getParameter("password");
        UserDao ud = new UserDao();
        int i=ud.Register(uname, password);
        PrintWriter out=response.getWriter();
        if(i>0){
        out.print("娉ㄥ唽鎴愬姛锛屽嵆灏嗚皟鍒扮櫥褰曢〉.....");
        response.setHeader("refresh", "2;url=Login.jsp");
        }else{
        out.print("娉ㄥ唽澶辫触锛屽嵆灏嗚皟鍒版敞鍐岄〉.....");
        response.setHeader("refresh", "2;url=register.jsp");
        }

    }
    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

  

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.entity.Email;
import com.jdbc2.EmailDao;
@WebServlet("/dowrite.do")
public class DoWrite extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public DoWrite() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);

    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html,charset=utf-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        Email e=new Email();
        HttpSession session=request.getSession();
        String fa=(String)session.getAttribute("uname");
        e.setFa(fa);
        String shou=request.getParameter("shou");
        e.setShou(shou);
        String title=request.getParameter("title");
        e.setTitle(title);
        String nei=request.getParameter("nei");
        e.setNei(nei);
        EmailDao ed=new EmailDao();
        ed.addEmail(e);
        request.getRequestDispatcher("main.jsp").forward(request, response);
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

  

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML >
<html>
  <head>
    <title>My JSP 'exit.jsp' starting page</title>
  </head>
  <body>
  <%session.invalidate(); 
  response.sendRedirect("login.jsp");
  %>
  </body>
</html>

  

<%@page import="com.entity.Email"%>
<%@page import="com.jdbc2.EmailDao"%>
<%@page import="com.jdbc2.UserDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
    <%
        String uname = (String) session.getAttribute("uname");
        EmailDao dao = new EmailDao();
        List<Email> list = dao.getAll(uname);
    %>
    欢迎<%=uname%>
    <a href="write1.jsp">写邮件</a>
    <a href="exit.jsp">退出登录</a>
    <br>
    <br>
    <table border="1px" cellspacing="0" cellpadding="30px">
        <thead></thead>
        <tr>
            <th>发件人</th>
            <th>标题</th>
            <th>时间</th>
            <th>状态</th>
            <th>操作</th>
            <th>操作</th>
        </tr>
        <tbody></tbody>
        <%
            for (int i = 0; i < list.size(); i++) {
        %>
        <tr>
            <td><%out.print(list.get(i).getFa().toString()); %></td>
            <td><a href="dolook.do?id=<%=list.get(i).getId() %>"><%out.print(list.get(i).getTitle().toString()); %></a></td>
            <td><%out.print(list.get(i).getTime().toString()); %></td>
            <td><%if(list.get(i).getZhuang().equals("0")){
            out.print("<img src='img/2.png'></img>");
            }else{
            out.print("<img src='img/1.png'></img>");
            }%>
            <td><a href="write.jsp?uname=<%=list.get(i).getFa()%>">回复</a></td>
            <td><a href="dodel.do?id=<%=list.get(i).getId()%>">删除</a></td>
            
            </td>
        </tr>
        <%
            }
        %>
    </table>
</body>
</html>

  

<%@page import="com.jdbc2.UserDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<h1>登录</h1>
    <script>
        function yz() {
            if (form.uname.value == null) {
                alert('用户名不能为空');
                return;
            }
            if (form.password.value == null) {
                alert('密码不能为空');
                return;
            }
            if (form.uname.value != null && form.password.value != null) {
                form.submit();
            }
        }
    </script>
    <form action="dologin.do" method="post" name="form">
        <table>
            <tr>
                <td>用户名</td>
                <td><input type="text" name="uname"></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td><input type="button" value="登录" onclick="yz()"></td>
                <td><a href="register.jsp">注册</a></td>
            </tr>
        </table>
    </form>
</body>
</html>

  

<%@page import="com.jdbc2.EmailDao"%>
<%@page import="com.entity.Email"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML>
<html>
<head>
<title>My JSP 'write.jsp' starting page</title>
</head>
<body>
    <%
        
   Email email=(Email)session.getAttribute("email");
    
    %>
    <table>
        <tr>
            <td>发件人:</td>
            <td><input type="text" name="fa" style="border: none"
                value="<%=email.getFa()%>">
            </td>
        </tr>
        <tr>
            <td>主题:</td>
            <td><input type="text" name="title" style="border: none"
                value="<%=email.getTitle()%>">
            </td>
        </tr>
        <tr>
            <td>时间:</td>
            <td><input type="date" name="time" style="border: none"
                value="<%=email.getTime()%>">
            </td>
        </tr>
        <tr>
            <td>收件人:</td>
            <td><input type="text" name="shou" style="border: none"
                value="<%=email.getShou()%>">
            </td>
        </tr>
        <tr>
            <td>内容:</td>
            <td><div style="border: none;outline: none;overflow: inherit;">
                    <%=email.getNei()%></div>
            </td>
        </tr>
    </table>
    <a href="index.jsp">返回</a>
</body>
</html>

  

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>注册</title>
</head>
<body>
    <h1>注册</h1>
    <script>
        function yz() {
            if (form.uname.value == "") {
                alert('用户名不能为空');
                return;
            }
            if (form.password.value == "") {
                alert('密码不能为空');
                return;
            }
            form.submit();
        }
    </script>
    <form action="doregister.do" method="post" name="form">
        <table>
            <tr>
                <td>用户名</td>
                <td><input type="text" name="uname">
                </td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="password">
                </td>
            </tr>
            <tr>
                <td>
                <input type="button" value="注册" onclick="yz()">
                </td>
                <td>
                <input type="submit" value="重置" >
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

  

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML>
<html>
<head>
<title>My JSP 'write.jsp' starting page</title>
</head>
<body>
    <form action="dowrite.do" method="post" name="form">
        <table>
            <tr>
                <td>收件人</td>
                <td><input type="text" name="shou">
                </td>
            </tr>
            <tr>
                <td>主题</td>
                <td><input type="text" name="title">
                </td>
            </tr>
            <tr>
                <td>内容</td>
                <td><textarea rows="6" cols="20" name="nei"></textarea>
                </td>
            </tr>
            <tr>
                <td><input type="submit" value="发送">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

  

标签:第十三,request,public,import,servlet,response,String
来源: https://www.cnblogs.com/ldp123/p/16320681.html