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.
<% = STR%>
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.
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: