JAVA new technology - JSP
Abstract: JAVA new technology - JSP
<tr> <td> <table CellSpacing=0 cellPadding=0 width="95%" border=0> <tr> <td class=NEWS id=zoom> Author: Cactus studio
Sun released in JSP (JavaServer Pages), the new Web application development technology soon attracted the attention of the people. JSP for creating highly dynamic Web application provides a unique development environment.
According to Sun's argument, JSP able to adapt to the market, including the Apache WebServer, IIS4.0, 85 per cent of the server products. Even if you ASP "一往情深", in our view, remains concerned about the development of JSP is necessary.
JSP and ASP (i) the simple comparison JSP and Microsoft's ASP technology is very similar. Both are available in the HTML code in a mixed code from the language engine explain the ability of the code enforcement procedures. In ASP or JSP environment, HTML code is mainly responsible for the display of information describing patterns, and the code is used to describe processing logic. Ordinary HTML pages that relies on the Web server, and ASP and JSP pages in the language of the engine needs additional analysis and implementation code. Results of the implementation of the code was re-embedded into the HTML code, and then send them in with your browser. ASP and JSP are technology-oriented Web server, the client browser does not require any additional software support.
ASP programming language is VBScript like scripting language, the use of Java JSP, which is the most obvious difference between the two. In addition, ASP and JSP there is a more fundamental difference between: two languages engine with completely different way of dealing with pages embedded code. In ASP, ASP VBScript code enforcement engine explained in the JSP, Servlet code compiled into by the implementation of Java Virtual Machine, which operate only in the compiler of the JSP pages of the first request occurred.
(Ii) operating environment, Sun's JSP Home in http://www.javasoft.com/products/jsp/index.html, also can be downloaded from here JSP norms, these norms defined providers in the creation of JSP engine must comply with the some rules.
JSP implementation of the code needs installed on the server JSP engine. Here we are using the Sun's JavaServer Web Development Kit (JSWDK). To facilitate learning, this package provides a lot of examples for the revision. JSWDK after installation, simply startserver order to start the implementation of the server. In the default configuration server listening on port 8080, the use of http://localhost:8080 to open default pages.
Examples running JSP pages, please JSWDK attention to the installation directory, in particular the "work" of the contents of a subdirectory. Examples of the implementation of pages, you can see here how the JSP pages are converted into Java source files, then be compiled into class files (ie Servlet). JSWDK package examples of pages divided into two categories, either JSP document is, or contains a form of HTML documents, which deal with forms by the JSP code. And ASP, JSP in Java code are in the implementation of server-side. Therefore, in the browser using the "View Source" menu is unable to see the source code of JSP, HTML code can only see the result. All are examples of the source code through a separate "examples" page provided.
(Iii) Examples Below we JSP pages of a simple JSP pages. You can JSWDK the examples directory to create another directory to store documents, names can be arbitrary, but the extension must be. Jsp. The following code from the list can be seen, apart from JSP pages than ordinary HTML pages more Java code, both have the same basic structure. Java code through <% and%> symbols into the middle of the HTML code, and its main function is to generate and display from a 0-9 string. In the front and rear of the string are output through the text of the HTML code.
<HTML>
<HEAD> <TITLE> JSP pages </ TITLE> </ HEAD>
<BODY>
<% @ Page language = "java"%>
<%! String str = "0";%>
<% For (int i = 1; i <10; i + +) (
Str = str + i;
)%>
JSP output before.
<P>
<% = Str%>
<P>
JSP output after.
</ BODY>
</ HTML>
The JSP pages can be divided into several parts to analyze.
JSP is the first directive. It describes the basic information pages, such as the language used, whether to maintain session state, such as whether or not to use the buffer. JSP instructions from <% @,%> end. In this case, the directive "<% @ page language =" java "%>" simply defines the case of the use of the Java language (At present, the JSP specification Java is the only language support).
Followed by the JSP statement. JSP statement can be regarded as a definition of the level of the local variables and methods. J
SP statement from <%!,%> End. If the case is "<%! String str =" 0 ";%>" definition of a string variable. In each of the following statements must have a semicolon, as in ordinary Java class member variables in the same statement.
At <% and%> block between the JSP pages dealing with the description of the logic of Java code, as is the case for the cycle shown.
Finally, at <=% and%> between the JSP code known as expression, such as in this case is "<= str%%>" shown. JSP provides a formula generated JSP will be embedded in HTML pages numerical simple method.
Second, the session state management is to maintain session state Web application developers must face. There are many ways can be used to solve this problem, such as the use of Cookies, hidden form input domains, or status information directly attached to the URL. Java Servlet provided a request in a number of effective and ongoing conversation between the object, the object allows users to store and extract information session state. JSP Servlet also support the concept.
Sun's JSP Guide can be seen in many of the hidden object (the implied meaning is that these objects can be directly quoted, and does not need explicit statement and does not require specialized code to create its examples). For example, request object, it is a sub-category HttpServletRequest. The object contains all of the current browser requests for information, including Cookies, HTML form variables, and so on. This session is an implicit target object. This object in a JSP page is loaded automatically create, and was linked to the request object. ASP and similar objects in the session, the session JSP target for those who wish to complete a through multiple pages of affairs is very useful.
To illustrate session for specific applications, the next simulation, we have used three pages of a multi-page Web applications. The first page (q1.html) only contains a requirement to enter a user name of the HTML form, the code below:
<HTML>
<BODY>
<= POST ACTION FORM METHOD = "q2.jsp">
Please enter your name:
<INPUT TYPE = TEXT NAME = "thename">
<INPUT TYPE = SUBMIT VALUE = "SUBMIT">
</ FORM>
</ BODY>
</ HTML>
The second page is a JSP page (q2.jsp), it adopted the request Object Extraction q1.html thename of the form, it will be stored for the variable name, and then the name of preservation of the session object. Session object is a name / value on the collection, where the name / value pairs in the name of "
Thename ", the name of which is the value of variables. Object in the session because during the session has been effective, and therefore the preservation of variables here on subsequent pages and effective. Q2.jsp Another task is to ask the second question. Below It is the code:
<HTML>
<BODY>
<% @ Page language = "java"%>
<%! String name = "";%>
<%
Name = request.getParameter ( "thename");
Session.putValue ( "thename", name);
%>
Your name is: <% name% =>
<P>
<= POST ACTION FORM METHOD = "q3.jsp">
What you like to eat?
<INPUT TYPE = TEXT NAME = "food">
<P>
<INPUT TYPE = SUBMIT VALUE = "SUBMIT">
</ FORM>
</ BODY>
</ HTML>
The third page is a JSP page (q3.jsp), the main task is to show that quiz results. It thename Object Extraction from the session and show the value of it, to prove that although the value in the first pages input, but the session objects will be retained. Q3.jsp Another task is to extract the second page for user input 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>
</ HTML>
Third, quoted JavaBean JavaBean component is a Java-based software components. JSP Web application in the integrated JavaBean component provides a perfect support. Such support will not only shorten the development time (can be tested and the direct use of trusted components has been to avoid duplication of development), as well as JSP applications can be more flexible. JavaBean components can be used to perform complex computing tasks, or to be responsible for the interaction with the database, etc., as well as data extraction. If we have three JavaBean, each show with news, stock prices, weather conditions functions, including creation of all three functions of Web pages need only three examples of Bean, the use of HTML tables they will be followed by positioning the.
To illustrate the JSP environment JavaBean application, we created a TaxRate known as the Bean. It has two attributes, namely Product (products) and Rate (rate). Two methods were used to set the establishment of the two properties, two methods are used to get from these two attributes. In practical applications, such Bean generally should extract from the database tax value, here we simplified the process, allowing arbitrary rate. Below is the list of Bean code:
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);
)
)
JSP pages in the application to use the Bean <jsp: useBean> marker. Depend on the specific use of the different JSP engine, where Bean configuration, as well as how to configure the method may also be slightly different. This paper will be of the Bean. Class document on the c: jswdk-1.0examplesWEB-INFjsp eans ax directory, where tax is a specialized storage of the Bean catalog. Below is an example of the application of the above-mentioned Bean pages:
<HTML>
<BODY>
<% @ Page language = "java"%>
<Jsp: useBean id = "taxbean" scope = "application" class = "tax.TaxRate" />
<% Taxbean.setProduct ( "A002");
Taxbean.setRate (17);
%>
Use 1: <p>
Products: <% = taxbean.getProduct ()%> <br>
Rate: <% = taxbean.getRate ()%>
<P>
<% Taxbean.setProduct ( "A003");
Taxbean.setRate (3);
%>
<B> use 2: </ b> <p>
Products: <jsp: getProperty name = "taxbean" property = "Product" />
<Br>
Rate: <jsp: getProperty name = "taxbean" property = "Rate" />
</ BODY>
</ HTML>
In "jsp: useBean> marker in the definition of several attributes, which is the id of the JSP pages Bean logo, the definition of the scope Bean attributes the survival time, the class attribute of the Bean class files (from a packet start).
This not only use the JSP pages Bean set and get the set-up and extraction method of attribute values, but also uses the attribute values from Bean second method, that is, using <jsp: getProperty> marker. <Jsp: getProperty> which is the name attribute <jsp: useBean> Bean defined in the id, and its property is the goal of the attributes specified attribute name.
Facts have proved that the development of a Java Servlet is the ideal framework for Web applications. JSP Servlet technology as a basis, and in many respects has been improved. JSP pages look like ordinary HTML pages, but it allows the implementation of the code embedded in this point, it is very similar and ASP technology. Use of cross-platform operation JavaBean components, JSP for separation processing logic and display modes provide an excellent solution. JSP ASP technology will become a powerful competitor. </ Td> </ tr> <tr> <td> </ td> </ tr> </ table> </ td> </ tr> </ table>
↑ Back
Tags: JSP, the core java technology






