JSP basic learning materials

xiaoxiao2021-03-06  218

jsp basis of learning materials: a cactus studio, JSP technology overview / r Author: Cactus studio after the official release JSP (JavaServer Pages) in the Sun, this new Web application development technology quickly attracted attention. JSP provides a unique development environment for creating a highly dynamic web application. According to Sun's statement, JSP can accommodate 85% of the 85% of Server products, including Apache Webserve, IIS4.0. Even if you are "a deep love", we believe that the development of JSP is still necessary. (I) JSP and ASP simple comparison JSP is very similar to Microsoft's ASP technology. Both provide mixing some program code in the HTML code, and interpret the ability to execute program code by the language engine. In an ASP or JSP environment, the HTML code is mainly responsible for describing the display style of the information, and the program code is used to describe processing logic. Ordinary HTML pages only depend on the web server, while the ASP and JSP pages require additional language engine analysis and execution program code. The execution result of the program code is rejected into the HTML code and then sent together to the browser. Both ASP and JSP are technologies for web servers, and client browsers do not require any additional software support. ASP's programming language is a scripting language such as VBScript. JSP is Java, which is the most obvious difference between the two. In addition, ASP and JSP have a more essential difference: two language engines handle the program code embedded in the page in a completely different way. Under the ASP, the VBScript code is interpreted by the ASP engine; under the JSP, the code is compiled into a servlet and executed by the Java virtual machine, which occurs when the JSP page is only requested. (Ii) Running Environment Sun's JSP homepage at http://www.javasoft.com/products/jsp/index.html, from here you can also download JSP specification, these specifications define suppliers must follow when you create a JSP engine. Some rules. The JSP code needs to be installed on the server to install the JSP engine. Here we use Sun's JavaServer Web Development Kit (JSWDK). In order to facilitate learning, this package provides a large example of an example of available modification. After installing JSWDK, just execute the startserver command to start the server. Under the default configuration, the server is listened to port 8080, using http: // localhost: 8080 to open the default page. Before running the JSP sample page, note the directory of the JSWDK installed, especially the contents of the "Work" subdirectory. When performing an example page, you can see how the JSP page is converted to a Java source file, and then compiled into a Class file (ie, servlet). The sample page in the JSWDK package is divided into two categories, they or the JSP file, or the HTML file containing a form, these forms are processed by JSP code. Like ASP, JAVA code in JSP is executed on the server side. Therefore, using the "View Source File" menu in the browser cannot see the JSP source code, you can only see the result html code. All sample source code is available through a separate "Examples" page. ㈢ JSP Page Sample Let's analyze a simple JSP page.

You can create another directory in the jswdk's examples directory, file names can be arbitrary, but the extension must be .jsp. As can be seen from the following code list, the JSP page has substantially the same structure in addition to more Java code than the ordinary HTML page. The Java code is added to the HTML code through <% and%> symbols, and its main function is to generate and display a string from 0 to 9. In front of this string, there are some texts output through the HTML code. JSP page </ title> </ head> <body> <% @ page language = "java"%> <%! String str = "0";%> <% for (int i = 1; i <10; i ) {str = STR I;}%> JSP output. <P> <% = STR%> <p> JSP output. </ Body> </ html> This JSP page can be divided into several parts. The first is the JSP instruction. It describes the basic information of the page, if the language is used, whether a session state is maintained, whether a buffer is used. The JSP directive is started by <% @,%> ends. In this example, the command "<%>" simply defines the Java language using this example (current, JAVA is the only supported language in the JSP specification). The next thing is a JSP declaration. The JSP declaration can be regarded as a place where the variables and methods of the class is defined. The JSP declaration is ended by <%! Start,%>. As "<%!" In this example, a string variable is defined. There must be a semicolon after each declaration, just like declaring member variables in a normal Java class. Code blocks between <% and%> are Java code to describe the JSP page processing logic, as shown in the For loop in this example. Finally, the code between <% = and%> is called JSP expressions, as shown in "<% = Str%>" in this example. The JSP expression provides a simple way to embed the value generated by the JSP into the HTML page. Second, session status management / R author: Cactus studio session status maintenance is a problem that Web application developers must face. There are several ways to solve this problem, such as using cookies, hidden form input fields, or directly attach status information to the URL. Java servlets provide a session object that continuously and valid between multiple requests that allow users to store and extract session status information. JSP also supports this concept in the servlet. Many of the instructions on implied objects (implicit meanings can be directly referenced in the JSP guide in Sun), which do not need to be explicitly declared, and do not require special code to create an example. For example, a Request object, it is a subclass of HttpServletRequest. This object contains all information about the current browser request, including cookies, HTML form variables, and more.</p> <p>The session object is also such an implied object. This object is automatically created when the first JSP page is loaded and is associated with the Request object. Similar to session objects in ASP, the session object in JSP is very useful for applications that do you want to complete a transaction through multiple pages. To illustrate the specific application of the session object, let's simulate a multi-page web application with three pages. The first page (Q1.html) only contains an HTML form requesting the username, the code is as follows: <html> <body> <form method = post action = "q2.jsp"> Please enter your name: <input TYPE = text name = "thename"> <input type = submit value = "submit"> </ form> </ body> </ html> second page is a JSP page (Q2.JSP), it passes the Request object Extract the thename value in the Q1.html form, store it as a Name variable, then save this Name value into the Session object. The session object is a collection of name / value, here, the name / value pair is "tell", the value is the value of the Name variable. Since the session object is always valid during the session, the variable saved here is also valid on the subsequent page. Another task of Q2.jsp is to ask the second question. Here is its code: <html> <body> <% @ page language = "java"%> <%! String name = "";%> <% Name = Request.getParameter ("Tename"); session.putValue ("TENAME", Name);%> Your name is: <% = Name%> <p> <form method = post action = "q3.jsp"> What do you like? <input type = text name = " Food "> <p> <input type = Submit value =" submit "> </ form> </ body> </ html> The third page is also a JSP page (Q3.JSP), the main task is to display the question and answer result. It extracts the value of the THENME from the Session object and displays it to prove although the value is entered in the first page, it is reserved by the Session object.</p> <p>Another task of Q3.JSP is to extract the user input in the second page and display it: <html> <body> <% @ page language = "java"%> <%! String food = ";%> <% Food = Request.getParameter ("Food"); String Name = (String) Session.getValue ("Thename");%> Your name is: <% = Name%> <p> You like: <% = Food%> </ body> </ html> Third, the JavaBean component author: Cactus Studio compiled JavaBean is a Java-based software component. JSP provides perfect support for integrating Javabean components in web applications. This support not only reduces development time (can directly utilize test and trusted components, avoid repeated development), but also bring more scalability for JSP applications. JavaBean components can be used to perform complex computing tasks or are responsible for interaction with the database and data extraction. If we have three Javabeans, they have functions of displaying news, stock prices, and weather conditions, and create a web page containing all three functions. It only needs to instantiate these three beans. You can use the HTML form to position them. . To illustrate the application of JavaBean in a JSP environment, we created a bean called TaxRate. It has two properties, namely Product (product) and RATE (tax rate). Two set methods are used to set these two properties, and the two GET methods are used to extract these two properties. In practical applications, this bean should generally extract tax rates from the database, which we simplifies this process, allowing any set tax rate. Below is this bean code list: package tax; public class taxrate {string product; double rate; public Taxrate () {this.Roduct = "A001"; this.rate = 5;} public void setProduct (String product "{THIS .Product = ProductName;} public String getProduct () {return (this.Product);} public void setRate (double rateValue) {this.Rate = rateValue;} public double getRate () {return (this.Rate);}} Apply the above Bean to the <JSP: Usebean> tag in the JSP page. Depending on the different JSP engine, where it is configured and how to configure bean may also be slightly different. This article places this bean's .class file in the C: JSWDK-1.0ExamplesWeb-Infjspeans AX directory, the TAX here is a directory that stores the bean.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-128736.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="128736" 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.050</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 = 'eepjUsGUm0R7rm5sFp0NATD0fo9LMXyBMlOv51o_2BcBvZDAXqXA63bRLdGYNscfEOc_2BHF5UdeMHKs_2BtZgxn4GDQ_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>