登入功能(只截取重要代码以及流程说明):
1.设计编写数据库
2.编写dao层及service层
3.(重点)servlet(control)层编写:
package stu.adam.control; import org.omg.CORBA.Request; import stu.adam.entity.Classinfo; import stu.adam.service.ClassinfoService; import stu.adam.service.Impl.ClassInfoServiceImpl; 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.xml.ws.Response; import java.io.IOException; import java.time.Year; import java.util.List; @WebServlet(name = "classinfo", urlPatterns = "/classinfo") public class ClassinfoServlet extends HttpServlet { ClassinfoService classinfoService=new ClassInfoServiceImpl(); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); // type:客户端提交的请求类型 String type = request.getParameter("type"); switch (type) { case "show": showClass(request, response); break; } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void showClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Classinfo> classinfoList=classinfoService.checkAllClass(); request.setAttribute("classList",classinfoList); request.getRequestDispatcher("classAction.jsp").forward(request,response); } }
重要代码:
response.sendRedirect("classinfo?type=show"); 在获取用户不为空之后请求重定向,切记此处不可以直接跳转jsp,而是应该发出请求,此处请求对应显示班级信息
登入完
显示班级信息开始:
1.设计编写数据库
2.编写dao层及service层
3.servlet层编写:
@WebServlet(name = "classinfo", urlPatterns = "/classinfo") public class ClassinfoServlet extends HttpServlet { ClassinfoService classinfoService=new ClassInfoServiceImpl(); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); // type:客户端提交的请求类型 String type = request.getParameter("type"); switch (type) { case "show": showClass(request, response); break; } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void showClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Classinfo> classinfoList=classinfoService.checkAllClass(); request.setAttribute("classList",classinfoList); request.getRequestDispatcher("classAction.jsp").forward(request,response); } }
重点代码:
这句话将classinfoList放到jsp里面 request.setAttribute("classList",classinfoList); 这句话只能转发,不能重定向 request.getRequestDispatcher("classAction.jsp").forward(request,response);
目前进度: