JSP第十三周作业

package com.fj.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 com.lwk.dao.MsgDao;

@WebServlet("/del")
public class DelServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        int mid = Integer.parseInt(request.getParameter("mid"));
        MsgDao md = new MsgDao();
        md.delMsg(mid);
        request.getRequestDispatcher("main.jsp").forward(request, response);
    }
}
package com.fj.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 com.lwk.dao.UsersDao;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        doGet(req, resp);
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String uname=request.getParameter("uname");    
        String upwd=request.getParameter("upwd");
        HttpSession session = request.getSession();
        UsersDao ud=new UsersDao();
        if(ud.login(uname, upwd)){
            session.setAttribute("uname", uname);
            request.getRequestDispatcher("main.jsp").forward(request, response);
        }else{
            response.sendRedirect("no.jsp");
        }
    }

}
package com.fj.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 com.lwk.dao.UsersDao;
import com.lwk.entity.Users;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        doGet(req, resp);
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String uname=request.getParameter("username");    
        String upwd=request.getParameter("password");
        String email=request.getParameter("email");
        
        UsersDao ud=new UsersDao();
        Users u=new Users(uname,upwd,email);
        ud.register(u);
        response.sendRedirect("login.jsp");
    }
}
package com.fj.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 com.lwk.dao.MsgDao;
import com.lwk.entity.Msg;

@WebServlet("/write")
public class WriteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String receiver = request.getParameter("sendto");
        String sender = (String) session.getAttribute("uname");

        Msg m = new Msg();
        m.setSendto(receiver);
        m.setUname(sender);
        m.setTitle(title);
        m.setMsgcontent(content);

        MsgDao md = new MsgDao();
        md.writeMsg(m);
        request.getRequestDispatcher("main.jsp").forward(request, response);
    }
}

 

posted @ 2022-05-29 12:35  计算机1905方骏  阅读(16)  评论(0编辑  收藏  举报