美文网首页
controller in MVC - servlet

controller in MVC - servlet

作者: Zihowe | 来源:发表于2017-07-24 15:13 被阅读34次

    MVC concept

    image.png

    从model 到 controller

    // step 1: get the student data from helper class (model)
    List<Student> students = StudentDataUtil.getStudents();
    
    // step 2: add students to request object
    request.setAttribute("student_list", students);
    
    • 从model取得数据,放入request中
    • 这里就是从自己写的类里面读了数据出来,使用setAttribute放入数据到request中。

    从controller 到 view

    • RequestDispatcher
    RequestDispatcher dispatcher = 
                    request.getRequestDispatcher("view_students_two.jsp");        
    

    这个dispatcher就是作为一个wrapper来封装服务器端文件(如servlet, JSP file, or HTML file)。
    getRequestDispatcher的参数用来定位文件位置。
    这里是封装了jsp文件,位于view_students_two.jsp

    • forward method
    dispatcher.forward(request, response);
    

    这个forward就是起传递作用,将requestservlet传递到服务器的其它文件(如servlet, JSP file, or HTML file)。
    这里就是将request传递给了jsp, 目的达到。

    package com.luv2code.servletdemo.mvctwo;
    
    import java.io.IOException;
    import java.util.List;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
    * Servlet implementation class MvcDemoServletTwo
    */
    @WebServlet("/MvcDemoServletTwo")
    public class MvcDemoServletTwo extends HttpServlet {
       private static final long serialVersionUID = 1L;
          
       /**
        * @see HttpServlet#HttpServlet()
        */
       public MvcDemoServletTwo() {
           super();
           // TODO Auto-generated constructor stub
       }
    
       /**
        * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
        */
       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
           // step 1: get the student data from helper class (model)
           List<Student> students = StudentDataUtil.getStudents();
           
           // step 2: add students to request object
           request.setAttribute("student_list", students);
           
           // step 3: get request dispatcher
           RequestDispatcher dispatcher = 
                   request.getRequestDispatcher("view_students_two.jsp");      
           
           // step 4: now forward to JSP
           dispatcher.forward(request, response);
       }
    
       
       
       
       
       
       
       
       
       
       /**
        * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
        */
       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           // TODO Auto-generated method stub
           doGet(request, response);
       }
    
    }
    
    package com.luv2code.servletdemo.mvctwo;
    
    import java.io.IOException;
    import java.util.List;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
    * Servlet implementation class MvcDemoServletTwo
    */
    @WebServlet("/MvcDemoServletTwo")
    public class MvcDemoServletTwo extends HttpServlet {
       private static final long serialVersionUID = 1L;
          
       /**
        * @see HttpServlet#HttpServlet()
        */
       public MvcDemoServletTwo() {
           super();
           // TODO Auto-generated constructor stub
       }
    
       /**
        * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
        */
       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
           // step 1: get the student data from helper class (model)
           List<Student> students = StudentDataUtil.getStudents();
           
           // step 2: add students to request object
           request.setAttribute("student_list", students);
           
           // step 3: get request dispatcher
           RequestDispatcher dispatcher = 
                   request.getRequestDispatcher("view_students_two.jsp");      
           
           // step 4: now forward to JSP
           dispatcher.forward(request, response);
       }
    
       
       
       /**
        * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
        */
       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           // TODO Auto-generated method stub
           doGet(request, response);
       }
    
    }
    

    Reference
    https://docs.oracle.com/javaee/7/api/javax/servlet/RequestDispatcher.html

    相关文章

      网友评论

          本文标题:controller in MVC - servlet

          本文链接:https://www.haomeiwen.com/subject/rysbkxtx.html