第十三周作业

1.第十二周上机作业(邮件功能)的控制层代码改用为servlet实现。
2.学习通发布了考试,截止到本周六。

package servlet;

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

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

import dao.MsgDao;
import entity.Msg;

public class AnswerServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public AnswerServlet() {
        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");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        String uname=request.getParameter("rely");
        String sendto=request.getParameter("sendto");
        String title=request.getParameter("title");
        String content=request.getParameter("content");

        Msg m=new Msg();
        m.setTitle(title);
        m.setMsgcontent(content);
        m.setUsername(uname);
        m.setSendto(sendto);

        MsgDao md=new MsgDao();
        md.addMsg(m);

        out.print("发送成功,即将跳回首页.....");
        response.setHeader("refresh", "3;url=main.jsp");

    }

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

}
package servlet;

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

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

import dao.UsersDao;

public class LoginServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public LoginServlet() {
        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");
        HttpSession session=request.getSession();
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        UsersDao ud = new UsersDao();
        if (ud.login(uname, upwd)) {
            session.setAttribute("uname", uname);
            request.getRequestDispatcher("main.jsp").forward(request,
                    response);
        } else {
            out.print("登录失败,即将跳回登录页.....");
            response.setHeader("refresh", "5;url=denglu.jsp");
        }

    }

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

}
package servlet;

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

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

import dao.UsersDao;

public class RegServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public RegServlet() {
        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 {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        UsersDao ud = new UsersDao();
        int i = ud.reg(uname, upwd);
        if (i > 0) {
            out.print("注册成功,即将跳到登录页.....");
            response.setHeader("refresh", "2;url=denglu.jsp");
        } else {
            out.print("注册失败,即将跳回注册页.....");
            response.setHeader("refresh", "2;url=reg.jsp");
        }
    }

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

}
package servlet;

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

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

import dao.MsgDao;
import entity.Msg;

public class WriteServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public WriteServlet() {
        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");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        HttpSession session=request.getSession();

        String uname=(String)session.getAttribute("uname");// 发件人在session中获取
        String sendto=request.getParameter("sendto");
        String title=request.getParameter("title");
        String content=request.getParameter("content");

        Msg m=new Msg();
        m.setTitle(title);
        m.setMsgcontent(content);
        m.setUsername(uname);
        m.setSendto(sendto);

        MsgDao md=new MsgDao();
        md.addMsg(m);

        out.print("发送成功,即将跳回首页.....");
        response.setHeader("refresh", "3;url=main.jsp");
    }

    /**
     * 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="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>   
    <title>My JSP 'answer.jsp' starting page</title>
  </head>
  <body>
   <form action="AnswerServlet" method="post" name="form">
        <table>
            <tr>
                <td>收件人</td>
                <td><input type="text" name="sendto" value="<%=request.getParameter("rely") %>">
                </td>
            </tr>
            <tr>
                <td>主题</td>
                <td><input type="text" name="title">
                </td>
            </tr>
            <tr>
                <td>内容</td>
                <td><textarea rows="6" cols="20" name="content"></textarea>
                </td>
            </tr>
            <tr>
                <td><input type="submit" value="发送">
                </td>
            </tr>
        </table>
    </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="entity.Msg"%>
<%@page import="dao.MsgDao"%>
<%@page import="dao.UsersDao"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'del.jsp' starting page</title>
  </head>
  <body>
    <%
    request.setCharacterEncoding("utf-8");
    int id=Integer.parseInt(request.getParameter("id"));
    MsgDao md=new MsgDao();
    md.delMsg(id);
    out.print("删除成功、、、、、、、");
    response.sendRedirect("main.jsp");
 %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'denglu.jsp' starting page</title>
</head>

<body>
    <h1>登录</h1>
    <script type="text/javascript">
        function validate() {
            if (loginForm.uname.value == "") {
                alert("账号不能为空!");
                return;
            }
            if (loginForm.upwd.value == "") {
                alert("密码不能为空!");
                return;
            }
            loginForm.submit();
        }
    </script>
    <form action="LoginServlet" method="post" name="loginForm">
        <table>
            <tr>
                <td>用户名</td>
                <td><input type="text" name="uname"></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="upwd" value="123456"></td>
            </tr>
            <tr>
                <td><input type="button" value="登录" onClick="validate()"></td>
                <td><a href="reg.jsp">立即注册</a></td>
            </tr>
        </table>
    </form>

</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'exit.jsp' starting page</title>
</head>

<body>
    <%
        session.invalidate();
        response.sendRedirect("reg.jsp");
    %>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="dao.MsgDao"%>
<%@page import="entity.Msg"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
    <%
        request.setCharacterEncoding("utf-8");
        String msgid = request.getParameter("id");
        int idd = Integer.parseInt(msgid);
        MsgDao md = new MsgDao();
        md.updateMsg(idd);
        Msg m = md.read(idd);
    %>
    <table>
        <tr>
            <td>发件人:</td>
            <td><input type="text" name="username" style="border: none"
                value="<%=m.getUsername()%>"></td>
        </tr>
        <tr>
            <td>主题:</td>
            <td><input type="text" name="title" style="border: none"
                value="<%=m.getTitle()%>"></td>
        </tr>
        <tr>
            <td>时间:</td>
            <td><input type="text" name="msg_create_date"
                style="border: none" value="<%=m.getMsg_create_date()%>"></td>
        </tr>
        <tr>
            <td>收件人:</td>
            <td><input type="text" name="sendto" style="border: none"
                value="<%=m.getSendto()%>"></td>
        </tr>
        <tr>
            <td>内容:</td>
            <td><div style="border: none;outline: none;overflow: inherit;">
                    <%=m.getMsgcontent()%></div></td>
        </tr>
    </table>
    <a href="main.jsp">返回</a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="entity.Msg"%>
<%@page import="dao.MsgDao"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'main.jsp' starting page</title>
</head>
<body>
    <%
        MsgDao md = new MsgDao();
        String uname = (String) session.getAttribute("uname");
        List<Msg> list = md.getMailByReceiver(uname);
    %>
    欢迎你<%=uname%>
    <a href="write.jsp">写邮件</a>
    <a href="exit.jsp">退出登录</a>
    <table border="1">
        <tr>
            <td>发件人</td>
            <td>主题</td>
            <td>状态</td>
            <td>时间</td>
            <td>操作</td>
            <td>操作</td>
        </tr>

        <%
            for (int i = 0; i < list.size(); i++) {
        %>
        <tr>
            <td><%=list.get(i).getUsername()%></td>

            <td><a href="detail.jsp?id=<%=list.get(i).getMsgid()%>">
                   <%out.print(list.get(i).getTitle().toString());%>
            </a>
            </td>
            <td>
                <%
                    if (list.get(i).getState() == 1) {
                %> <img src="image/sms_unReaded.png" /> <%
     } else {
 %> <img src="image/sms_readed.png" /> <%
     }
 %>
            </td>
            <td><%=list.get(i).getMsg_create_date()%></td>
            <td><a href="answer.jsp?rely=<%=list.get(i).getUsername()%>">回复</a>
            </td>
            <td><a href="del.jsp?id=<%=list.get(i).getMsgid()%>">删除</a>
            </td>
        </tr>
        <%
            }
        %>
    </table>

</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'reg.jsp' starting page</title>
</head>

<body>
    <h1>注册</h1>
    <script>
        function yz() {
            if (form.uname.value == "") {
                alert('用户名不能为空');
                return;
            }
            if (form.upwd.value == "") {
                alert('密码不能为空');
                return;
            }
            form.submit();
        }
    </script>
    <form action="RegServlet" method="post" name="form">
        <table>
            <tr>
                <td>用户名</td>
                <td><input type="text" name="uname"></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="upwd" value="123456"></td>
            </tr>
            <tr>
                <td><input type="button" value="注册" onclick="yz()"></td>
                <td><a href="denglu.jsp">登录</a></td>
            </tr>
        </table>
    </form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'write.jsp' starting page</title>
  </head>
  <body>
    <form action="WriteServlet" method="post" name="form">
        <table>
            <tr>
                <td>收件人</td>
                <td><input type="text" name="sendto">
                </td>
            </tr>
            <tr>
                <td>主题</td>
                <td><input type="text" name="title">
                </td>
            </tr>
            <tr>
                <td>内容</td>
                <td><textarea rows="6" cols="20" name="content"></textarea>
                </td>
            </tr>
            <tr>
                <td><input type="submit" value="发送">
                </td>
            </tr>
        </table>
    </form>
  </body>
</html>

 

 

 

 

 

 

 

posted @ 2022-05-29 18:36  假̶装̶乐̶观̶♡̶  阅读(4)  评论(0编辑  收藏  举报