JSP第十一次作业

1.第十二周上机作业(邮件功能)的控制层代码改用为servlet实现。

<%@page import="com.gd.entity.Email"%>
<%@page import="com.gd.dao.EmailDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>du.jsp</title>
</head>

<body>
    <%
        String id = request.getParameter("id");
        int eid = Integer.parseInt(id);

        EmailDao ed = new EmailDao();
        ed.GauBian(eid);
        Email email = ed.getOneEmail(eid);
    %>
    

    <table border="1px">
        <tr>
            <td>发件人:</td>
            <td><%=email.getFa()%></td>
        </tr>
        <tr>
            <td>标题:</td>
            <td><%=email.getBiao()%></td>
        </tr>
        <tr>
            <td>内容:</td>
            <td><%=email.getNei()%></td>
        </tr>
        <tr>
            <td>时间:</td>
            <td><%=email.getTime()%></td>
        </tr>
    </table><br>
    <a href="index.jsp">返回</a>
</body>
</html>
<%@page import="com.gd.entity.Email"%>
<%@page import="com.gd.dao.EmailDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.jsp</title>
</head>

<body>
    <%
        EmailDao mail = new EmailDao();
        String user = (String) session.getAttribute("user");
        List<Email> MailList = mail.getAllEmail(user);
    %>

    欢迎你:<%=user%>
        
    <br>
    <br>
    <table border="1px">
        <tr>
            <td>编号</td>
            <td>收件人</td>
            <td>标题</td>
            <td>状态</td>
            <td>发件人</td>
            <td>时间</td>
            <td>操作</td>
            <td>操作</td>
        </tr>
        <%
            for (int i = 0; i < MailList.size(); i++) {
        %>
        <tr>
            <td>
                <%
                    out.print(MailList.get(i).getId());
                %>
            </td>
            <td>
                <%
                    out.print(MailList.get(i).getShou());
                %>
            </td>
            <td><a href="du.jsp?id=<%=MailList.get(i).getId()%>">
                    <%
                        out.print(MailList.get(i).getBiao());
                    %>
            </a>
            </td>
            <td>
                <%
                    if (MailList.get(i).getZhuang() == 0) {
                %> <img
                src="./images/sms_unReaded.png" /> <%
     } else {
 %> <img
                src="./images/sms_readed.png" /> <%
     }
 %>
            </td>
            <td>
                <%
                    out.print(MailList.get(i).getFa());
                %>
            </td>
            <td>
                <%
                    out.print(MailList.get(i).getTime());
                %>
            </td>
            <td><a href="xie.jsp?reply=<%=MailList.get(i).getFa()%>">回复</a>
            </td>
            <td><a href="shan.jsp?id=<%=MailList.get(i).getId()%>">删除</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>MyJsp.jsp</title>
</head>

<body>
    <form action="deng" method="post" name="form1">
        用户名:<input name="name" type="text"><br>
        <br> 密码:    <input name="password" type="password"><br>
        <br> <input type="button" value="登录" onclick="d()"> <a
            href="MyJsp2.jsp">注册 </a>
        <script type="text/javascript">
            function d() {
                if (form1.name.value == "") {
                    alert("用户名不能为空!");
                    return;
                }
                if (form1.password.value == "") {
                    alert("密码不能为空!");
                    return;
                }
                form1.submit();
            }
        </script>
    </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>MyJsp2.jsp</title>
</head>

<body>
    <form action="zhuce" method="post" name="form2">
        用户名:<input name="name" type="text"><br><br>
        密码:    <input name="password1" type="password"><br><br>
        确认密码:<input name="password2" type="password"><br><br> 
        <input type="button" value="注册" onclick="zc()"> 
            <a href="MyJsp.jsp">返回</a>
        <script type="text/javascript">
            function zc() {
                if (form2.name.value == "") {
                    alert("用户名不能为空!");
                    return;
                }
                if (form2.password1.value == "") {
                    alert("密码不能为空!");
                    return;
                }
                if (form2.password2.value == "") {
                    alert("确认密码不能为空!");
                    return;
                }
                if (form2.password1.value != form2.password2.value) {
                    alert("密码不一致!");
                    return;
                }
                form2.submit();
            }
        </script>
    </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>xie.jsp</title>
</head>

<body>
    <form action="xie" method="post" >
        收件人:<input type="text" name="shou" value="<%=request.getParameter("reply")%>"><br><br>
        标题:  <input type="text" name="biaoti"><br><br>
        内 容:<br><br>
        <textarea rows="10" cols="30" name="nei"></textarea>
        <br> <input type="submit" value="发送">
        <a href="index.jsp">返回</a>
    </form>
</body>
</html>
package com.gd.sevlet;

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 com.gd.dao.UsersDao;

public class deng extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public deng() {
        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();
        HttpSession session = request.getSession();
        
         request.setCharacterEncoding("utf-8");
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            UsersDao d = new UsersDao();
            if (d.DengLu(name, password)) {
                session.setAttribute("user", name);
                request.getRequestDispatcher("index.jsp").forward(request,response);
            } else {
                out.print("登陆失败!");
                response.setHeader("refresh", "1;url=MyJsp.jsp");
            }
        
    }

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

}
package com.gd.sevlet;

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 com.gd.dao.EmailDao;

public class shan extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public shan() {
        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");
        int emailId = Integer.parseInt(request.getParameter("id"));
        EmailDao ed = new EmailDao();
        ed.delEmail(emailId);
        response.sendRedirect("index.jsp");
    }

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

}
package com.gd.sevlet;

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 com.gd.dao.EmailDao;
import com.gd.entity.Email;

public class xie extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public xie() {
        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();
        HttpSession session = request.getSession();
        
        request.setCharacterEncoding("utf-8");
        String name = (String) session.getAttribute("user");
        String shou = request.getParameter("shou");
        String biao = request.getParameter("biaoti");
        String nei = request.getParameter("nei");
        Email e = new Email();
        e.setFa(name);
        e.setBiao(biao);
        e.setNei(nei);
        e.setShou(shou);
        EmailDao ed = new EmailDao();
        ed.addEmail(e);
        out.print("发送成功!");
        response.setHeader("refresh", "1;url=index.jsp");
    }

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

}
package com.gd.sevlet;

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 com.gd.dao.UsersDao;

public class zhuce extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public zhuce() {
        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");
            String name = request.getParameter("name");
            String password1 = request.getParameter("password1");

            UsersDao d = new UsersDao();
            int i = d.ZhuCe(name, password1);
            if (i > 0) {
                out.print("注册成功!");
                response.setHeader("refresh", "1;url=MyJsp.jsp");
            } else {
                out.print("注册失败!");
                response.setHeader("refresh", "1;url=MyJsp2.jsp");
            }
    }

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

}

 

 

 

posted @ 2022-07-02 23:36  O我爱吃橙子O  阅读(12)  评论(0编辑  收藏  举报