Develop web applications using JavaServer Pages2.0

zhaozj2021-02-16  54

Develop web applications using JavaServer Pages2.0

Author Qusay H. Mahmoud July 2003

· JSP2.0

JSP2.0 provides some interesting new features compared to JSP1.2, making the web application designers and developers' life easier. The goal of JSP2.0 is to make JSP more easily, more importantly, using it without learning Java language. JSP2.0 simplifies the tag (TAG) API by joining the new extension mechanism of SimpleTag. In addition, there are some other improvements, the following is a new key feature introduced by JSP2.0. 1. Simple representation of language EL (Expression Language), which simplifies data accesses in JSP. It simplifies the code written based on JSP-based web applications, which can no longer use traditional Java scripts and Java expressions. 2. A new syntax of reusable custom behavior is defined, which is described by developers and page people in .tag and .tagx files. 3. XML syntax also has a substantial progress, joining new standard file name extensions (tag file .tagx and JSP file .jspx) This article will focus on the EL, simple tag API, and tag files. I believe that existing JSP developers will discover these new features very interesting and practical. · Why jump from 1.2 to 2.0

The version listed in JSR152 is 1.3. However, these new features that join will have a profound impact on the development model of JSP applications, so the expert group feels that as long as the primary version number is 2 to 2, it will reflect more appropriately. Of course, the new version number is also conducive to attracting new features to pay attention to new features. And the new 2.0 is fully compatible with the original version 1.2, and does not affect the original application. ·let's start!

In order to use JSP2.0, you need to support JSP 2.0 and servlet2.4 JSP containers. Jakarta Tomcat5.0 supports new JSP 2.0 and servlet2.4, you can download Tomcat or JWSDP1.2. · JSP Representation Language (EL)

Representing the language (EL) is designed for page personnel, which enhances JSP scope attributes to standard ways for business logic and JSP page information exchange. Note that although EL is a key aspect of JSP2.0, it is not a usual programming language, which is just a simple data access language, and its appearance is to facilitate access to the value of the expression in the scripting language and the request. The data. Before JSP2.0, the page personnel must use the expression <% = aname%> to access the system, as the following example: >> Or data in JavaBean <% = acustom.getaddress (). GetCountry ()%> Representative language allows page staff to access objects using a simple syntax. For example, obtain a simple variable value, you can get the properties value in the JavaBean using

You can use three forms of access to application data: objects of the object, use "." Operator or use of array elements in parentheses "[Name]". Expression $ {data} represents the scope variable DATA. You can obtain properties from the aggregation by using a period "." Or parentheses "[]" operator. 1. The period operator is used to get a named property. For example, the expression is $ {Customer.Name} represents the Name property in the Customer domain. 2. Bracket operators can also be used to get naming properties, such as $ {Customer ["name"]}. It can also use the index access attribute, $ {Customer [0]} represents the first element in the Customer collection. Indicates that the language will be treated uniformly. Therefore, $ {Customer.Name} and $ {Customer ["name]"} "} It is true that all EL use $ {and}. EL uses PageContext.FindAttribute (String) Query properties, returns null values ​​if the property does not exist. · Operator EL supports arithmetic, relationships, and logical operators, and there is also a special operator to determine if the object is empty. The operator is listed in Table 1. You can use the EMPTY operator to determine if a gathering or string is empty or NULL. For example, $ {empty param.name} is true only if the request parameter named param does not exist. The EMPTY operator can be used in combination with "!", Like $ {! Empty param.name} is true if param exists.

Table 1: EL operator

Operator

description

plus

-

Less

*

Multiply

/ or DIV

except

% or mod

Output (surplus)

== OR =

equal

! = OR! =

not equal to

Be less than

> or gt

more than the

<= or le

no greater than

> = OR GE

not less than

&& or and

Logic

|| OR OR

Logic or

OR NOT

Logical

EMPTY

Check if

a? b: c

Conditional statements

· Implicit object

In addition to the operator, EL also defines a series of implicit objects to support access to application data. Implied object definitions such as Table 2

Table 2: Implied objects provided by EL

Include object

content

ApplicationsCope

Application domain variable collection

cookie

Collection of all cookies

HEADER

HTTP request head string

HEADERVALUES

HTTP request head string collection

Initparam

Application initial parameter collection

PageContext

Javax.servlet.jsp.pageContext object

PageScope

Table area variable collection

PARAM

Request parameter string

Paramvalues

Request parameter string

Requestscope

Collection of request domain objects

Sessionscope

Collection of session domain objects

· EL example

Web Pages can use EL development on the basis of Java. Example of adding

Include objects, $ param [var] can be used to read data in the table. See form.jsp code Code Sample 1: form.jsp

form content </ title> </ head></p> <p><Body></p> <p><H3> Fill-Out-Form </ H3> <p> <form action = "form.jsp" method = "get"> name = <input type = "text" name = "value" value = "$ {param ['Name']}> <input type = "submit" value = "submit name"> </ form> <p> the name is: $ {param.name}</p> <p></ Body> </ html> In this example, when the user enters the name and click the button "Submit Name", the input name will be displayed in the same page. · Development and use functions (Function)</p> <p>EL allows you to define functions called in the expression. The function must be defined in the Public Class as a Public Static method. Once the function is developed, its signature must be mapped in the tag library descriptor (TLD). In order to illustrate the usage of the function, I use a simple plus example. First, I wrote a Java code that implements the addition function, and the example shows this feature. Code Sample 2: compute.java</p> <p>Package jsp2.examples.el; import java.util. *;</p> <p>Public class compute {public static int add (string x, string y) {int a = 0; int b = 0; try {a = integer.parseint (x); b = integer.parseint (y);} catch (Exception e) {} return a b;}} Once Javac compiles, the next step is to map function signature in TLD. Example III illustrates how to do it. Code Sample 4: Function Descriptor</p> <p><Function> <description> Add x and y </ description> <name> add </ name> <function-class> jsp2.examples.el.compute </ function-class> <function-signature> int Addd (Java. Lang.string, java.lang.string) </ function-signature> </ function> Now we can write JSP page code to use this feature. Example 4 Displays a table, the user enters two numbers and click the Add Numbers button, the function add is called, the result is displayed on the same page. Code Sample 4: Math.jsp</p> <p><% @ taglib prefix = "my" URI = "http://jakarta.apache.org/tomcat/jsp2-example-taglib%></p> <p><Head> <title> Functions </ title> </ head> <body> <h3> add number </ h3> <p> <form action = "math.jsp" method = "get"> x = <input Type = "text" name = "x" value = "$ {param [" x "]}"> <br> y = <input type = "text" name = "y" value = "$ {param [" y " ]} "> <Input type =" submit "value =" add numbers "> </ form> <p> the sum is: $ {my: add (param [" x "], param [" y "])} </ Body> </ html> • Markup processor API in JSP1.2 is complicated because the script is used in the marker. As EL appears, the JSP page that is now developed without a script is now possible. For this purpose, JSP2.0 introduces a new tag extension mechanism called simple marking extension, which can be used in two steps. 1. Java developers: Classes that implement the Javax.Servlet.jsp.TageXt.SIMpleTAG interface by defining. 2. Java-page author did not understand: Mark file by using. Here is an example. Code Example 5 introduces a simple tag processor for outputting this is my first tag! Information. Code Sample 5: Hellotag.javaPackage Jsp2.examples.SIMpletAg;</p> <p>Import javax.servlet.jsp.jspexception; import javax.servlet.jsp.tagext.simpletagsupport; import java.io.ioException;</p> <p>/ ** * SimpleTag handler that prints "This is my first tag!" * / Public class HelloTag extends SimpleTagSupport {public void doTag () throws JspException, IOException {getJspContext (). GetOut (). Write ( "This is my first tag ! ");}} Once by compiling, the next step is to define a tag descriptor in TLD. Code Sample 6: Tag Descriptor</p> <p><tag> <description> Prints this is my first tag </ description> <name> hello </ name> <tag-class> jsp2.examples.simpletag.HelloTag </ tag-class> <body-content> EMPTY </ Body-Content> </ tag> Finally, this tag I just developed in JSP can be used. Code Sample 7: HelloWorld.jsp</p> <p><% @ Taglib prefix = "MyTag" URI = "/ Web-INF / JSP2 / JSP2-EXAMPLE-TAGLIB.TLD"%> <html> <head> <title> Simple tag handler </ title> </ head> < Body> <H2> Simple Tag Handler </ h2> <p> <b> my first tag prints </ b>: <mytag: hello /> </ body> </ html> • Tag file Using Simple Tag Extension Mechanism Another way is to use the tag file. The tag file is an original file that provides a page authors with a method for abstracting the JSP code segment and reused this code by custom behavior. In other words, the tag file allows the JSP page author to create a reusable tag library using JSP syntax. The file extension required by the tag file is .tag. To illustrate how it is easy, consider the code in Example 8. Code Sample 8: Greetings.tag</p> <p>Hello the. How are you doing? After you create a tag file, you can use this custom behavior to write the JSP page. As an example, look at the code 9-style JSP. Code Sample 9: chat.jsp</p> <p><% @ taglib prefix = "tags" tagdir = "/ web-inf / tags"%> <html> <head> <title> JSP 2.0 Examples - Hello World Using a tag file </ title> </ head> <body > <H2> Tag File Example </ h2> <p> <b> The output of my first tag file is </ b>: <tags: greetings /> </ body> </ html> another tag file example</p> <p>The tag file can be used as a template. Consider the tag file described in the code 10, display.tag. This example is extended from the Panel example of Tomcat5.0. Attribute instructions are similar to the <attribute> element in TLD; it is used to define the properties of custom behavior. Code Sample 10: Display.tag</p> <p><% @ attribute name = "color"%> <% @ attribute name = "bgcolor"%> <% @ attribute name = "title"%> <table border = "0" bgcolor = "$ {color}"> <{color}> < Tr> <TD> <b> $ {title} </ b> </ td> </ tr> <tr> <td bgcolor = "$ {bgcolor}"> <jsp: dobody /> </ td> </ Tr> </ table> Code Example 11 Description How simple JSP page uses the Display tag. Code Sample 11: Newsportal.jsp</p> <p><% @ taglib prefix = "tags" tagdir = "/ web-inf / tags"%> <html> <head> <title> another tag file example </ title> </ head> <body> <h2> News Portal : Another Tag File Example </ H2> <Table Border = "0"> <TR VALIGN = "TOP"> <TD> <tags: display color = "# ff0000" bgcolor = "# ffc0c0" title = "travel"> Last French Concorde Arrives In Ny Another Travel Headline YET Another Travel Headline </ Tags: Display> </ TD> <TD> <tags: display color = "# 00fc00" BGColor = "# c0ffc0" title = "technology"> Java for in-flight entertainment <br> another technology headline <br> iNother Technology Headline <br> </ tags: display> </ td> <td> <tags: display color = "# ffcc11" bgcolor = "# fffcc" title = "sports"> American football NBA SoCCER </ tags: display> </ td> </ tr> </ table> </ Bo DY> </ html> Run this example: 1. Copy the display.tag file to the <Tomcat_Home> / WebApps / JSP-Examples / Web-INF / TAG directory. 2. Save NewSportal.jsp in the <Tomcat_Home> / WebApps / JSP-Examples / JSP2-Tutorial directory. 3. Start the Tomcat server and request a newsportal.jsp page in your browser now, you should see the following display as the template in the browser as follows</p> <p>· Conclusion JSP2.0 makes dynamic web pay more frequently, more simple maintenance. Although there is a word in JAVA in JSP2.0, it is actually the development of a new dynamic web page without learning Java languages. The above example illustrates how simple is how simple new features developed using JSP2.0. More information</p> <p>Fast TRACK JSP 1.2</p> <p>JavaServer Pages TECHNOLOGY</p> <p>JavaServer Pages Specification (JSR 152)</p> <p>The Tomcat 5 Servlet / JSP Container</p> <p>JSP developers forum</p> <p>JavaServer Pages Standard Tag Library (JSTL)</p> <p>Faster Development with JavaServer Pages Standard Tag Library (JSTL 1.0)</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-26881.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="26881" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.049</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'FJ6xi84OomuhiAUR9pIWamdVNIi4urXkTVZzXdxfgoJ68GqOyl9hzjNqJVkEzxUmk9Uw6fjuHUDo8GUgXiIDcA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>