Java MVC (Model View Controller) ---- (JSP + servlet + javabean instance)

Source: Internet
Author: User

I finally came into contact with MVC in DRP. I feel that such an architecture system is indeed quite flexible. Now I feel that tomcat is used as a server for publishing much better than IIS. At least it is very simple and easy to use.


First, let's take a simple look at the basic knowledge of MVC. The full name of MVC is Model View Controller, short for model-view-controller, A software design model that organizes Code by means of explicit separation of business logic and data, and aggregates business logic into a component, the interface and user interaction around data can be improved and customized without re-writing the business logic.


Overview



MVC is uniquely developed to map traditional input, processing, and output functions in a logical graphical user interface structure. MVC is the embodiment of hierarchical thinking, but it is different from the three-tier design model (we will share the difference with you later ).





MVC is a framework model that forcibly separates the input, processing, and output of an application. MVC applications are divided into three core components: model, view, and controller. They process their own tasks. The most typical MVC is the JSP + servlet + javabean mode.



Instance resolution


UML diagram: familiar with MVC call process logic





First: JSP: the query interface query_condention.jsp, which consists of page commands and HTML, is similar to the current html page and asp page.



<% @ Page language = "java" contentType = "text/html; charset = GBK" %>Student Information


Second: the control layer SearchStudentServlet is used to accept the customer's request to process the process. It calls Model (StudentManager. java) and forwards it to the student_list.jsp page of the backend server to be requested.




Import java. text. *; import java. util. *; import java. io. *; import javax. servlet. http. *; import javax. servlet. *; import com. bjpowernode. exam. model. *; import com. bjpowernode. exam. manager. *; public class SearchStudentServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, response);} public void doPost (HttpServletRequest, httpServletResponse response) throws ServletException, IOException {String sBeginDate = request. getParameter ("beginDate"); String sEndDate = request. getParameter ("endDate"); Date beginDate = new Date (); Date endDate = new Date (); try {beginDate = new SimpleDateFormat ("yyyy-MM-dd "). parse (sBeginDate); endDate = new SimpleDateFormat ("yyyy-MM-dd "). parse (sEndDate);} catch (Exception e) {e. printStackTrace ();} StudentManager studentManager = new StudentManagerImpl (); List
 
  
StudentList = studentManager. findStudentList (beginDate, endDate); // sets the student list to the requet range // request. setAttribute ("student_list", studentList); // forward, which is forwarded on the server and unknown on the client. // request. getRequestDispatcher ("/student_list.jsp "). forward (request, response); // put studentList in session HttpSession session = request. getSession (); session. setAttribute ("student_list", studentList); // redirection, does not share the request // The following syntax error. This "/" indicates port 8080 // response. sendRedirect ("/student_list.jsp"); response. sendRedirect (request. getContextPath () + "/student_list.jsp ");}}
 

Third: The student_list.jsp page receives data to form html, returns to the browser, and renders the data on the interface.



<% @ Page language = "java" contentType = "text/html; charset = GBK" %> <% @ page import = "java. util. * "%> <% @ page import =" java. text. * "%> <% @ page import =" com. bjpowernode. exam. model. * "%> <% @ page import =" com. bjpowernode. exam. manager. * "%>Student Information
 
  
StudentList = (List) request. getAttribute ("student_list"); List
  
   
StudentList = (List) session. getAttribute ("student_list"); for (Iterator
   
    
Iter = studentList. iterator (); iter. hasNext ();) {Student student = iter. next (); %>
   
  
 
 
 <% // List 
   <% Long B = 1000L * 60L * 60L * 24L * 365L; long a = System. currentTimeMillis ()-student. getBirthday (). getTime (); %> 
    <% }%> 
  
Student code Name Gender Date of birth Contact number Home address Class Name Age
<% = Student. getStudentId () %> <% = Student. getStudentName () %> <% = Student. getSex () %> <% = New SimpleDateFormat ("yyyy-MM-dd"). format (student. getBirthday () %> <% = Student. getContactTel () %> <% = Student. getAddress () %> <% = Student. getClasses (). getClassesName () %><% = A/B %>



In View's student_list.jsp page, there is a large mix of html and java code. In the query condition interface, query_condention.jsp is mainly html, because it does not involve background data interaction.



Fourth: xml configuration Servlet:


 
 
  
   
    SearchStudentServlet
   
   
    SearchStudentServlet
   
  
  
   
    SearchStudentServlet
   
   
    /SearchStudentServlet
   
  
 


Fifth: display query results

Java is a type of blog that can be learned and improved together. Stay tuned!






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.