十三周
作者:互联网
1.第十二周上机作业(邮件功能)的控制层代码改用为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 { } }
标签:ServletException,十三,request,response,import,servlet,public 来源: https://www.cnblogs.com/songchi/p/16323013.html