JSP and Servlet differences and relations

JSP (Java Server Pages): jsp files are converted to execute the Servlet container.
  Servlet (Server Applet) for interactively view and modify data, generate dynamic Web content.
  jsp is written inside the java code in html, servlet to write html code inside java
  jsp more focused on the front page display, servlet more focused on business logic.
Jsp servlet is the essence, the page with servlet with jsp achieve can be achieved.

jsp is an advanced package for the servlet. Nature or servlet. When first loaded jsp page, it will generate a java file, compiled in a virtual machine as a .class file, and finally loaded and initialized to a servlet. 

   jsp and can be placed (webapps subfolder of the root directory or root directory webapps) HTML configuration and does not require the same position, can be used directly.
   To use the servlet must put it in a specific location on its map must be configured.

In the framework of struts, JSP located view MVC design pattern layer, Servlet located in the control layer.

Servlet and JSP main difference that the application logic is a Java Servlet file, and completely separated from the HTML presentation layer

MVC architecture:

Write pictures described here

 

Write pictures described here

 1, Web browser sends an HTTP request to the server, the Controller (Servlet) acquires and processes (e.g. parameter analysis, request forwarding) 
 2, Controller (Servlet) call --Model core business logic part, the results obtained 
 3, Controller ( servlet) to the logic processing result View (JSP), and dynamically generated HTML content back to the browser display

Servlet program modules describes
1.JSP for displaying portions of data collection.
2.Servlet used to validate data, instantiates the JavaBean, call connection DAO database control page jump
3.DAO operable to connect to the database and the database such as: query, delete or change
4.JavaBean for the encapsulated data, results will be easily transferred and the like between the servlet jsp pages
5.servlet pre-filter request before it can reach a servlet request, response may be treated upon leaving the servlet
6.servlet Web listener listening various operations , when the event will trigger the generation of related operations, and handle this event
1,2,3,4 section above constitute the MVC pattern, JSP is among the MVC pattern V, Servlet as C, DAO fit in with the JavaBean together M.

Flow mode
       the user first opens a JSP page, the JSP page used to collect user input data (implemented using Form Form), when the user clicks the submit button, the JSP page is submitted to a corresponding Servlet, this time turning work Servlet . Servlet is submitted to data processing, the process is as follows:
1. (optional) to verify the legitimacy of the data, hash processing or the like
2. Examples of DAO, DAO calling process (update data database query)
3. DAO position data returned save to alternative (request, session, etc.)
4. page jump
during the method call Servlet the DAO, DAO process is as follows:
1. connect database (these operations may be put in the constructor of DAO which can be written on a single method)
2. the method for processing a query, the basic steps given here. Press query and pass over the parameters of the database query to obtain a result set ResultSet, for convenience Servlet returns the result to the rear of the display and convenience, the results to be packaged and ArraryLis string or JSON, like XML strings of t the form is returned to the servlet. For the encapsulated data, where it is necessary to instantiate a JavaBean, JavaBean this field correspondence among the fields of the database table, and add the get (), set () method for each field.
Serlvet After obtaining the data, then a page to jump, this time the data has been saved. We can extract the data in another JSP page which, arranged according to a certain format. At this point, a complete query operation is complete.
This is a relatively simple, but it fully embodies the idea of the MVC pattern.
 

Guess you like

Origin blog.csdn.net/weixin_38911591/article/details/90290960