第十三周作业
1.第十二周上机作业(邮件功能)的控制层代码改用为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; @WebServlet("/delete") public class delete extends HttpServlet { private static final long serialVersionUID = 1L; public dodelete() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id=Integer.parseInt(request.getParameter("id")); MsgDao md=new MsgDao(); md.delMail(id); response.sendRedirect("main.jsp"); } }
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.UsersDao; @WebServlet("/doreg") public class doreg extends HttpServlet { private static final long serialVersionUID = 1L; public doreg() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); UsersDao ud=new UsersDao(); ud.reg(uname, upwd); request.getRequestDispatcher("login.jsp").forward(request, response); } }
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.UsersDao; /** * Servlet implementation class dologin */ @WebServlet("/login") public class login extends HttpServlet { private static final long serialVersionUID = 1L; public login() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); UsersDao ud=new UsersDao(); if(ud.login(uname, upwd)){ request.getSession().setAttribute("uname", uname); request.getRequestDispatcher("main.jsp").forward(request, response); }else{ response.getWriter().print("登陆失败,即将跳回登陆页....."); response.setHeader("refresh", "5;url=login.jsp"); } } }
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; import Entity.Msg; @WebServlet("/dowrite") public class dowrite extends HttpServlet { private static final long serialVersionUID = 1L; public dowrite() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String uname=(String)request.getSession().getAttribute("uname"); String addressee=request.getParameter("addressee"); String title=request.getParameter("title"); String content=request.getParameter("content"); Msg m=new Msg(); m.setContent(content); m.setSender(uname); m.setAddressee(addressee); m.setTitle(title); MsgDao md=new MsgDao(); md.addMsg(m); response.getWriter().print("发送成功....."); response.setHeader("refresh", "5;url=main.jsp"); } }