jsp技术---外文文献
- 格式:doc
- 大小:101.50 KB
- 文档页数:9
外文文献JSP Technology and ServletsJSP profileJSP (JavaServer Pages) is initiated by Sun Microsystems, Inc., with many comp anies to participate in the establishment of a dynamic web page technical standar ds. JSP technology somewhat similar to ASP technology, it is in the traditional HT ML web page document (*. htm, *. html) to insert the Java programming paragrap h (Scriptlet) and JSP tag (tag), thus JSP documents (*. jsp). Using JSP development of the Web application is cross that can run on Linux, is also available for other op erating systems.JSP technology to use the Java programming language prepared by the categ ory 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 l ogic. 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 t he 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 program ming operation of the database can be reoriented websites, in order to achieve th e establishment of dynamic pages needed to function.JSP and Java Servlet, is in th e implementation of the server, usually returned to the client is an HTML text, as l ong as the client browser will be able to visit.JSP 1.0 specification of the final version is launched in September 1999, Dece mber 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 an d Java code embedded in one of the components.JS script language synopsisJS is javascrip, Javascript is one kind the script language which comes by the Netscape LiveScript development, the main purpose is to solve the server termina l language, for instance Perl, carry-over speed question. At that time served the en d to need to carry on the confirmation to the data, because the network speed wa s quite slow, only then 28.8kbps, the confirmation step waste's time were too many. Therefore Netscape browser Navigator has joined Javascript, has provided the d ata confirmation basic function.The JavaScript official name is “ECMAScript”. This standard by ECMA organiza tion development and maintenance. ECMA262 is the official JavaScript standard. T his standard based on JavaScript (Netscape) and JScript (Microsoft). Netscape (Nav igator 2.0) Brendan Eich has invented this language, started from 1996, already ap peared in all Netscape and in the Microsoft browser. The ECMA262 development began in 1996, in 1997 July, the ECMA general meeting has accepted its first editio n.The script uses one specific descriptive language, rests on certain form compi lation to be possible the execution document, is also called as great or the batch r un document. The script usually may transfer temporarily by the application proce dure and carry out. Each kind of script present widely is applied in the homepage design, because the script not only may reduce the homepage the scale and raises the homepage browsing speed, moreover may enrich the homepage performanc e, like animation, sound and so on. Cites a most common example, when we click i n the homepage the Email address can transfer Outlook Express or the Foxmail thi s kind of mail software automatically, is realizes through the script function. Also b ecause of script these characteristics, the human who harbors ulterior motives by some are often using. For example joins some destruction computer system's orde r in the script, like this works as the user browsing homepage, once transfers this k ind of script, will then cause the user the system to come under the attack. Theref ore the user should act according to visits homepage the trust degree selective se curity rank, specially regarding these itself content on the illegal homepage, do no t permit the use script easily. Through “the safe establishment” the dialog box, the choice “the script” under option each kind of establishment may with ease realize to script being forbid and begins using.JSP and ServletsThe technology of JSP and Servlet is the most important technology which us e Javatechnology to exploit request of server, and it is also the standard which expl oit business application .Java developers prefer to use it for a variety of reasons, one of which is already familiar with the Java language for the development of this technology are easy to learn Java to the other is "a preparation, run everywhere" t o bring the concept of Web applications, To achieve a "one-prepared everywhere r ealized." And more importantly, if followed some of the principles of good design, it can be said of separating and content to create high-quality, reusable, easy to m aintain and modify the application. For example, if the document in HTML embed ded Java code too much (script), will lead the developed application is extremely c omplex, difficult to read, it is not easy reuse, but also for future maintenance and modification will also cause difficulties. In fact, CSDN the JSP / Servlet forum, can often see some questions, the code is very long, can logic is not very clear, a large number of HTML and Java code mixed together. This is the random development o f the defects.Early dynamic pages mainly CGI (Common Gateway Interface, public Gateway Interface) technology, you can use different languages of the CGI programs, such as VB, C / C + + or Delphi, and so on. Though the technology of CGI is developed a nd powerful, because of difficulties in programming, and low efficiency, modify co mplex shortcomings, it is gradually being replaced by the trend. Of all the new tec hnology, JSP / Servlet with more efficient and easy to program, more powerful, m ore secure and has a good portability, they have been many people believe that th e future is the most dynamic site of the future development of technology.Similar to CGI, Servlet support request / response model. When a customer s ubmit a request to the server, the server presented the request Servlet, Servlet res ponsible for handling requests and generate a response, and then gave the server, and then from the server sent to the customer. And the CGI is different, Servlet n ot generate a new process, but with HTTP Server at the same process. It threads t hrough the use of technology, reduce the server costs. Servlet handling of the req uest process is this: When received from the client's request, calling service metho ds, the method of Servlet arrival of the first judgement is what type of request (GE T / POST / HEAD…), then calls the appropriate treatment (DoGet / doPost / doHea d…) and generate a response.Although such a complex, in fact, simply said to Servlet is a Java class. And th e general category of the difference is that this type operating in a Servlet contain er, which can provide session management and targeted lifecycle management. Sothat when you use the Servlet, you can get all the benefits of the Java platform, in cluding the safety of the management, use JDBC access the database and crosspla tform capability. Moreover, Servlet using thread, and can develop more efficient Web applications.JSP technology is a key J2EE technology, it at a higher level of abstraction of a Servlet. It allows conventional static and dynamic HTML content generated by co mbining an HTML page looks like, but as a Servlet to run. There are many commer cial application server support JSP technology, such as BEA WebLogic, IBM WebSp here, JRun, and so on. JSP and Servlet use more than simple. If you have a JSP sup port for Web servers, and a JSP document, you can put it Fangdao any static HTML files can be placed, do not have to compile, do not have to pack, do not have to Cl assPath settings, you can visit as ordinary Web It did visit, the server will automati cally help you to do other work.JSP document looks like an ordinary static HTML document, but inside contai ns a number of Java code. It uses. Jsp the suffix, used to tell the server this docum ent in need of special treatment. When we visit a JSP page, the document will first be translated into a JSP engine Java source files, is actually a Servlet, and compiler , and then, like other Servlet, from Servlet engine to handle. Servlet engine of this type loading, handling requests from customers, and the results returned to the c ustomer.After another visit this page to the customer, as long as the paper there have been no changes, JSP engine has been loaded directly call the Servlet. If you have already been modified, it will be once again the implementation of the above proc ess, translate, compile and load. In fact, this is the so-called "first person to punish ment." Because when the first visit to the implementation of a series of the above process, so will spend some time after such a visit would not.Java servlets offer a powerful API that provides access to all the information a bout the request, the session, and the application. combining JSP with servlets let s you clearly separate the application logic from the presentation of the applicatio n; in other words, it lets you use the most appropriate component type for the rol es of Model, View and Controller.Servlets, Filters, and ListenersA servlet is a Java class that extends a server with functionality for processing a request and producing a response. It's implemented using the classes and interf aces defined by the Servlet API. The API consists of two packages: the javax.servlet package contains classes and interfaces that are protocolindependent, while the j avax.servlet.http package provides HTTP-specific extensions and utility classes.What makes a servlet a servlet is that the class implements an interface nam ed javax.servlet.Servlet, either directly or by extending one of the support classes. This interface defines the methods used by the web container to manage and inte ract with the servlet. A servlet for processing HTTP requests typically extends the javax.servlet.http.HttpServlet class. This class implements the Servlet interface a nd provides additional methods suitable for HTTP processing.Servlet LifecycleThe web container manages all aspects of the servlet's lifecycle. It creates an inst ance of the servlet class when needed, passes requests to the instance for process ing, and eventually removes the instance. For an HttpServlet, the container calls t he following methods at the appropriate times in the servlet lifecycle.Besides the doGet( ) and doPost( ) methods, there are methods corresponding t o the other HTTP methods: doDelete( ), doHead( ), doOptions( ), doPut( ), and doT race( ). Typically you don't implement these methods; the HttpServlet class alread y takes care of HEAD, OPTIONS, and TRACE requests in a way that's suitable for mo st servlets, and the DELETE and PUT HTTP methods are rarely used in a web applic ation.It's important to realize that the container creates only one instance of each s ervlet. This means that the servlet must be thread safe - able to handle multiple re quests at the same time, each executing as a separate thread through the servlet c ode. Without getting lost in details, you satisfy this requirement with regards to in stance variables if you modify the referenced objects only in the init( ) and destroy ( ) methods, and just read them in the request processing methods.Compiling and Installing a ServletTo compile a servlet, you must first ensure that you have the JAR file containi ng all Servlet API classes in the CLASSPATH environment variable. The JAR file is distributed with all web containers. Tomcat includes it in a file called servlet.jar, locat ed in the common/lib directory. On a Windows platform, you include the JAR file i n the CLASSPATH.Reading a RequestOne of the arguments passed to the doGet( ) and doPost( ) methods is an obj ect that implements the HttpServletRequest interface. This interface defines meth ods that provide access to a wealth of information about the request.Generating a ResponseBesides the request object, the container passes an object that implements the HttpServletResponse interface as an argument to the doGet( ) and doPost( ) meth ods. This interface defines methods for getting a writer or stream for the response body. It also defines methods for setting the response status code and headers.Using Filters and ListenersThe servlet specification defines two component types beside servlets: filters an d listeners. These two types were introduced in the Servlet 2.3 specification, so if y ou're using a container that doesn't yet support this version of the specification, I' m afraid you're out of luck.FiltersA filter is a component that can intercept a request targeted for a servlet, JSP page, or static page, as well as the response before it's sent to the client. This mak es it easy to centralize tasks that apply to all requests, such as access control, loggi ng, and charging for the content or the services offered by the application. A filter has full access to the body and headers of the request and response, so it can also perform various transformations. One example is compressing the response body i f the Acceptlanguage request header indicates that the client can handle a comp ressed response.A filter can be applied to either a specific servlet or to all requests matching a URL pattern, such as URLs starting with the same path elements or having the sa me extension.ListenersListeners allow your application to react to certain events. Prior to Servlet 2.3 , you could handle only session attribute binding events (triggered when an object was added or removed from a session). You could do this by letting the object sav ed as a sessionattributimplement the HttpSessionBindingListener interface. With the new interfaces introduced in the 2.3 version of the specification, you can crea te listeners for servlet context and session lifecycle events as well as session activa tion and passivation events (used by a container that temporarily saves session sta te to disk or migrates a session to another server). A newsession attribute event lis tener also makes it possible to deal with attribute binding events for all sessions in one place, instead of placing individual listener objects in each session.The new types of listeners follow the standard Java event model. In other wo rds, a listener is a class that implements one or more of the listener interfaces. Th e interfaces define methods that correspond to events. The listener class is registe red with the container when the application starts, and the container then calls th e event methods at the appropriate times.Initializing Shared Resources Using a ListenerBeans like this typically need to be initialized before they can be used. Forinstance, they may need a reference to a database or some other external da ta source and may create an initial information cache in memory to provide fast ac cess even to the first request for data. You can include code for initialization of the shared resources in the servlet and JSP pages that need them, but a more modula r approach is to place all this code in one place and let the other parts of the appli cation work on the assumption that the resources are already initialized and availa ble. An application lifecycle listener is a perfect tool for this type of resource initial ization. This type of listener implements the javax.servlet.ServletContextListener i nterface, with methods called by the container when the application starts and w hen it shuts down.Picking the Right Component Type for Each TaskThe Project Billboard application introduced is a fairly complex application. H alf the pages are pure controller and business logic processing, it accesses a database to authenticate users, and most pages require access control. In real life, it wo uld likely contain even more pages, for instance, pages for access to a shared docu ment archive, time schedules, and a set of pages for administration. As the applica tion evolves, it may become hard to maintain as a pure JSP application. It's easy to forget to include the access control code in new pages.This is clearly an application that can benefit from using a combination of JSP pages and the component types defined by the servlet specification for the MVC r oles. Let's look at the main requirements and see how we can map them to appro priate component types:Database access should be abstracted, to avoid knowledge of a specific data schema or database engine in more than one part of the application: beans in th e role of Model can be used to accomplish this.The database access beans must be made available to all other parts of the application when it starts: an application lifecycle event listener is the perfect co mponent type for this task.Only authenticated users must be allowed to use the application: a filter can perform access control to satisfy this requirement.Request processing is best done with Java code: a servlet, acting as the Controller fits the bill.It must be easy to change the presentation: this is where JSP shines, acting as the View.Adding servlets, listeners, and filters to the mix minimizes the need for co mplex logic in the JSP pages. Placing all this code in Java classes instead makes it p ossible to use a regular Java compiler and debugger to fix potential problems.Centralized Request Processing Using a ServletWith a servlet as the common entry point for all application requests, you gain control over the page flow of the application. The servlet can decide which ty pe of response to generate depending on the outcome of the requested action, su ch as returning a common error page for all requests that fail, or different respons es depending on the type of client making the request. With the help from some utility classes, it can also provide services such as input validation, I18N preparation s, and in general, encourage a more streamlined approach to request handling.Mapping Application Requests to the ServletThe first requirement for using a Controller servlet is that all requests must pass through it. This can be satisfied in many ways. If you have played around a bi t with servlets previously, you're probably used to invoking a servlet with a URI tha t starts with /myApp/servlet. This is a convention introduced by Suns Java Web Ser ver (JWS), the first product to support servlets before the API was standardized. M ost servlet containers support this convention today, even though it's not formally defined in the servlet specification.。