Differences between JSP and Servlet and PHP analogy

Source: Internet
Author: User

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

Related Article

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.