I think JSP has problems

zhaozj2021-02-11  183

Xiaolong Pavilion Blueski compile

(Editor: The original text of this article first out of foreign countries, JSP is only a kind of technology that has just emerged, and it is not like this. Now it seems that some views of this article may have certain limitations, but I have to admit that this is a very atmospheric work involving many JSP's internal principles. Therefore, I still need to introduce this article to everyone so that you can understand JSP technology from another side.)

Now every developer using servlets knows JSP, a Web technology that is invented by Sun's invented and spending a lot of effort to implement on SUN technology. JSP offers the HTML code in the servlet, so that web application development and page maintenance can be accelerated. In fact, the official "Application Development Model" document released by Sun: "JSP technology should be considered standard, while servlets can be considered as a supplement in most cases."

This article will compare JSP and another servlets-based technology: Template Engines.

Direct use of servlets

When servlets are invented, the entire world has seen its superiority. Servlet-based dynamic web pages can be quickly executed, can be easily transferred between multiple servers, and can be perfectly integrated with the background database, so servlets is widely accepted as a preferred platform for a web server side.

However, the HTML code that is usually implemented by a simple manner is now ready to call each line HTML line through out.println (), which becomes a serious problem in the actual servlet application. HTML content has to be implemented through code, which is a heavy time when it is a heavy cost. In addition, those responsible for web content have to ask developers to do all updates. To this end, people seek this better solution.

JSP is born

JSP 0.90 was born. In this technique, you can embed Java code into the HTML file, and the server will automatically create a servlet to the page. JSP is considered to be a simple way to write a servlet. All HTMLs can be directly obtained without having to call out of Out.println (), and those responsible for page content can directly modify HTML without having to break the risk of Java code.

However, letting the page art designer and developers work is not ideal in the same document, so that Java embeds HTML proves to be as embarrassing like HTML embedding Java. Reading a pile of chaotic code is still a difficult thing.

Thus, people use JSP to mature and use JavaBeans more. Beans contains the business rule of service required by JSP. Most of the code in JSP can be taken out in the bean, and only a minimal mark is used to call the bean.

Recently, people began to think that JSP pages in this approach is really like views. They become a component for displaying the client request results. So people will think, why not send requests directly to View? Target View If the request is not appropriate? Say, a lot of requests have 挚 芾> Media 峁鹶 峁鹶 峁鹶 视. For example, the same request may produce a successful page, a database exceptional error report, or an error report for missing parameters. The same request may generate an English page or the Spanish page, depending on the client's Locale. Why do the client must send the request to View directly? Why should the client should send the request to some universal server components and let the server to determine the return of the JSP View?

This allows many people to accept design known as "Model 2", which is a model-based model-based model defined in JSP 0.92. In this design, the request is sent to a servlet controller that performs commercial labeling and generates a similar data "model" for display. This data is then sent to a JSP "View" to display, which looks like a JSP page is an ordinary embedded JavaBean. The appropriate JSP page can be selected based on the internal logic of the SERVLET responsible for control. In this way, the JSP file has become a beautiful template view. This is another development and is admired by another developer. Enter Template Engines

If you use Template Engine to replace the JSP of the usual purpose, the next design will become simple, the syntax is simpler, and the error message is more readily read, the tool is more user-friendly. Some companies have made such engines, the most famous may be Webmacro, and their engine is free.

The developer should be clear, select a Template Engine to replace JSP to provide the following technical advantages, and these are also the shortcomings of JSP:

Question # 1: Java code is too template, although it is considered a bad design, JSP still tries to join the Java code to the web page. This is something like Java has done, that is, simplified modifications to C , Template Engines also simplifies the source code of the lower layer in JSP. Template Engines implements a better design. Question # 2: Requires Write Java code to write some Java code in the JSP page. For example, suppose a page is to determine the context of the root in the current web application to guide its homepage, it is best to use the JAVA code in JSP: home Page You can try to avoid Java code, and use the tag, but this will give you a string that is difficult to read as follows: HomePage Using Template Engine without java code and ugly Syntax. Here is the same requirement to write in WebMacro: Home Page In WebMacro, ContextPath is used as a property similar to the $ request variable using a syntax similar to Perl. Other Template Engines uses other syntax types. Look at another example, assume a advanced "View" to set a cookie to record the user's default color configuration - this task looks probably only by View instead of the servlet controller. There is such Java code in JSP: <% cookie c = new cookie ("Colorscheme", "Blue"); response.addcookie (c);%> No Java code in WebMacro: #set $ cookie.colorscheme = "Blue" As the last example, if it is also re-retrieving the color configuration in the original cookie. For JSP, we can think that there is also a corresponding tool class to help because it will make such a low-level in this low-level and difficult to do this with getCookies (). In JSP: <% string colorscheme = servletUtils.getCookie (Request, "Colorscheme");%> There is no need to tool class in WebMacro, usually: $ cookie.colorscheme.value For graphics interface design that must be written JSP Teacher, which syntax is easier to learn? JSP 1.1 Introduces Custom Tags Allows any and HTML similar tags to execute Java code in the background in the JSP page, which will have a certain value, but the premise is to have a widely known, full-featured, You can get a free, standardized tag library. There is currently no such a tag library. Question # 3: Simple work is still very tiring even if it is very simple, such as HEADER and FOOTER, still difficult in JSP. Suppose there is a "header" and a "Footer" template to include all pages, and each template is in Content contains the current page title.

The best way in JSP is: <% string title = "the page title";%> <% @ include file = "/ header.jsp"%> ... Your page content ... <% @ include file = "/ footer.jsp"%> page designer To remember that you cannot miss the first line of semicolons and to define Title as a string. In addition, / header.jsp and /footer.jsp must be in the root directory and must be an accessible full file. In WebMacro contains Headers and Footers to do more simple: #set $ title = "The page title" #parse "Header.WM" Your Content Here #Parse "Footer.WM" This is not to keep in mind for the designer Number or definition of Title, .wm file can be placed under a customizable search path. Question # 4: Very rough loop is difficult in JSP. Here is to repeat each ISP object name with JSP. <% Enumeration e = list.elements ();

While (E.haASMoreElements ()) {

Out.print ("The next name is");

Out.println ((ISP) E.NEXTELEMENT ()). getName ());

Out.print ("
");

}

%> Maybe there is a user-defined tag to do these loops. This is also true for "if". The JSP page may seem to have a very quirky Java code. At the same time, the Webmacro loop is very beautiful: #Foreach $ ISPIS $ ISPS {The next name is $ isp.name} If necessary, the # foreach directive can be kept in custom # foreach-backwards instructions. If you use JSP, it is likely to change this: (Here is a possible tag) The next name is Designer Of course, will choose the former . Question # 5: Useless error message JSP often has some amazing error information. This is because the page is first converted into a servlet and then compiles. A good JSP tool can relatively increase the possibility of finding an error position, but even the best tools cannot make all error information easily read. Some errors may not be recognized in the tool due to the transformation process. For example, assume that the JSP page needs to create a header that is common to all pages. The following code is not wrong: <% static string title = "global title";%> But Tomcat will provide the following error information: Work /% 3A8080% 2F / JC_0002EJSPJC_JSP_1.Java: 70: statement expected. Static int count = 0; ^ This information considers that the above script is placed in the _jspservice () method and the static variable is not allowed to put in the method. This syntax should be <%!%>. The page designer is hard to read these error messages. Even the best platform is not enough in this regard. Even if all Java code is removed from the page, it cannot solve the problem. In addition, what is wrong with the following expression? <% Count%> tomcat given by: work / 8080 / _0002ftest_0002ejsptest_jsp_0.java: 56: Class count not found in type declaration count ^ work / 8080 / _0002ftest_0002ejsptest_jsp_0.java:. 59:. Invalid declaration out.write ( "/ r / n "); ^ In other words, it is actually just a missing mark. Should be <% = count%>. Since Template Engine can be generated directly in the template file without any dramatic to code conversion, the appropriate error report can be given very easily. Push it according to next time, when the C language command is entered into the unix shell command line, you don't want the shell to generate a C program to run this command, but just need shell to simply interpret the command and execute, if there is an error, Out. Question # 6: Need a compiler JSP to need a compiler placed in the webserver. Since Sun refuses to abandon the Tools.jar library containing their Javac compiler, this has become problematic. The web server can include Jikes that enters a third-party compiler such as IBM. But such a compiler does not work smoothly on all platforms (written with C ) is also not conducive to establishing a pure Java web server. JSP has a pre-compilation option to play a role, but it is not perfect. Question # 7: The waste of JSP consumes additional memory and hard disk space. For each 30K JSP file on the server, you must have a corresponding class file greater than 30K.

In fact, the hard disk space is doubled. Considering that the JSP file can easily pass <% @ include> to include a large data file, such attention is very meaningful. At the same time, each JSP class file data must be loaded into the server's memory, which means that the server's memory must save the entire JSP document tree forever. A small number of JVMs have the ability to remove class file data from memory; however, programmers typically cannot control such rules to refine, and re-disclosing to large sites may not be very effective. Space is saved because there is no second file to be produced. Template Engines also provides programmers to provide full control of Templates in memory. There are also some problems with Template Engine

Template's problem # 1: There is no strict definition of Template Engine how work is not strictly defined. However, but relatively JSP, this is not very important, and the JSP is different. Template Engines does not have any special requirements for web servers - any support for servlets can support Template Engines (including API 2.0 servers such as apache / JSERV, they can't fully support JSP)! If you design a healthy competition for the best Template Engine can cause a dazzling innovation, especially the promotion of open source, (you can push and promote ideas), then Today's WebMacro will be like Perl, without strict definition but the promotion of public code organization is its standard. Template's question # 2: Nothing to recognize Template Engines has not been widely known. JSP has occupied a great commercial market and deep into the hearts. And G Template Engines can only be an unprecedented alternative technology. Template's problem # 3: Nice Template Engines has not been highly adjusted. There is no performance test and comparison for both Template Engine and JSP. Theoretically, a good Template Engine implementation should match a non-formation JSP; however, considering that third parties have made such a far-reaching promotion, and the result is that JSP is well adjusted well.

JSP role

Of course, JSP will necessarily have its position. Even from the name, it can also be seen from the similarity of JSP and ASP, they only have a difference in letters. So if you want to use ASP to turn to Java, very similar JSP environments will play a lot of role in this, and the ASP can maintain this corresponding relationship can also be launched by the designer of JSP. Taken into account.

However, it is necessary to emphasize that it is conducive to transfer to the new environment, and whether the best way to use this environment is used.

The development of JSP has become increasingly shown that it is one of the most important Java technology, which makes people leave the world of ASP - From this, Sun will support this strong commercial CASE, Java related technical supporters will also give more Vigorously support.

Unfortunately, this is actually the best solution for this Java platform. This will make the Java solution seem to be a solution without Java.


New Post(0)