22
JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete application that can be specialized to produce custom applications [Johnson]. Like people, software applications are more alike than they are different. They run on the same computers, expect input from the same devices, output to the same displays, and save data to the same hard disks. Developers working on conventional desktop applications are accustomed to toolkits and development environments that leverage the sameness between applications. Application frameworks build on this common ground to provide developers with a reusable structure that can serve as the foundation for their own products. A framework provides developers with a set of backbone components that have the following characteristics: 1.They are known to work well in other applications. 2. They are ready to use with the next project. 3. They can also be used by other teams in the organization. Frameworks are the classic build-versus-buy proposition. If you build it, you will understand it when you are done but how long will it be before you can roll your own? If you buy it, you will have to climb the learning curve and how long is that going to take? There is no right answer here, but most observers would agree that frameworks such as Struts provide a significant return on investment compared to starting from scratch, especially for larger projects. Other types of frameworks: The idea of a framework applies not only to applications but to application componentsas well. Throughout this article, we introduce other types of frameworks that you can use with Struts. These include the Lucene search engine, the Scaffold toolkit, the Struts validator, and the Tiles tag library. Like application frameworks, these tools provide semi-complete versions of a subsystem that can be specialized to provide a custom component. Some frameworks have been linked to a proprietary development environment. This is not the case with Struts or any of the other frameworks shown in this book. You can use any development environment with Struts: Visual Age for Java, JBuilder, Eclipse, Emacs, and Textpad are all popular choices among Struts developers. If you can use it with Java, you can use it with Struts. Enabling technologies: Applications developed with Struts are based on a number of enabling technologies.These components are not specific to Struts and underlie every Java web application. A reason that developers use frameworks like Struts is to hide the nasty

JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

JSP application frameworks

What are application frameworks:

A framework is a reusable, semi-complete application that can be specialized to

produce custom applications [Johnson]. Like people, software applications are more alike than they are different. They run on the same computers, expect input from the same devices, output to the same displays, and save data to the same hard disks. Developers working on conventional desktop applications are accustomed to toolkits and development environments that leverage the sameness between applications. Application frameworks build on this common ground to provide developers with a reusable structure that can serve as the foundation for their own products.

A framework provides developers with a set of backbone components that have the following characteristics:

1.They are known to work well in other applications.

2. They are ready to use with the next project.

3. They can also be used by other teams in the organization.

Frameworks are the classic build-versus-buy proposition. If you build it, you will understand it when you are done—but how long will it be before you can roll your own? If you buy it, you will have to climb the learning curve —and how long is that going to take? There is no right answer here, but most observers would agree that frameworks such as Struts provide a significant return on investment compared to starting from scratch, especially for larger projects.

Other types of frameworks:

The idea of a framework applies not only to applications but to application componentsas well. Throughout this article, we introduce other types of frameworks that you can use with Struts. These include the Lucene search engine, the Scaffold toolkit, the Struts validator, and the Tiles tag library. Like application frameworks, these tools provide semi-complete versions of a subsystem that can be specialized to provide a custom component.

Some frameworks have been linked to a proprietary development environment. This is not the case with Struts or any of the other frameworks shown in this book. You can use any development environment with Struts: Visual Age for Java, JBuilder, Eclipse, Emacs, and Textpad are all popular choices among Struts developers. If you can use it with Java, you can use it with Struts.

Enabling technologies:

Applications developed with Struts are based on a number of enabling technologies.These components are not specific to Struts and underlie every Java web application. A reason that developers use frameworks like Struts is to hide the nasty

Page 2: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

details behind acronyms like HTTP, CGI, and JSP. As a Struts developer, you don ’ t need to be an alphabet soup guru, but a working knowledge of these base technologies can help you devise creative solutions to tricky problems.

Hypertext Transfer Protocol (HTTP):

When mediating talks between nations, diplomats often follow a formal protocol.

Diplomatic protocols are designed to avoid misunderstandings and to keep negotiations from breaking down. In a similar vein, when computers need to talk, they also follow a formal protocol. The protocol defines how data is transmitted and how to decode it once it arrives. Web applications use the Hypertext Transfer Protocol (HTTP) to move data between the browser running on your computer and the application running on the server.

Many server applications communicate using protocols other than HTTP. Some of these maintain an ongoing connection between the computers. The application server knows exactly who is connected at all times and can tell when a connection is dropped. Because they know the state of each connection and the identity of each person using it, these are known as stateful protocols.

By contrast, HTTP is known as a stateless protocol. An HTTP server will accept any request from any client and will always provide some type of response, even if the response is just to say no. Without the overhead of negotiating and retaining a connection, stateless protocols can handle a large volume of requests. This is one reason why the Internet has been able to scale to millions of computers.

Another reason HTTP has become the universal standard is its simplicity. An HTTP request looks like an ordinary text document. This has made it easy for applications to make HTTP requests. You can even send an HTTP request by hand using a standard utility such as Telnet. When the HTTP response comes back, it is also in plain text that developers can read.

The first line in the HTTP request contains the method, followed by the location

of the requested resource and the version of HTTP. Zero or more HTTP request headers follow the initial line. The HTTP headers provide additional information to the server. This can include the browser type and version, acceptable document types, and the browser ’s cookies, just to name a few. Of the seven request methods, GET and POST are by far the most popular.

Once the server has received and serviced the request, it will issue an HTTP response. The first line in the response is called the status line and carries the HTTP protocol version, a numeric status, and a brief description of the status. Following the status line, the server will return a set of HTTP response headers that work in a way similar to the request headers.

As we mentioned, HTTP does not preserve state information between requests.The server logs the request, sends the response, and goes blissfully on to the next request. While simple and efficient, a stateless protocol is problematic for dynamic applications that need to keep track of their users. (Ignorance is not always bliss.

Page 3: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

Cookies and URL rewriting are two common ways to keep track of users between requests. A cookie is a special packet of information on the user ’s computer. URL rewriting stores a special reference in the page address that a Java server can use to track users. Neither approach is seamless, and using either means extra work when developing a web application. On its own, a standard HTTP web server does not traffic in dynamic content. It mainly uses the request to locate a file and then returns that file in the response. The file is typically formatted using Hypertext Markup Language (HTML) [W3C, HTML] that the web browser can format and display. The HTML page often includes hypertext links to other web pages and may display any number of other goodies, such as images and videos. The user clicks a link to make another request, and the process begins a new.

Standard web servers handle static content and images quite well but need a helping hand to provide users with a customized, dynamic response.

DEFINITION:Static content on the Web comes directly from text or data files, like HTML or JPEG files. These files might be changed from time to time, but they are not altered automatically when requested by a web browser. Dynamic content, on the other hand, is generated on the fly, typically in response to an individualized request from a browser.

Common Gateway Interface (CGI):

The first widely used standard for producing dynamic content was the Common Gateway Interface (CGI). CGI uses standard operating system features, such as environment variables and standard input and output, to create a bridge, or gateway, between the web server and other applications on the host machine. The other applications can look at the request sent to them by the web server and create a customized response.

When a web server receives a request that ’s intended for a CGI program, it runs that program and provides the program with information from the incoming request. The CGI program runs and sends its output back to the server. The web server then relays the response to the browser.

CGI defines a set of conventions regarding what information it will pass as environment variables and how it expects standard input and output to be used. Like HTTP, CGI is flexible and easy to implement, and a great number of CGI-aware programs have been written.

The main drawback to CGI is that it must run a new copy of the CGI-aware program for each request. This is a relatively expensive process that can bog down high-volume sites where thousands of requests are serviced per minute. Another drawback is that CGI programs tend to be platform dependent. A CGI program written for one operating system may not run on another.

Java servlets:

Sun’s Java Servlet platform directly addresses the two main drawbacks of CGI programs.First, servlets offer better performance and utilization of resources than conventional CGI programs. Second, the write-once, run-anywhere nature of Java

Page 4: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

means that servlets are portable between operating systems that have a Java Virtual Machine (JVM).

A servlet looks and feels like a miniature web server. It receives a request and renders a response. But, unlike conventional web servers, the servlet application programming interface (API) is specifically designed to help Java developers create dynamic applications.

The servlet itself is simply a Java class that has been compiled into byte code, like any other Java object. The servlet has access to a rich API of HTTP-specific services, but it is still just another Java object running in an application and can leverage all your other Java assets.

To give conventional web servers access to servlets, the servlets are plugged into containers. The servlet container is attached to the web server. Each servlet can declare what URL patterns it would like to handle. When a request matching a registered pattern arrives, the web server passes the request to the container, and the container invokes the servlet.

But unlike CGI programs, a new servlet is not created for each request. Once the container instantiates the servlet, it will just create a new thread for each request. Java threads are much less expensive than the server processes used by CGI programs. Once the servlet has been created, using it for additional requests incurs very little overhead. Servlet developers can use the init() method to hold references to expensive resources, such as database connections or EJB Home Interfaces, so that they can be shared between requests. Acquiring resources like these can take several seconds—which is longer than many surfers are willing to wait.

The other edge of the sword is that, since servlets are multithreaded, servlet developers must take special care to be sure their servlets are thread-safe. To learn more about servlet programming, we recommend Java Servlets by Example, by Alan R. Williamson [Williamson]. The definitive source for Servlet information is the Java Servlet Specification [Sun, JST].

JavaServer Pages:

While Java servlets are a big step up from CGI programs, they are not a panacea. To generate the response, developers are still stuck with using println statements to render the HTML. Code that looks like:

out.println("<P>One line of HTML.</P>");

out.println("<P>Another line of HTML.</P>");

is all too common in servlets that generate the HTTP response. There are libraries that can help you generate HTML, but as applications grow more complex, Java developers end up being cast into the role of HTML page designers.

Meanwhile, given the choice, most project managers prefer to divide development teams into specialized groups. They like HTML designers to be working on the presentation while Java engineers sweat the business logic. Using servlets alone encourages mixing markup with business logic, making it difficult for team members to specialize.

Page 5: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

To solve this problem, Sun turned to the idea of using server pages to combine scripting and templating technologies into a single component. To build Java Server Pages, developers start by creating HTML pages in the same old way, using the same old HTML syntax. To bring dynamic content into the page, the developer can also place JSP scripting elements on the page. Scripting elements are tags that encapsulate logic that is recognized by the JSP. You can easily pick out scripting elements on JSP pages by looking for code that begins with <% and ends with %>.

To be seen as a JSP page, the file just needs to be saved with an extension of .jsp.

When a client requests the JSP page, the container translates the page into a source code file for a Java servlet and compiles the source into a Java class file— just as you would do if you were writing a servlet from scratch. At runtime, the container can also check the last modified date of the JSP file against the class file. If the JSP file has changed since it was last compiled, the container will retranslate and rebuild the page all over again.

Project managers can now assign the presentation layer to HTML developers, who then pass on their work to Java developers to complete the business-logic portion. The important thing to remember is that a JSP page is really just a servlet. Anything you can do with a servlet, you can do with a JSP.

JavaBeans:

JavaBeans are Java classes which conform to a set of design patterns that make them easier to use with development tools and other components.

DEFINITION A JavaBean is a reusable software component written in Java. To qualify as a JavaBean, the class must be concrete and public, and have a noargument constructor. JavaBeans expose internal fields as properties by providing public methods that follow a consistent design pattern. Knowing that the property names follow this pattern, other Java classes are able to use introspection to discover and manipulate JavaBean properties.

The JavaBean design patterns provide access to the bean ’s internal state through two flavors of methods: accessors are used to read a JavaBean ’s state; mutators are used to change a JavaBean ’s state.

Mutators are always prefixed with lowercase token set followed by the property name. The first character in the property name must be uppercase. The return value is always void—mutators only change property values; they do not retrieve them. The mutator for a simple property takes only one parameter in its signature, which can be of any type. Mutators are often nicknamed setters after their prefix. The mutator method signature for a weight property of the type Double would be:

public void setWeight(Double weight)

A similar design pattern is used to create the accessor method signature. Accessor methods are always prefixed with the lowercase token get, followed by the property name. The first character in the property name must be uppercase. The return value will match the method parameter in the corresponding mutator. Accessors for simple properties cannot accept parameters in their method signature. Not surprisingly,

Page 6: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

accessors are often called getters.

The accessor method signature for our weight property is:

public Double getWeight()

If the accessor returns a logical value, there is a variant pattern. Instead of using the lowercase token get, a logical property can use the prefix is, followed by the property name. The first character in the property name must be uppercase. The return value will always be a logical value—either boolean or Boolean. Logical accessors cannot accept parameters in their method signature.

The boolean accessor method signature for an on property would be

public boolean isOn()

The canonical method signatures play an important role when working with Java- Beans. Other components are able to use the Java Reflection API to discover a JavaBean’s properties by looking for methods prefixed by set, is, or get. If a component finds such a signature on a JavaBean, it knows that the method can be used to access or change the bean ’s properties.

Sun introduced JavaBeans to work with GUI components, but they are now used with every aspect of Java development, including web applications. When Sun engineers developed the JSP tag extension classes, they designed them to work with JavaBeans. The dynamic data for a page can be passed as a JavaBean, and the JSP tag can then use the bean ’s properties to customize the output.

For more on JavaBeans, we highly recommend The Awesome Power of JavaBeans, by Lawrence H. Rodrigues [Rodrigues]. The definitive source for JavaBean information is the JavaBean Specification [Sun, JBS].

Model 2:

The 0.92 release of the Servlet/JSP Specification described Model 2 as an architecture that uses servlets and JSP pages together in the same application. The term Model 2 disappeared from later releases, but it remains in popular use among Java web developers.

Under Model 2, servlets handle the data access and navigational flow, while JSP pages handle the presentation. Model 2 lets Java engineers and HTML developers each work on their own part of the application. A change in one part of a Model 2 application does not mandate a change to another part of the application. HTML developers can often change the look and feel of an application without changing how the back-office servlets work.

The Struts framework is based on the Model 2 architecture. It provides a controller servlet to handle the navigational flow and special classes to help with the data access. A substantial custom tag library is bundled with the framework to make Struts easy to use with JSP pages.

Summary:

In this article, we introduced Struts as an application framework. We examined the technology behind HTTP, the Common Gateway Interface, Java servlets, JSPs,

Page 7: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

and JavaBeans. We also looked at the Model 2 application architecture to see how it is used to combine servlets and JSPs in the same application.

Now that you have had a taste of what it is like to develop a web application with Struts, in chapter 2 we dig deeper into the theory and practice behind the Struts architecture.

JSP (JavaServer Pages) is initiated by Sun Microsystems, Inc., with many

companies to participate in the establishment of a dynamic web page technical

standards. JSP technology somewhat similar to ASP technology, it is in the traditional

HTML web page document (*. htm, *. html) to insert the Java programming

paragraph (Scriptlet) and JSP tag (tag), thus JSP documents (*. jsp).

Using JSP development of the Web application is cross-platform that can run on

Linux, is also available for other operating systems.

JSP technology to use the Java programming language prepared by the category

of XML tags and scriptlets, to produce dynamic pages package processing logic. Page

also visit by tags and scriptlets exist in the services side of the resources of logic.

JSP page logic and web page design and display separation, support reusable

component-based design, Web-based application development is rapid and easy.

Web server in the face of visits JSP page request, the first implementation of the

procedures of, and then together with the results of the implementation of JSP

documents in HTML code with the return to the customer. Insert the Java

programming operation of the database can be re-oriented websites, in order to

achieve the establishment of dynamic pages needed to function.

JSP and Java Servlet, is in the implementation of the server, usually returned to

the client is an HTML text, as long as the client browser will be able to visit.

JSP 1.0 specification of the final version is launched in September 1999,

December has introduced 1.1 specifications. At present relatively new is JSP1.2

norms, JSP2.0 norms of the draft has also been introduced.

JSP pages from HTML code and Java code embedded in one of the components.

The server was in the pages of client requests after the Java code and then will

generate the HTML pages to return to the client browser. Java Servlet JSP is the

technical foundation and large-scale Web application development needs of Java

Servlet and JSP support to complete. JSP with the Java technology easy to use, fully

object-oriented, and a platform-independent and secure, mainly for all the

characteristics of the Internet.

Page 8: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

JavaScript, which is completely distinct from the Java programming language, is

normally used to dynamically generate HTML on the client, building parts of the Web

page as the browser loads the document. This is a useful capability and does not

normally overlap with the capabilities of JSP (which runs only on the server). JSP

pages still include SCRIPT tags for JavaScript, just as normal HTML pages do. In fact,

JSP can even be used to dynamically generate the JavaScript that will be sent to the

client. So, JavaScript is not a competing technology; it is a complementary one.It is

also possible to use JavaScript on the server, most notably on Sun ONE (formerly

iPlanet), IIS, and BroadVision servers. However, Java is more powerful, flexible,

reliable, and portable.

JSP (a recursive acronym for "JSP: Hypertext Preprocessor") is a free,

open-source, HTML-embedded scripting language that is somewhat similar to both

ASP and JSP. One advantage of JSP is that the dynamic part is written in Java, which

already has an extensive API for networking, database access, distributed objects, and

the like, whereas PHP requires learning an entirely new, less widely used language. A

second advantage is that JSP is much more widely supported by tool and server

vendors than is JSP.Versus Pure Servlets.

JSP doesn't provide any capabilities that couldn't, in principle, be

accomplished with servlets. In fact, JSP documents are automatically translated into

servlets behind the scenes. But it is more convenient to write (and to modify!) regular

HTML than to use a zillion println statements to generate the HTML. Plus, by

separating the presentation from the content, you can put different people on different

tasks: your Web page design experts can build the HTML by using familiar tools and

either leave places for your servlet programmers to insert the dynamic content or

invoke the dynamic content indirectly by means of XML tags.

Does this mean that you can just learn JSP and forget about servlets? Absolutely

not! JSP developers need to know servlets for four reasons:

a. JSP pages get translated into servlets. You can't understand how JSP works

without understanding servlets.

b. JSP consists of static HTML, special-purpose JSP tags, and Java code. What

kind of Java code? Servlet code! You can't write that code if you don't understand

servlet programming.

c. Some tasks are better accomplished by servlets than by JSP. JSP is good at

generating pages that consist of large sections of fairly well structured HTML or other

Page 9: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

character data. Servlets are better for generating binary data, building pages with

highly variable structure, and performing tasks (such as redirection) that involve little

or no output.

d. Some tasks are better accomplished by a combination of servlets and JSP

than by either servlets or JSP alone.

JSP technology strength

(1) time to prepare, run everywhere. At this point Java better than PHP, in

addition to systems, the code not to make any changes.

(2) the multi-platform support. Basically on all platforms of any development

environment, in any environment for deployment in any environment in the expansion.

Compared ASP / PHP limitations are obvious.

(3) a strong scalability. From only a small Jar documents can run Servlet JSP,

to the multiple servers clustering and load balancing, to multiple Application for

transaction processing, information processing, a server to numerous servers, Java

shows a tremendous Vitality.

(4) diversification and powerful development tools support. This is similar to

the ASP, Java already have many very good development tools, and many can be free,

and many of them have been able to run on a variety of platforms under.

JSP technology vulnerable:

(1) and the same ASP, Java is the advantage of some of its fatal problem. It is

precisely because in order to cross-platform functionality, in order to extreme

stretching capacity, greatly increasing the complexity of the product.

(2) Java's speed is class to complete the permanent memory, so in some cases

by the use of memory compared to the number of users is indeed a "minimum cost

performance." On the other hand, it also needs disk space to store a series of. Java

documents and. Class, as well as the corresponding versions of documents.

Know servlets for four reasons:

1. JSP pages get translated into servlets. You can't understand how JSP works

without understanding servlets.

2. JSP consists of static HTML, special-purpose JSP tags, and Java code. What

kind of Java code? Servlet code! You can't write that code if you don't understand

servlet programming.

3. Some tasks are better accomplished by servlets than by JSP. JSP is good at

generating pages that consist of large sections of fairly well structured HTML or other

Page 10: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

character data. Servlets are better for generating binary data, building pages with

highly variable structure, and performing tasks (such as redirection) that involve little

or no output.

4. Some tasks are better accomplished by a combination of servlets and JSP

than by either servlets or JSP alone.

Versus JavaScript

JavaScript, which is completely distinct from the Java programming language, is

normally used to dynamically generate HTML on the client, building parts of the Web

page as the browser loads the document. This is a useful capability and does not

normally overlap with the capabilities of JSP (which runs only on the server). JSP

pages still include SCRIPT tags for JavaScript, just as normal HTML pages do. In fact,

JSP can even be used to dynamically generate the JavaScript that will be sent to the

client. So, JavaScript is not a competing technology; it is a complementary one.

JSP is by no means perfect. Many people have pointed out features that could be

improved. This is a good thing, and one of the advantages of JSP is that the

specification is controlled by a community that draws from many different companies.

So, the technology can incorporate improvements in successive releases.

However, some groups have developed alternative Java-based technologies to try

to address these deficiencies. This, in our judgment, is a mistake. Using a third-party

tool like Apache Struts that augments JSP and servlet technology is a good idea when

that tool adds sufficient benefit to compensate for the additional complexity. But

using a nonstandard tool that tries to replace JSP is a bad idea. When choosing a

technology, you need to weigh many factors: standardization, portability, integration,

industry support, and technical features. The arguments for JSP alternatives have

focused almost exclusively on the technical features part. But portability,

standardization, and integration are also very important. For example, the servlet and

JSP specifications define a standard directory structure for Web applications and

provide standard files (.war files) for deploying Web applications. All JSP-compatible

servers must support these standards. Filters can be set up to apply to any number of

servlets or JSP pages, but not to nonstandard resources. The same goes for Web

application security settings.

JSP six built-in objects:

request, response, out, session, application, config, pagecontext, page, exception.

ONE. Request for:

Page 11: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

The object of the package of information submitted by users, by calling the

object corresponding way to access the information package, namely the use of the

target users can access the information.

TWO. Response object:

The customer's request dynamic response to the client sent the data.

THREE. session object

1. What is the session: session object is a built-in objects JSP, it in the first JSP

pages loaded automatically create, complete the conversation of management.

From a customer to open a browser and connect to the server, to close the browser,

leaving the end of this server, known as a conversation. When a customer visits a

server, the server may be a few pages link between repeatedly, repeatedly refresh a

page, the server should be through some kind of way to know this is the same client,

which requires session object.

2. session object ID: When a customer's first visit to a server on the JSP pages,

JSP engines produce a session object, and assigned a String type of ID number, JSP

engine at the same time, the ID number sent to the client, stored in Cookie, this

session objects, and customers on the establishment of a one-to-one relationship.

When a customer to connect to the server of the other pages, customers no longer

allocated to the new session object, until, close your browser, the client-server object

to cancel the session, and the conversation, and customer relationship disappeared.

When a customer re-open the browser to connect to the server, the server for the

customer to create a new session object.

FORE. aplication target

1. What is the application:

Servers have launched after the application object, when a customer to visit the

site between the various pages here, this application objects are the same, until the

server is down. But with the session difference is that all customers of the application

objects are the same, that is, all customers share this built-in application objects.

2. application objects commonly used methods:

(1) public void setAttribute (String key, Object obj): Object specified

parameters will be the object obj added to the application object, and to add the

subject of the designation of a keyword index.

(2) public Object getAttribute (String key): access to application objects

containing keywords for.

Page 12: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

FIVE. out targets

out as a target output flow, used to client output data. out targets for the output

data.

SIX. Cookie

1. What is Cookie:

Cookie is stored in Web server on the user's hard drive section of the text. Cookie

allow a Web site on the user's computer to store information on and then get back to

it.

For example, a Web site may be generated for each visitor a unique ID, and then

to Cookie in the form of documents stored in each user's machine.

If you use IE browser to visit Web, you will see all stored on your hard drive on

the Cookie. They are most often stored in places: c: \ windows \ cookies (in

Window2000 is in the C: \ Documents and Settings \ your user name \ Cookies)

Cookie is "keyword key = value value" to preserve the format of the record.

2. Targets the creation of a Cookie, Cookie object called the constructor can

create a Cookie. Cookie object constructor has two string parameters: Cookie Cookie

name and value.

Cookie c = new Cookie ( "username", "john");

3. If the JSP in the package good Cookie object to send to the client, the use of

the response addCookie () method.

Format: response.addCookie (c)

4. Save to read the client's Cookie, the use of the object request getCookies ()

method will be implemented in all client came to an array of Cookie objects in the

form of order, to meet the need to remove the Cookie object, it is necessary to

compare an array cycle Each target keywords.

Page 13: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

JSP 应用程序框架什么是应用程序框架 :

一个框架是一个可重用的 ,已可以专门的应用程序生成自定义应用程序 [约翰逊 ]。就像人一样 ,软件应用程序比他们都是不同的。 他们在同一计算机上运行 ,预计来自相同的输入设备 ,输出相同的显示 ,并将数据保存到硬盘。 在传统的桌面应用程序开发人员习惯于工具包和开发环境 ,利用应用程序之间的一致性。应用程序框架建立在这个共同点为开发人员提供一个可重用的结构 ,可以作为自己的产品的基础。

一个框架为开发人员提供了一组骨干组件有以下特点 :

1。他们知道在其他应用程序中工作得很好。

2。他们已经准备好使用的下一个项目。

3所示。也可以由其他团队在组织中使用。

框架的经典 build-versus-buy 命题。如果你建立它 ,你就会明白 ,当你做 ,但会是多久之前 ,你可以自己滚 ?如果你购买它 ,你将不得不爬上学习曲线 ,是需要多长时间 ?这里没有正确答案 ,但大多数观察家都同意 ,如 Struts 框架提供了

一个重要的投资回报相比 ,从零开始 ,特别是对于更大的项目。

其他类型的框架 :

框架不仅适用于应用程序的想法但是应用程序 componentsas好。在本文中 ,我们将介绍其他类型的框架 ,您可以使用 Struts 。这些包括 Lucene搜索引擎 ,脚手架工具包 ,Struts 验证器 ,瓷砖标记库。 像应用程序框架 ,这些工具提供已版本的一个子系统 ,可以专门提供一个自定义组件。

有些框架与专有开发环境。情况不是这样使用 Struts 或任何其他框架的这

本 书 所 示 。 您 可 以 使 用 任 何 与 Struts 开 发 环 境 :Java 、视 觉 年 龄JBuilder,Eclipse,Emacs,Textpad Struts 开发人员都是受欢迎的选择。如果你

可以用它与 Java,您可以使用 Struts 。

实现技术 :

使用 Struts 开发的应用程序是基于一系列的支持技术。这些组件并不是特

定于 Struts 是每个 Java web 应用程序的基础。这样的原因 ,开发人员使用框架Struts 是缩略词背后隐藏的细节如 HTTP、CGI和 JSP。作为一个 Struts 开发人

员 ,你不需要一个字母汤大师 ,但是这些基本的工作知识技术可以帮助你设计出创造性的解决棘手的问题。

超文本传输协议 (HTTP):

当协调国家之间的会谈 ,外交官往往遵循一个正式的协议。

Page 14: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

外交协议是为了避免误解和防止谈判打破。同样 ,当电脑需要说话的时候 ,他们也遵循一个正式的协议。协议定义了数据是如何传播的 ,以及如何解码一旦它到达。Web应用程序使用超文本传输协议 (HTTP)之间移动数据在您的计算机上运行的浏览器和应用程序在服务器上运行。

许多服务器应用程序使用 HTTP协议以外的交流。其中的一些维护一个持续的计算机之间的连接。应用程序服务器知道谁是连接 ,连接时可以告诉下降。因为他们知道每个连接的状态 ,每个人使用它的身份 ,这些被称为状态协议。

相比之下 ,HTTP是称为一个无状态的协议。 HTTP服务器将接受任何请求从客户端和总是提供一些类型的反应 ,即使只是说“不”的响应。没有谈判的开销和保持一个连接 ,无状态的协议可以处理大量的请求。这就是为什么互联网能够扩展到数以百万计的电脑。

HTTP已经成为通用标准的另一个原因是它的简单。 一个 HTTP请求看起来像一个普通的文本文档。这使得应用程序更容易使 HTTP请求。你甚至可以手工发送一个 HTTP请求使用一个标准的实用程序 ,如 Telnet 。当 HTTP响应返回时 ,它也在开发者可以阅读纯文本。

第一行的 HTTP请求包含方法 ,其次是位置所请求的资源和 HTTP的版本。零个或多个 HTTP请求头遵循最初的线。 HTTP头信息提供额外的信息到服务器。这可以包括浏览器类型和版本 ,可接受的文档类型 ,和浏览器的 cookie, 仅举几例。的七个请求方法 ,GET和 POST是目前最受欢迎的。

一旦服务器已经收到和服务请求时 ,它将发出一个 HTTP响应。第一行的响应称为状态行和 HTTP协议版本 ,一个数字状态和状态的简要描述。状态行后 ,服务器将返回一组 HTTP响应头工作在某种程度上类似于请求头。

正如我们提到的 ,HTTP 不维护请求之间的状态信息。服务器日志请求 ,发送响应 ,并会幸福到下一个请求。 虽然简单而高效 ,无状态的协议是有问题的动态应用程序需要跟踪他们的用户。

饼干和 URL重写是两种常见的方法来跟踪用户请求之间。 饼干是一种特殊的

数据包的信息在用户的电脑上。 URL重写特别引用存储在 Java 服务器的页面地址可以使用跟踪用户。两种方法都是无缝的 ,使用意味着额外的工作在开发一个web应用程序。就其本身而言 ,一个标准的 HTTP web服务器不会交通动态内容。它主要使用请求来定位一个文件 ,然后在响应中返回该文件。文件通常使用超文本标记语言 (HTML)格式 (W3C HTML),web浏览器可以格式和显示。 HTML页面通常包括超文本链接到其他网页 ,可能显示任意数量的其他产品 ,如图像和视频。用户点击一个链接做出另一个请求 ,并开始一个新的过程。

标准的 web服务器处理静态内容和图片很好但需要援助之手为用户提供一个定制的 ,动态响应。

Page 15: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

定义 :静态内容在网络上直接来自文本或数据文件 ,比如 HTML或 JPEG文件。这些文件可能会不时改变 ,但他们并不改变时自动请求一个 web浏览器。动态内容 ,另一方面 ,是动态生成的 ,一般在回应一个个性化的请求从浏览器。

通用网关接口 (CGI):

第一个被广泛使用的标准生产动态内容是通用网关接口 (CGI)。CGI 使用标准的操作系统特性 ,比如环境变量和标准输入和输出 ,建立一个桥 ,或网关之间的web服务器和主机上的其他应用程序。 其他应用程序可以查看请求发送到他们的

web服务器 ,并创建一个定制的响应。

当 web服务器接收请求的 CGI程序 ,它运行程序和提供程序从传入请求的信息。CGI程序运行并将它的输出发送回服务器。然后 web服务器传递到浏览器的响应。

CGI 定义了一组约定关于什么信息会通过环境变量以及它如何预计使用标准输入和输出。像 HTTP、CGI是灵活和容易实现 ,开发了大量 CGI-aware 程序。

CGI的主要缺点是 ,它必须运行一个新的 CGI-aware程序为每个请求的副本。这是一个相对昂贵的过程 ,可以停顿高容量网站成千上万的请求服务每分钟。 CGI程序的另一个缺点是往往是依赖于平台。 CGI程序编写一个操作系统不能运行在另一个地方。

Java servlet:

Sun的 Java Servlet 平台直接地址 CGI程序的两个主要缺点。 首先 ,servlet提供更好的性能比传统的 CGI程序和资源的利用率。第二 ,写一次 ,可以到处 Java的本质意味着 servlet 之间是可移植的操作系统有一个 Java 虚拟机 (JVM)。

一个 servlet 看起来就像一个小型的 web服务器。它接收请求并呈现响应。但是 ,与传统的 web服务器 ,servlet 应用程序编程接口 (API) 是专门设计用于帮助 Java 开发人员创建动态的应用程序。

servlet 本身仅仅是一个 Java 类,它被编译成字节码 ,像任何其他 Java 对象。 servlet 访问 HTTP-specific 服务的丰富的 API,但它仍只是一个 Java 对象中运行应用程序 ,可以利用你所有的其他 Java 资产。

给传统的 web服务器访问 servlet,servlet 插入容器。 servlet 容器连接到

web服务器。每个 servlet 可以声明它要处理什么 URL模式。当一个请求匹配的注册模式到达时 ,web服务器将请求传递到容器 ,容器调用 servlet 。

但与 CGI程序 ,为每个请求创建一个新的 servlet 不是。 servlet 容器实例

化之后 ,它会为每个请求创建一个新线程。 Java 线程更便宜比 CGI程序所使用的服务器进程。一旦创建了 servlet, 使用额外的请求开销非常少。 Servlet 开发人员可以使用 init() 方法将引用昂贵的资源 ,如数据库连接或 EJB本地接口 ,以便他们可以请求之间共享。获取这些资源可以几秒也长于许多冲浪者愿意等待。

Page 16: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

另一刀 ,因为 servlet 是多线程 ,servlet 开发人员必须要特别注意确保他们

的 servlet 线程安全的。了解更多关于 servlet 编程 , 我们建议由示例 Java servlet, 阿兰?r . Williamson[ 威廉森 ] 。Servlet 的权威来源信息是 Java Servlet 规范 (太阳 ,JST)。

JavaServer Pages:

虽然 Java servlet 是一大步从 CGI程序 ,它们不是灵丹妙药。生成响应 ,开发商仍坚持使用 println 语句呈现 HTML。代码看起来像 :

出去了。 println( “< P >一行 HTML。< / P > \");

出去了。 println( “< P >另一行 HTML。< / P > \");

太常见的 servlet 生成 HTTP响应。有库可以帮助您生成 HTML,但随着应用程序变得越来越复杂 ,Java 开发人员最终被转换为 HTML页面设计师的角色。

与此同时 ,考虑到这个选择 ,大多数项目经理倾向于开发团队划分为专业团体。他们喜欢 HTML设计师致力于表示尽管 Java 工程师汗水业务逻辑。 单独使用servlet 鼓励混合标记与业务逻辑 ,使团队成员很难专门化。

为了解决这个问题 ,太阳转向使用服务器页面的想法将脚本和模板技术结合到一个组件。构建 Java 服务器页面 ,开发人员首先创建 HTML页面相同的老方法 ,使用相同的旧 HTML语法。将动态内容到页面中 ,开发人员还可以将 JSP脚本元素在页面上。脚本元素标签封装逻辑所公认的 JSP。您可以轻松地挑选出脚本元素在 JSP页面上寻找代码始于 < %和结束% >。

被视为一个 JSP页面 ,文件只需要保存 JSP的延伸。

当客户端请求的 JSP页面时 ,容器将页面转换为源代码文件的 Java servlet和将源代码编译成 Java 类文件 ,你会怎么做如果你从头编写一个 servlet 。在运行时 ,容器还可以检查 JSP文件的最后修改日期类文件。如果 JSP文件改变了自去年编译 ,容器将重译和重建页面。

项目经理可以将表示层分配给 HTML开发人员 ,然后将他们的工作给 Java 开发人员完成业务逻辑部分。需要记住的一件重要事情是 ,JSP 页面就是一个servlet 。任何你可以做一个 servlet, 您可以与一个 JSP。

javabean:

javabean 是 Java类,符合一组设计模式 ,使他们更容易使用和开发工具和其他组件。

定义一个 JavaBean是用 Java 编写的一个可重用的软件组件。 有资格作为一

个 JavaBean,类必须是具体的和公共 ,noargument 构造函数。 javabean 暴露内部字段作为属性通过提供公共方法 ,遵循一个一致的设计模式。知道属性的名称遵循这种模式 ,其他 Java 类可以使用内省发现和操纵 JavaBean属性。

Page 17: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

JavaBean设计模式提供访问 bean 的内部状态通过两种方法 :访问器是用于读取一个 JavaBean的状态 ;调整器是用来改变一个对象的状态。

调整器总是以小写的令牌设置属性名紧随其后。 属性名的第一个字符必须是

大写。返回值总是 void-mutators 只改变属性值 ;他们不检索。赋值函数为一个简单的属性只需要一个参数在其签名 ,可以是任何类型的。后调整器通常是绰号setter 前缀。重量的 mutator 方法签名属性的类型双重将 :

公共空间 setWeight( 双重量 )

类似的设计模式是用于创建访问器方法签名。 访问器方法总是以小写的令牌 ,其次是属性名。属性名的第一个字符必须是大写。 返回值将在相应的增变基因匹

配方法参数。访问器不能接受简单属性参数的方法签名。不足为奇的是 ,访问器通常被称为 getter 。

体重属性的访问器方法签名 :

公共双 getWeight()

如果访问器返回一个逻辑值 ,有一个变异模式。而不是使用小写的令牌 ,一个逻辑属性可以使用前缀 ,其次是属性名。属性名的第一个字符必须是大写。返回值将永远是一个合乎逻辑的价值 ,布尔或布尔。逻辑访问器不能接受参数的方法签名。

房地产的布尔访问器方法签名

公共布尔关 ()

规范化方法签名发挥重要作用在处理 Java bean。其他组件能够使用 Java Reflection API 来发现一个 JavaBean属性通过寻找方法前缀的集合 ,或得到。如果一个组件发现这样一个 JavaBean 上签名 ,它知道方法可以用来访问或改变bean的属性。

太阳介绍 javabean 使用 GUI组件 ,但是现在他们使用 Java开发的方方面面 ,包括 web应用程序。当太阳工程师开发了 JSP 标记扩展类 ,他们设计他们使用javabean。页面的动态数据可以作为一个 JavaBean,传递和 JSP 标记可以使用bean的属性来定制输出。

更多 javabean, 我们强烈推荐 javabean 的可怕的力量 ,劳伦斯 h·罗德里格斯 (Rodrigues) 。JavaBean信息的来源是 JavaBean规范 [太阳 ,JBS] 。

模型 2:

Servlet / JSP 规范的 0.92 版形容 Model 2 架构 ,使用 Servlet 和 JSP页面在同一个应用程序。 术语模型 2消失后释放 ,但仍在流行中使用 Java web开发人员。

在模型 2中 ,servlet 处理数据访问和导航流 ,而 JSP页面处理报告。模型 2让 Java 工程师和 HTML开发人员每个工作在自己的应用程序的一部分。 改变一个

Page 18: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

模型 2 的一部分应用程序不要求改变到另一个应用程序的一部分。 HTML开发人员可以经常改变应用程序的外观和感觉 ,而不更改后台 servlet 如何工作。

Struts 框架是基于 Model 2 架构。它提供了一个控制器 servlet 处理导航

流和特殊的帮助与数据访问类。 大量定制标记库与 Struts 框架容易捆绑使用 JSP页面。

简介 :

在本文中 ,我们介绍了 Struts 应用程序框架。我们检查 HTTP背后的技术 ,公共网关接口 ,Java servlet 、jsp 和 javabean。我们还研究了模型 2应用程序架构 ,看看它是用来结合在同一个应用程序 servlet 和 jsp 。

现在您已经有一个味道是什么样的开发一个 web应用程序使用 Struts, 在第2章中 ,我们深入挖掘 Struts 架构背后的理论和实践。

JSP(JavaServer Pages)是由 Sun Microsystems 公司倡导、许多公司参与一

起建立的一种动态网页技术标准。 JSP技术有点类似 ASP技术,它是在传统的网

页 HTML文件 (*.htm,*.html) 中插入 Java 程序段 (Scriptlet) 和 JSP标记 (tag) ,

从而形成 JSP文件 (*.jsp) 。

用 JSP开发的 Web应用是跨平台的, 即能在 Linux 下运行,也能在其他操作

系统上运行。

JSP技术使用 Java编程语言编写类 XML的 tags 和 scriptlets ,来封装产生

动态网页的处理逻辑。 网页还能通过 tags 和 scriptlets 访问存在于服务端的资

源的应用逻辑。 JSP将网页逻辑与网页设计和显示分离,支持可重用的基于组件

的设计,使基于 Web的应用程序的开发变得迅速和容易。

Web服务器在遇到访问 JSP网页的请求时,首先执行其中的程序段,然后将

执行结果连同 JSP文件中的 HTML代码一起返回给客户。 插入的 Java 程序段可以

操作数据库、重新定向网页等,以实现建立动态网页所需要的功能。

JSP与 Java Servlet 一样,是在服务器端执行的,通常返回该客户端的就

是一个 HTML文本,因此客户端只要有浏览器就能浏览。

JSP的 1.0 规范的最后版本是 1999年 9月推出的,12月又推出了 1.1 规范。

目前较新的是 JSP1.2 规范, JSP2.0规范的征求意见稿也已出台。

JSP页面由 HTML代码和嵌入其中的 Java代码所组成。 服务器在页面被客户

端请求以后对这些 Java 代码进行处理,然后将生成的 HTML页面返回给客户端的

浏览器。 Java Servlet 是 JSP的技术基础,而且大型的 Web应用程序的开发需

要 Java Servlet 和 JSP配合才能完成。 JSP具备了 Java 技术的简单易用,完全

的面向对象,具有平台无关性且安全可靠,主要面向因特网的所有特点。

Page 19: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

许多年前,Marty 受到邀请 ,参加一个有关软件技术的小型 (20 个人 )研讨会 .

做在 Marty 旁边的人是 James Gosling--- Java 编程语言的发明者。隔几个位置 ,

是来自华盛顿一家大型软件公司的高级经理。在讨论过程中 ,研讨会的主席提出

了 Jini 的议题 ,这在当时是一项新的 Java 技术.主席向该经理询问他的想法 .他

继续说 ,他们会持续关注这项技术 ,如果这项技术变得流行起来 ,他们会遵循公司

的“接受并扩充 (embrace and extend) 的策略。此时,Gosling 随意地插话说 “你

的意思其实就是不接受且不扩充 (disgrace and distend) 。”

JSP页面最终会转换成 servler 。因而,从根本上, JSP页面能够执行的任

何任务都可以用 servler 来完成。然而,这种底层的等同性并不意味着 servler

和 JSP页面对于所有的情况都等同适用。 问题不在于技术的能力, 而是二者在便

利性、生产率和可维护性上的不同。毕竟,在特定平台上能够用 Java 编程语言

完成的事情,同样可以用汇编语言来完成,但是选择哪种语言依旧十分重要。

和单独使用 servler 相比, JSP提供下述好处:

a. JSP中 HTML的编写与维护更为简单。 JSP中可以使用常规的 HTML:没

有额外的反斜杠,没有额外的双引号,也没有暗含的 Java 语法。

b. 能够使用标准的网站开发工具。即使对那些对 JSP一无所知的 HTML工

具,我们也可以使用,因为它们会忽略 JSP标签( JSP tags)。

c. 可以对开发团队进行划分。 Java程序员可以致力于动态代码。 Web开发

人员可以将经理集中在表示层( presentation layer )上。对于大型的项目,这

种划分极为重要。依据开发团队的大小,及项目的复杂程度,可以对静态 HTML

和动态内容进行弱分离 (weaker separation )和强分离(stronger separation )。

在此,这个讨论并不是让您停止使用 servlets ,只使用 JSP。几乎所有的项目都

会同时用到这两种技术。 针对项目中的某些请求, 您可能会在 MVC构架下组合使

用这两项技术。我们总是希望用适当的工具完成相对应的工作,仅仅是 servlet

并不能填满您的工具箱。

JSP技术的强势

(1) 一次编写,到处运行。在这一点上 Java 比 PHP更出色,除了系统之外,

代码不用做任何更改。

(2) 系统的多平台支持。基本上可以在所有平台上的任意环境中开发,在

任意环境中进行系统部署, 在任意环境中扩展。 相比 ASP/PHP的局限性是显而易

见的。

(3) 强大的可伸缩性。 从只有一个小的 Jar 文件就可以运行 Servlet/JSP ,

到由多台服务器进行集群和负载均衡, 到多台 Application 进行事务处理,消息

处理,一台服务器到无数台服务器, Java显示了一个巨大的生命力。

Page 20: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

(4) 多样化和功能强大的开发工具支持。这一点与 ASP很像, Java 已经有

了许多非常优秀的开发工具, 而且许多可以免费得到, 并且其中许多已经可以顺

利的运行于多种平台之下。

JSP技术的弱势:

(1) 与 ASP一样,Java 的一些优势正是它致命的问题所在。正是由于为了

跨平台的功能,为了极度的伸缩能力,所以极大的增加了产品的复杂性。

(2) Java的运行速度是用 class 常驻内存来完成的, 所以它在一些情况下

所使用的内存比起用户数量来说确实是“最低性能价格比”了。从另一方面,它

还需要硬盘空间来储存一系列的 .java 文件和 .class 文件,以及对应的版本文

件。

需要了解 Servlet :

(1) JSP页面会转换成 Servlet 。不了解 Servlet 就无法知道 JSP如何工作。

(2) JSP由静态 HTML、专用的 JSP标签和 Java 代码组成。哪种类型的 Java代

码呢?当然是 Servlet 代码!如果不了解 Servlet 编程,那么就无法编写这种代

码。 (3) 一些任务用 Servlet 完成比用 JSP来完成要好。 JSP擅长生成由大量

组织有序的结构化 HTML或其他字符数据组成的页面。 Servlet 擅长生成二进制

数据,构建结构多样的页面, 以及执行输出很少或者没有输出的任务 (比如重定

向)。 (4) 有些任务更适合于组合使用 Servlet 和 JSP来完成,而非单独使用

Servlet 或 JSP。

与 JavaScript 相比

JavaScript 和 Java编程语言完全是两码事, 前者一般用于在客户端动态生

成 HTML,在浏览器载入文档时构建网页的部分内容。这是一项有用的功能,一

般与 JSP的功能(只在服务器端运行)并不发生重叠。和常规 HTML页面一样,

JSP页面依旧可以包括用于 JavaScript 的 SCRIPT标签。实际上, JSP甚至能够

用来动态生成发送到客户端的 JavaScript 。因此, JavaScript 不是一项竞争技

术,它是一项补充技术。

JavaScript 也可以用在服务器端,最因人注意的是 SUN ONE(以前的

iPlanet )、 IIS 和 BroadVision 服务器。然而, Java 更为强大灵活、可靠且可

移植。

JavaScript 也可以用在服务器端,最因人注意的是 SUN ONE(以前的

iPlanet )、 IIS 和 BroadVision 服务器。然而, Java 更为强大灵活、可靠且可

移植。

JSP六种内置对象:

Page 21: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

Request,response,out,session ,application ,config ,pagecontext ,page,

exception 。

一 . request 对象:

该对象封装了用户提交的信息, 通过调用该对象相应的方法可以获取封装的

信息,即使用该对象可以获取用户提交信息。

二 . response 对象:

对客户的请求做出动态的响应,向客户端发送数据。

三 . session 对象

1. 什么是 session :session 对象是一个 JSP内置对象,它在第一个 JSP

页面被装载时自动创建,完成会话期管理。

从一个客户打开浏览器并连接到服务器开始, 到客户关闭浏览器离开这个服

务器结束,被称为一个会话。 当一个客户访问一个服务器时, 可能会在这个服务

器的几个页面之间反复连接, 反复刷新一个页面, 服务器应当通过某种办法知道

这是同一个客户,这就需要 session 对象。

2. session 对象的 ID:当一个客户首次访问服务器上的一个 JSP页面时,

JSP引擎产生一个 session 对象,同时分配一个 String 类型的 ID 号, JSP引擎

同时将这个 ID 号发送到客户端, 存放在 Cookie 中,这样 session 对象和客户之

间就建立了一一对应的关系。 当客户再访问连接该服务器的其他页面时, 不再分

配给客户新的 session 对象,直到客户关闭浏览器后,服务器端该客户的 session

对象才取消,并且和客户的会话对应关系消失。 当客户重新打开浏览器再连接到

该服务器时,服务器为该客户再创建一个新的 session 对象。

四 . aplication 对象

1. 什么是 application:

服务器启动后就产生了这个 application 对象,当客户再所访问的网站的各

个页面之间浏览时, 这个 application 对象都是同一个, 直到服务器关闭。 但是

与 session 不同的是,所有客户的 application 对象都是同一个, 即所有客户共

享这个内置的 application 对象。

2. application 对象常用方法 :

(1) public void setAttribute(String key,Object obj) :将参数 Object

指定的对象 obj 添加到 application 对象中,并为添加的对象指定一个索引关键

字。

(2) public Object getAttribute(String key): 获取 application 对象

中含有关键字的对象。

五 . out 对象

Page 22: JSP application frameworksstatic.tongtianta.site/paper_pdf/b012b4cc-7097-11e... · JSP application frameworks What are application frameworks: A framework is a reusable, semi-complete

out 对象是一个输出流,用来向客户端输出数据。 out 对象用于各种数据的

输出。

六 . Cookie

1. 什么是 Cookie:

Cookie 是 Web服务器保存在用户硬盘上的一段文本。 Cookie 允许一个 Web

站点在用户的电脑上保存信息并且随后再取回它。

举例来说,一个 Web站点可能会为每一个访问者产生一个唯一的 ID,然后

以 Cookie 文件的形式保存在每个用户的机器上。

如果您使用 IE 浏览器访问 Web,您会看到所有保存在您的硬盘上的 Cookie。

它们最常存放的地方是: c:\windows\cookies( 在 Window2000中则是

您的用户名 \Cookies )Cookie 是以“关键字 key=

值 value“的格式来保存纪录的 .

2. 创建一个 Cookie 对象,调用 Cookie 对象的构造函数可以创建 Cookie。

Cookie 对象的构造函数有两个字符串参数: Cookie 名字和 Cookie 值。

Cookie c=new Cookie( “username”,”john”);

3. JSP中如果要将封装好的 Cookie 对象传送到客户端, 使用 response 的

addCookie() 方法。

格式: response.addCookie(c)

4. 读取保存到客户端的 Cookie, 使用 request 对象的 getCookies() 方法,

执行时将所有客户端传来的 Cookie 对象以数组的形式排列,如果要取出符合需

要的 Cookie 对象,就需要循环比较数组内每个对象的关键字。