JS One, JSP technology overview
After Sun officially released JSP (JavaServer Pages), this new WEB application development technology quickly caught people's attention. JSP provides a unique development environment for creating highly dynamic WEB applications. According to Sun, JSP can adapt to the market, including Apache WebServer, IIS4.0 and other 85% of the server products. Even if you are "madly" to the ASP, we believe that it is still necessary to focus on the development of JSP.
A simple comparison between ㈠jsp and ASP
JSP is very similar to Microsoft's ASP technology. Both provide the ability to mix some kind of program code in HTML code, and the language engine to interpret the execution code. In an ASP or JSP environment, HTML code is primarily responsible for describing the display style of information, while program code is used to describe the processing logic. Plain HTML pages rely on WEB servers only, while ASP and JSP pages require additional language engine analysis and code execution. The execution results of the program code are then embedded in the HTML code and sent to the browser together. Both ASP and JSP are WEB server-oriented technologies, and client browsers do not require any additional software support.
ASP's programming language is the scripting language such as VBScript, JSP uses Java, this is the most obvious difference between the two. In addition, there is a more fundamental difference between ASP and JSP: The two language engines handle embedded program code in a page in a completely different way. In ASP, VBScript code is interpreted by the ASP engine, and in JSP the code is compiled into a Servlet and executed by the Java virtual machine, which occurs only on the first request to the JSP page.
㈡ Operating Environment
Sun's JSP home page in http://www.javasoft.com/products/jsp/index.html, where you can also download JSP specifications that define some of the rules that vendors must follow to create a JSP engine.
Executing JSP code requires the JSP engine to be installed on the server. Here we are using Sun's JavaServer Web Development Kit (JSWDK). For ease of learning, this package provides a large number of examples to modify. After you install JSWDK, you can start the server by simply executing the startserver command. The server listens on port 8080 under the default configuration and opens the default page using http://localhost:8080.
Before running the JSP sample page, be aware of the directory where you installed JSWDK, especially the contents of the "work" subdirectory. When you execute the sample page, you can see here how the JSP page is converted into a Java source file and then compiled into a class file (that is, a Servlet). The sample pages in the JSWDK package are grouped into two categories, either JSP files or HTML files that contain a form that is processed by JSP code. Like the ASP, Java code in the JSP executes on the server side. Therefore, using the View Source menu in the browser is not possible to see the JSP source code, only to see the resulting HTML code. The source code for all the samples is provided through a separate "examples" page.
㈢jsp Page Sample
Below we analyze a simple JSP page. You can create another directory in the JSWDK examples directory where the file name can be arbitrary, but the extension must be. jsp. As you can see from the code listing below, the JSP page has essentially the same structure as a few more Java code than a normal HTML page. Java code is added to the HTML code by <% and%> notation, and its main function is to generate and display a string from 0 to 9. At the front and back of this string are some text that is output through the HTML code.
This JSP page can be divided into several sections to analyze.
The first is the JSP directive. It describes the basic information about the page, such as the language used, whether the session state is maintained, whether buffering is used, and so on. The JSP instruction begins with the <%@, and the%> ends. In this case, the directive "<%@ page language=" java "%>" simply defines the Java language used in this example (Currently, Java is the only supported language in the JSP specification).
The next is the JSP declaration. A JSP declaration can be viewed as a place to define variables and methods at this level of class. JSP declaration by <%! Start,%> end. As in this example, "<%!" String str= "0"; %> "defines a string variable. After each declaration, you must have a semicolon, just as you would declare a member variable in a normal Java class.
Code blocks between <% and%> are Java code that describes the processing logic of JSP pages, as shown in the For loop in this example.
Finally, the code between <%= and%> is called a JSP expression, as shown in the "<%= str%>" in this example. A JSP expression provides a simple way to embed a JSP-generated value into an HTML page.
Second, session state management
Author: Cactus Studio
Session state 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 attaching state information directly to a URL. The Java Servlet provides a session object that continues to be valid between multiple requests, allowing the user to store and extract session state information. JSP also supports this concept in the Servlet.
In Sun's JSP guide you can see a number of descriptions of suppressed objects (implicitly, these objects can be referenced directly, not explicitly declared, and do not require specialized code to create their instances). For example, the request object, which is a subclass of HttpServletRequest. This object contains all the information about the current browser request, including Cookies, HTML form variables, and so on. 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 the Session object in ASP, the sessions object in the JSP is useful for applications that want to accomplish a transaction across multiple pages.
To illustrate the specific application of the Session object, we then use three pages to simulate a multiple-page Web application. The first page (q1.html) contains only one HTML form that requires the user's name to be entered, and 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>
The second page is a JSP page (q2.jsp) that extracts the thename value from the Q1.html form from the request object, stores it as a name variable, and then saves the name value to the Session object. The Session object is a collection of name/value pairs, where the name is "Thename" in the name/value pair, and the value is the value of the name variable. Because the session object is always valid during sessions, the variables saved here are also valid for subsequent pages. Another task for q2.jsp is to ask the second question. The following is its code:
< html>
< body>
<%@ page language= "java"%>
<%! String name= ""; %>
<%
Name = Request.getparameter ("thename");
Session.putvalue ("Thename", name);
%>
Your name is: <%= name%>
< p>
< FORM method=post action= "q3.jsp" >
What do you like to eat?
< INPUT type=text name= "Food" >
< p>
< INPUT type=submit value= "SUBMIT" >
</form>
</body>
The third page is also a JSP page (q3.jsp), the main task is to display the question and answer results. It extracts the value of Thename from the Session object and displays it to prove that although the value is entered in the first page, it is retained through the session object. Another task for q3.jsp is to extract the user input from 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 to eat: <%= food%>
</body>
Iii. Referencing JavaBean components
Author: Cactus Studio compilation
JavaBean is a Java-based software component. JSP provides complete support for integrating JavaBean components in WEB applications. This support not only shortens development time (can directly leverage tested and trusted existing components, avoids duplication of development), but also provides more scalability for JSP applications. The JavaBean component can be used to perform complex computational tasks, or to interact with the database and to extract data. If we have three JavaBean, each with a feature that displays news, stock prices, and weather conditions, then creating a Web page that contains all three features requires only instantiating the three beans and using HTML tables to locate them in turn.
To illustrate the application of JavaBean in the JSP environment, we created a Bean named TaxRate. It has two attributes, namely product (products) and Rate (tax rate). The two set methods are used to set these two properties, and two get methods are used to extract the two properties. In practice, this Bean should generally extract the tax value from the database, where we simplified the process and allowed arbitrary tax rates. Here is the code listing for this Bean:
Package tax;
public class TaxRate {
String Product;
Double Rate;
Public TaxRate () {
This. Product = "A001";
This. Rate = 5;
}
public void Setproduct (String ProductName) {
This. Product = ProductName;
}
Public String getproduct () {
Return (this. PRODUCT);
}
public void Setrate (double ratevalue) {
This. Rate = RateValue;
}
Public double getrate () {
Return (this. Rate);
}
}
Applying the above Bean to the JSP page uses the < jsp:usebean> tag. Depending on the specific JSP engine used, there may be a slightly different way of configuring and configuring the Bean. This article puts the. class file of this bean in the C:\jswdk-1.0\examples\WEB-INF\jsp\beans\tax directory, where tax is a directory that stores the bean specifically. The following is a sample page that applies the above Bean:
There are several properties defined within the < jsp:usebean> tag, where the ID is the identity of the bean within the entire JSP page, and the scope property defines the lifetime of the Bean, which describes the Bean's class file (starting with the package name).
This JSP page uses not only the bean's set and get methods to set and extract the property values, but also the second way to extract the value of the Bean property, using the < jsp:getproperty> tag. The Name property in < jsp:getproperty> is the ID of the Bean defined in < jsp:usebean>, and its property attribute specifies the name of the target attribute.
As it turns out, the Java Servlet is an ideal framework for developing WEB applications. JSP is based on Servlet technology and has been improved in many ways. The JSP page looks like a normal HTML page, but it lets you embed execution code, which is very similar to the ASP technology. Using JavaBean components that run across platforms, JSP provides an excellent solution for separating processing logic and display styles. JSP will become a strong competitor of ASP technology.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.