第十三周作业

package servlet;

import java.io.IOException;

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 dao.MsgDao;
import dao.UsersDao;

/**
 * Servlet implementation class doLogin
 */
@WebServlet("/doLogin")
public class doLogin extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public doLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        HttpSession session=request.getSession();
        request.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        UsersDao ud=new UsersDao();
        MsgDao md=new MsgDao();
        if(ud.login(uname, upwd)){    
            session.setAttribute("uname", uname);    
            request.getRequestDispatcher("main.jsp").forward(request, response);
        }else{
            System.out.println("登陆失败,即将跳回登陆页.....");
            response.setHeader("refresh", "5;url=denglu.jsp");
        }
    }

}
复制代码

servlet  doRes

复制代码
package servlet;

import java.io.IOException;

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 dao.MsgDao;
import dao.UsersDao;

/**
 * Servlet implementation class doRes
 */
@WebServlet("/doRes")
public class doRes extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public doRes() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        HttpSession session=request.getSession();
        request.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        UsersDao ud=new UsersDao();
        MsgDao md=new MsgDao();
        if(ud.reg(uname, upwd)>0){    
            session.setAttribute("uname", uname);    
            request.getRequestDispatcher("denglu.jsp").forward(request, response);
        }else{
            System.out.println("登陆失败,即将跳回登陆页.....");
            response.setHeader("refresh", "5;url=shibai.jsp");
        }
    }

}
复制代码

servlet doDel

复制代码
package servlet;

import java.io.IOException;

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 dao.MsgDao;

/**
 * Servlet implementation class doDel
 */
@WebServlet("/doDel")
public class doDel extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public doDel() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        
        int id=Integer.parseInt(request.getParameter("id"));
        MsgDao md=new MsgDao();
        md.delMsg(id);
        
        response.sendRedirect("main.jsp");
    }

}
复制代码

servlet dowrite

复制代码
package servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

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 dao.MsgDao;
import entity.Msg;

/**
 * Servlet implementation class dowrite
 */
@WebServlet("/dowrite")
public class dowrite extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public dowrite() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        HttpSession session=request.getSession();
        request.setCharacterEncoding("utf-8");
        String uname=(String)session.getAttribute("uname");// 发件人在session中获取
        String sendto=request.getParameter("sendto");
        String title=request.getParameter("title");
        String content=request.getParameter("content");
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        Msg m=new Msg();
        m.setTitle(title);
        m.setMsgcontent(content);
        m.setUsername(uname);
        m.setSendto(sendto);
        m.setMsg_create_date(calendar.getTime());
        MsgDao md=new MsgDao();
        md.addMsg(m);
        System.out.println("发送成功,即将跳回首页.....");
        response.setHeader("refresh", "3;url=main.jsp");
    }

}
posted @ 2022-05-29 21:50  崔明月  阅读(18)  评论(0编辑  收藏  举报