jsp第十三周作业

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

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

public class servlet2 extends HttpServlet {

    
    public servlet2() {
        super();
    }

    
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request,response);
    }

    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
          String uname = request.getParameter("uname");
            String upwd =request.getParameter("upwd");
            UsersDao usersDao=new UsersDao();
            if(usersDao.login(uname, upwd)){
                request.getSession().setAttribute("uname", uname);
                request.getRequestDispatcher("main2.jsp").forward(request, response);
            }
            else{
         
                response.setHeader("refresh", "5;url=login.jsp");
            }
    }

    
    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.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.UsersDao;

public class servlet3 extends HttpServlet {

    public servlet3() {
        super();
    }

    
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost (request, response);
        
    }

    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        UsersDao usersDao = new UsersDao();
        int i=usersDao.reg(uname, upwd);
        if(i>0){
        
            response.setHeader("refresh", "2;url=login.jsp");
        }else{
    
            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 com.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 com.dao.MsgDao;
import com.entity.Msg;

public class servlet4 extends HttpServlet {

    
    public servlet4() {
        super();
    }

    
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost( request, response);
    }

    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

         request.setCharacterEncoding("utf-8");
            String uname = (String)  request.getSession().getAttribute("uname");
            String sendto = request.getParameter("receiver");
            String title = request.getParameter("title");
            String content = request.getParameter("content");

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

            MsgDao md = new MsgDao();
            md.addMsg(m);
            response.getWriter().print("发送成功.....");
            response.setHeader("refresh", "3;url=main2.jsp");
    }

    
    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.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.MsgDao;

public class servlet5 extends HttpServlet {

    public servlet5() {
        super();
    }

    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request,  response);
    }

    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
         
        int id=Integer.parseInt(request.getParameter("id"));
        MsgDao md=new MsgDao();
        md.delMail(id);    
        response.getWriter().print("刪除成功.....");
        response.setHeader("refresh", "2;url=main.jsp");
        response.sendRedirect("main2.jsp");
    }

    
    public void init() throws ServletException {
    
    }

}

 

posted @ 2022-05-29 20:49  计算机1902佟阳  阅读(9)  评论(0编辑  收藏  举报