Using JSTL to achieve rapid development

zhaozj2021-02-12  121

Using JSTL to achieve rapid development

In this article, I will introduce you to JSP-based and flexible techniques, which is JSTL.JSTL is called Java Server Pages Standard Tag Library. Although JSP has become very popular, JSTL is based on SQL database based on SQL database Simple, fast inner rear development is not extensive. Once you have learned JSTL, you will learn about its advantages and discover in your daily work as a programmer, it has been applied to many aspects. I am assuing that you are familiar with HTML, you can understand the basic SQL statements, and the basics of JSP. These knowledge should be involved in the following content. JSTL is a collection of tag library that has been standardized. She supports iteration, condition, XML document parsing, internationalization, and functions of using SQL and database interaction. At first, JSTL specification has been perfectred by JSR # 52 under JAVA Community Process Program, "JCP shoulder the development of Java technology" - the official website is evaluated. As an open organization, JCP has absorbed official membership and informal members. JCP has played an important leading role in the formation and development of Java technical specifications. JSTL mainly includes four basic partial tags: Core, XML, internationalization, and support for SQL. Since this article is primarily learned by the application of the SQL part, our article only introduces some basic functions of the Core and SQL tag libraries.

This technology is simple and powerful, enough to PHP, ColdFusion is higher. He has sufficient capacity in expanding the Java application, which does not include large-scalely upgraded web applications, for only the simple home page There is no problem as the web program. This allows you to avoid integration of XML often considered XML when you set up a site, and connect to the database. As I mentioned just mentioned, the key point of JSTL is simple and easy. Also, that is JSTL, based on JSP, which allows us to use all Java technology, this is necessary to remember. Before you start, we need to figure out how to run JSTL. Because we are based on JSP technology, we need a container that can compile JSP, here we use free JSP containers: Tomcat (http: //jakarta.apache. ORG / Tomcat / Index.html). As for how to install this product, it has exceeded the scope described in this article. There is no doubt that this software product is now very popular, and there are many documentation on how to install. Here, you have installed and successfully configured this container, you only need to install the JSTL to run the required file, it can download http://jakarta.apache.org/taglibs/doc/standard-doc/intro. HTML You don't need to install it, just need to put it .jar files in the web-inf / lib directory of your application, I will specify what to do later.

Because we have to run programs on a database that supports standard SQL, you need to have a database on your computer. There are many kinds of databases, here I have selected MySQL. The reason why I chose his choice is to first, we need to show the role of JSTL to construct a simple and fast application field, and can also compare with PHP mysql that is in the dominant position in this area; second point is MySQL is available for free download and include a JDBC driver for Java. In short, in order to use the following example, you need to download a mysql server (http://www.mysql.com/products/mysql/index.html; mysql connection / J jdbc driver http://www.mysql.com/products/connector-j/index.html; there is a mysql control center http://www.mysql.com/products/connector-j/index.html ), This product allows you to manage the MySQL database file very easy to operate. After all download is complete, you need to install MySQL and MySQL Control Center. There is also the JDBC driver of mysql needs to be placed in the / web-inf / lib directory in your web application. Before creating the program code, you need to create and fill in the database table. The subject matter on this aspect is also quite extensive, how to operate more than the scope of this article. Here I recommend you to a visual management tool I mentioned above. You can use him to create a test user, database, and a test table for running the program, and fill in a number of records. With regard to the login name and password database name, these configuration environment parameters, you should remember them, and you need to apply to our code later.

Now, you can prepare to create your first JSTL application. It needs us to do something below:

The entire instance code includes two files hello.jsp and continue.jsphello.jsp files Allow you to enter the database name, login name, login password, database table name. Continue.jsp: Accept data information in Hello.JSP and connect the database and perform a SELECT request to the table in the database.

Below is all of this system all code files, I will explain one by one. These codes are quite simple, for the structure of the code, I believe that you can understand even if you don't have my explanation.

1: 2: 3: 4: Hello </ title> 5: </ head> 6: <body bgcolor = "# fffff"> 7 : <h1> please, Enter All Necessary Information and Click OK. </ h1> 8: <form method = "post" action = "continue.jsp"> 9: <br> Your login to database: <input type = " TEXT "name =" login "size =" 15 "> 10: <br> Your password to database: <input type =" password "name =" password "size =" 15 "> 11: <br> your database name: <Input Type = "text" name = "database" size = "15"> 12: <br> Your database table: <input type = "text" name = "table" size = "15"> 13: <br> <br> <input type = "submit" name = "submit" value = "ok"> 14: </ form> 15: </ body> 16: </ html> (please note the number of text on the left side just for you Provide some tag information, you don't need to enter them into your code file.)</p> <p>The above is all the source code of all hello.jsp, very surprised, he is just a pure HTML code, it is so simple, I think there should be no comments. I have included these code snippets into the article is to show it. JSTL integrates how simple is those HTML sites that need to quickly expand additional function. Let me show all the code of Continue.jsp, after reading it, you will have some understanding of JSTL.</p> <p>1: <! - Continue.jsp -> 2: <% @ Taglib prefix = "c" URI = "http://java.sun.com/jstl/core"%> 3: <@ Taglib prefix = " SQL "URI =" http://java.sun.com/jstl/sql "%> 4: <c: set var =" h "value =" localhost "/> 5: <C: set var =" l " Value = "$ {param.login}" /> 6: <c: set var = "p" value = "$ {param.password}" /> 7: <c: set var = "Value =" $ {param.database} "/> 8: <c: set var =" t "value =" $ {param.table} "/> 9: <html> 10: <head> 11: <title> Continue </ Title > 12: </ head> 13: <body bgcolor = "# ffffff"> 14: <SQL: setDataSource driver = "com.mysql.jdbc.driver" url = "jdbc: mysql: // $ {l} / $ {D}? user = $ {u} & password = $ {p} "/> 15: <SQL: query var =" result "> 16: Select * from <c: out value =" $ {t} "/> 17: </ SQL: Query> 18: <c: foreach var = "row" items = "$ {result.rowsbyindex}"> 19: <c: out value = "$ {row [0]}" /> < Br> 20: </ c: foreach> 21: </ body> 22: </ html> (Please note the number of text on the left side of the text just to provide some tag information, you don't need to enter them into your code file .)</p> <p>This is all our code, is it very good? Now I have the role of the above code.</p> <p>Ring 1 is an annotation description of HTML.</p> <p>Line 2-3 These JSP tags are to reference the external label library, more precisely herein references to the Core and SQL tag library parts in the JSTL library. We all set a prefix name so that we have access to the functional methods in the introduced label library through these prefix names.</p> <p>Row 4 - 8 As hello.jsp actually runs like, he will request Continue.jsp, Continue.JSP needs to get and resolve several variables from Hello.jsp, we use this way $ {param Your_var}. At the line 4 <C: Set tag, set the variable $ {h} to "localhost", the fifth line variable $ {l} will get the information entered in the login text domain in Hello.jsp, and 6th, 7 The variables of the 8 lines will obtain the passwords, database names, and data table names entered from users in Hello.jsp, respectively. Line 9-3 is some simple HTML tags I often use to usually see the HTML web page, right, important features come immediately.</p> <p>Line 14, we tried to establish a database connection with our previous MySQL driver (com.mysql.jdbc.driver), in the URL, we specify the parameters required to connect the database connection, like the name of the database, host name, login name and landing password. CENTY, we can connect their relative database with any other JDBC driver. If we need to connect to other SQL databases, just change this URL is enough.</p> <p>Row 15 - Here we have implemented a SELECT query, please pay special attention to Chapter 16, we use another JSTL function function <c: OUT to output the name of the data sheet we gain, here we can also use other other SQL commands, such as INSERT, DELETE, and more. To perform these query requests that have not returned values, you need to use the <SQL: Update JSTL function function. He can perform them directly like <SQL: Query, just executing it does not require special specified result variables to store the result of the return statement.</p> <p>Line 18-20 Since we have implemented the above SELECT query statement, we should display his return results. <c: foreach is a function with an iterative function. When performing it, we return each data line information returned by $ {result.rowsbyndex} $ {row}, followed by line 19, we The value in the first data column of each data row returns via <c: out value = "$ {row [0]}" />. As long as the fields contained in your data table, you can access them by changing the values ​​in any of the request tables in any field in any field in the request table by changing the number of variables $ {row}.</p> <p>Row 21-22 is HTML footer in your personally created a JSTL application process, maybe you have not found out how powerful, but you should be able to realize the simple and efficacy of JSTL functional functions, trying to have JSTL Integrated SQL-based news column, how is it is easy to integrate your existing Web site.</p> <p>Very good, our code is easy to understand, even if a non-professional programmer is, a designer can also read it, understand it, understand it, and at least is the modification on the page layout. .</p> <p>As we mentioned, we need to install the JAR file in MySQL Connector / J in order to make our JSTL code. Of course, there is JSTL. Because we use Tomcat this JSP container, you need to create your own folder under Tomcat's file directory webapps, put your hello.jsp, Continue.jsp file in the file directory you created, the directory you created You still have to create a folder called Web-INF, put your profile web.xml inside, the web.xml file is as follows: <? XML Version = "1.0" Encoding = "UTF-8"?> < ! Doctype web-app public "- // sun microsystems, Inc.//dtd Web Application 2.3 // en" "http://java.sun.com/dtd/web-app_2_3.dtd"><Web-App / > Next, we also need to create a child named lib -ified and put the following files in: jstl.jar saxpath.jar standard.jar mysql-connector-java-3.0.9-stable-bin.jar (Note that this name can vary depending on your MySQL Connector / J version)</p> <p>All of this information You can check in the JSTL or Tomcat manual, if you want to understand how they are running, why do you run this, you should study these manuals. However, in order to help you quickly master the basic operation of JSTL, I have already introduced it.</p> <p>If you are using other JSP containers, you need you to read them related manual information.</p> <p>So much rocosso, I still want to explain it, this article is only the basic introduction to JSTL technology, not a complete manual. JSTL contains many functions of functions to help you complete your JSP development in a simple and fast way. I suggest you read some more detailed documentation about JSTL features and how it works with JavaBeans. Will find that he is the long-awaited development platform. By reading this article, you should have been able to create some simple SQL database-based applications.</p> <p>Translated by windowsdna 2004/1/6</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-6205.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="6205" 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.040</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 = 'cPpu7CJrOgZaid7Um4spfpvvPyjqvbsNy5WiqcHUb115G4vjID0jRVbbGvATIsDhxBgB1ngGrxH4oU7z0N_2BZLg_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>