In the MVC mode, PHP can be used to build pages directly at the view layer and HTML language mixing,
You can also perform data processing and process control tasks on the Controller layer, compute data in the controller, and then pass the data to the view.
For exampleCodeExample:
// Controllers/users. php $ users = getusers (); Include "../views/users. php"; // views/users. phpprint_r ($ users );
Similarly, in Java Web, JSP is used at the view layer, and Servlet plays the role of controller.
JSP is Java in HTML
Servlet is HTML in Java
That is to say, JSP is used in many HTML fields to embed dynamic content into HTML pages,
Servlet can be used to directly print data where Java is needed to process more data, or dynamic content can be transferred to JSP for display by setting request attributes,
JSP is usually compiled as a servlet in the first request to improve the running performance, for example, through Tomcat's built-in JSP Engine Jasper for compilation.
The following is a simple code example for servlet to pass computing data to JSP:
Public class userservlet extends httpservlet {protected void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {list <user> Users = getuserlist (); string url = "... "; // relative URL for display JSP page servletcontext SC = getservletcontext (); requestdispatcher RD = SC. getrequestdispatcher (URL); Request. setattribute ("users", users); Rd. forward (request, response );}
Obtain the property data in JSP:
<% USER [] users = (User []) request. getattribute ("users"); If (users. length> 0) {for (User: Users) {%> <BLOCKQUOTE> User name: <% = user. getname () %> </BLOCKQUOTE> <% }}%>
By iefreer