十三周作业
1.第十二周上机作业(邮件功能)的控制层代码改用为servlet实现。
登录的:
package com.nn.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.nn.Dao.usersDao; public class Login extends HttpServlet { /** * Constructor of the object. */ public Login() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); HttpSession session=request.getSession(); String uname = request.getParameter("uname"); String upwd =request.getParameter("upwd"); usersDao usersDao=new usersDao(); try { if(usersDao.login(uname, upwd)){ session.setAttribute("uname", uname); request.getRequestDispatcher("zhuye.jsp").forward(request, response); } else{ response.setHeader("refresh", "5;url=login.jsp"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
注册的:
package com.nn.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.nn.Dao.usersDao; public class Reg extends HttpServlet { /** * Constructor of the object. */ public Reg() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String uid=request.getParameter("id"); Integer id=Integer.parseInt(uid); String uname=request.getParameter("uname"); String upwd =request.getParameter("upwd"); String upwd2 =request.getParameter("upwd2"); usersDao usersDao=new usersDao(); if(upwd.equals(upwd2)){ try { if(usersDao.id(id)){ out.print("已经有这个id了,即将跳回注册页....."); response.setHeader("refresh", "5;url=zhuce.jsp"); } else{ usersDao.zhuce(id, uname, upwd); out.print("注册成功,即将跳回登录页....."); response.setHeader("refresh", "5;url=login.jsp"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else{ out.print("两次密码不一致,即将跳回注册页....."); response.setHeader("refresh", "5;url=zhuce.jsp"); } out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }