New Java movement Nava: PHP Technology to develop Web Applications

Source: Internet
Author: User

Currently, PHP is the mainstream Web applications, especially Internet and mobile Internet websites. In addition, Python is becoming more and more technical-oriented projects with a small and fresh attitude. Some new users may also use node. js to launch Web websites. Java, which has never been used in the past few years, is now becoming marginalized in the Web development field. However, for a single language, no matter in terms of performance, performance, and scalability, the Java language is undoubtedly excellent, and the root cause of Java's dilemma in the Web field is, it is not the Java language, but the mindset of Java practitioners. The Java industry attaches too much importance to the framework, which directly leads to the popularity of heavyweight SSH frameworks such as Struts + Spring + Hibernate, which cannot be tolerated by the agile development team. In addition, Java has always advocated JEE enterprise-level application development, which is widely used by EJB, JCA, and other technologies. These are typical dragon slaughter techniques and are ironic, the current large-capacity, high-concurrency system is basically not developed by JEE.

To sum up, the Java language itself is excellent, but it is gradually becoming weaker in the field of agile development due to the drag of frameworks and specifications. However, if we can change our mindset and adopt the current mainstream PHP development model, we believe that Java has the advantages of language completeness and performance in terms of Web development.

First, let's analyze the processing process of a typical PHP application. The user first requests an index. php page, which completes various initialization work, routes it to the corresponding Controller, executes the business logic, generates the Model to be displayed on the page, and then calls the page template, display the Model content to the page.

By directly ing the above-brown idea to Java, a super simple Web framework can be obtained.

First, a Web application has a Servlet that processes all user requests. As follows:

/*** Whether POST or GET request, all processes through this function * @ param request * @ param response * @ throws IOException * @ throws ServletException */public void doProcess (HttpServletRequest request, HttpServletResponse response) throws IOException, servletException {request. setCharacterEncoding ("UTF-8"); response. setCharacterEncoding ("UTF-8"); response. addHeader ("Content-Type", "text/html; charset = UTF-8"); PrintWriter out = respons E. getWriter (); // obtain the request command String cmd = "none"; if (request. getParameter ("cmd ")! = Null) {cmd = request. getParameter ("cmd");} // call different methods based on the request command switch (cmd) {case "dispApplyCapital": dispApplyCapital (request, response); break ;}}

As shown in the preceding figure, the system forwards the command to different processing methods based on different cmd parameters in the request. For example, in the above example, the dispApplyCapital command will call the dispApplyCapital method. The Code is as follows:

Private void dispApplyCapital (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {String json = "{\" name \ ": \" yt Yan Tao \ ", \" userId \": 200} "; request. setAttribute ("json", json); Map <String, String> model = new HashMap <String, String> (); model. put ("page_title", "stock trading"); model. put ("header_title", "transaction v1"); JSONArray capOpts = new JSONArray (); JSONObject capOptsItem = null; try {capOptsItem = new JSONObject (); capOptsItem. put ("amount", 1); capOpts. put (capOptsItem); capOptsItem = new JSONObject (); capOptsItem. put ("amount", 10); capOpts. put (capOptsItem); capOptsItem = new JSONObject (); capOptsItem. put ("amount", 100); capOpts. put (capOptsItem);} catch (JSONException e) {// TODO Auto-generated catch blocke. printStackTrace ();} model. put ("capOpts", capOpts. toString (); model. put ("procFeeRatio", "0.00399"); model. put ("marginRatio", "0.08"); request. setAttribute ("model", model); this. getServletContext (). getRequestDispatcher ("/caporder/apply_capital.jsp "). forward (request, response );}

In the above Code, calling the Controller and generating the Model content are all completed in this method, mainly for demonstration purpose. In actual application, you need to call an independent Controller class, generate the Model content. At the same time, it should be noted that in the code above, the value object is not defined, but the Map interface class is directly used, because a value object needs to be defined when any content is passed in Java, it is very troublesome. In contrast, PHP basically implements all parameter passing through arrays. arrays in PHP are roughly equivalent to Map in Java. Therefore, the Model content is transmitted to the page through the Map object. Finally, the request is forwarded to the corresponding page.

The template technology is implemented on the page. The mainstream Java method is to use tags to add different content to different templates. However, PHP directly uses require_once to implement the function that contains other pages. Therefore, we also directly use <% @ page include = "" %> in JSP to implement the function. The specific code is as follows:

<% @ Page contentType = "text/html; charset = UTF-8" %> <% @ include file = "/header. jsp "%> <script> var g_mjson = null; $ (document ). ready (function () {g_mjson = <% = model. get ("capOpts") %>; var opts = ""; var I = 0; for (I = 0; I <g_mjson.length; I ++) {opts + = "<input type = 'Radio 'name = 'applyamount 'value = '" + g_mjson [I]. amount + "'>" + g_mjson [I]. amount + "Ten Thousand" ;}$ ("# capOpts" 2.16.html (opts); var procFeeRatio = <% = model. get (" ProcFeeRatio ") % >;$ (" # procFeeTxt "). val ("0.0"); $ ("input [name = 'applyamount ']"). click (function (eve) {alert ("click me! V1 v = "+ $ (" [name = 'applyamount ']: checked "). val () + "! "); $ (" # ProcFeeTxt "). val (procFeeRatio * 10000.0) ;};}); </script> real-disk application <% = model. get ("capOpts") %> <br/> application amount: <span id = "capOpts"> </span> <input type = "text" id = "amount"/> <br/> service fee: <input type = "text" id = "procFeeTxt" readonly/> gold account: <a href = "#"> recharge now </a> <br/> deposit: wallet account: <a href = "#"> recharge now </a> <br/> <input type = "button" value = "Confirm application"/> <input type = "button" value = "cancel application"/> <br/> <% @ include file = "/footer. jsp "%>

In the above Code, the javascript library jQuery is supported by the header. jsp file. In the page display, JSP is directly used as the template technology to directly print the content in the Model to the page. PHP also adopts the special template technology style, but it usually sacrifices the performance. The template technology is only worth the cost to avoid the emergence of Java or PHP code on the page. Currently, most page design tools may perfectly solve the problem of embedding Java or PHP code in the page, therefore, you can embed code directly in the page. At the same time, this also allows developers to learn less of a template language, reducing the burden on developers.

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.