




已阅读5页,还剩15页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1 毕业设计毕业设计 论文论文 外文资料翻译外文资料翻译 设计 论文 题目 图书管理系统 院 系 计算机工程系 专 业 计算机科学与技术 年 级 学生学号 学生姓名 bianhaiwei 外文出处CHINA USA Business Review 指导教师评语 签名 2 年 月 日 3 Combining JSP and Servlets The technology of JSP and Servlet is the most important technology which use Java technology to exploit request of server and it is also the standard which exploit 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 to bring the concept of Web applications To achieve a one prepared everywhere realized 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 maintain and modify the application For example if the document in HTML embedded Java code too much script will lead the developed application is extremely complex 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 of 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 and powerful because of difficulties in programming and low efficiency modify complex shortcomings 4 it is gradually being replaced by the trend Of all the new technology JSP Servlet with more efficient and easy to program more powerful more secure and has a good portability they have been many people believe that the future is the most dynamic site of the future development of technology Similar to CGI Servlet support request response model When a customer submit a request to the server the server presented the request Servlet Servlet responsible 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 not generate a new process but with HTTP Server at the same process It threads through the use of technology reduce the server costs Servlet handling of the request process is this When received from the client s request calling service methods the method of Servlet arrival of the first judgement is what type of request GET POST HEAD then calls the appropriate treatment DoGet doPost doHead and generate a response Although such a complex in fact simply said to Servlet is a Java class And the general category of the difference is that this type operating in a Servlet container which can provide session management and targeted life cycle management So that when you use the Servlet you can get all the benefits of the Java platform including the safety of the management use JDBC access the database and cross platform 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 5 Servlet It allows conventional static and dynamic HTML content generated by combining an HTML page looks like but as a Servlet to run There are many commercial application server support JSP technology such as BEA WebLogic IBM WebSphere JRun and so on JSP and Servlet use more than simple If you have a JSP support 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 ClassPath settings you can visit as ordinary Web It did visit the server will automatically help you to do other work JSP document looks like an ordinary static HTML document but inside contains a number of Java code It uses Jsp the suffix used to tell the server this document 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 customer 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 process translate compile and load In fact this is the so called first person to punishment 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 about the 6 request the session and the application combining JSP with servlets lets you clearly separate the application logic from the presentation of the application in other words it lets you use the most appropriate component type for the roles of Model View and Controller Servlets Filters and Listeners A 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 interfaces defined by the Servlet API The API consists of two packages the javax servlet package contains classes and interfaces that are protocol independent while the javax servlet http package provides HTTP specific extensions and utility classes What makes a servlet a servlet is that the class implements an interface named 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 interact with the servlet A servlet for processing HTTP requests typically extends the javax servlet http HttpServlet class This class implements the Servlet interface and provides additional methods suitable for HTTP processing Servlet Lifecycle The web container manages all aspects of the servlet s lifecycle It creates an instance of the servlet class when needed passes requests to the instance for processing and eventually removes the instance For an HttpServlet the container calls the following 7 methods at the appropriate times in the servlet lifecycle Besides the doGet and doPost methods there are methods corresponding to the other HTTP methods doDelete doHead doOptions doPut and doTrace Typically you don t implement these methods the HttpServlet class already takes care of HEAD OPTIONS and TRACE requests in a way that s suitable for most servlets and the DELETE and PUT HTTP methods are rarely used in a web application It s important to realize that the container creates only one instance of each servlet This means that the servlet must be thread safe able to handle multiple requests at the same time each executing as a separate thread through the servlet code Without getting lost in details you satisfy this requirement with regards to instance 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 Servlet To compile a servlet you must first ensure that you have the JAR file containing 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 located in the common lib directory On a Windows platform you include the JAR file in the CLASSPATH 8 Reading a Request One of the arguments passed to the doGet and doPost methods is an object that implements the HttpServletRequest interface This interface defines methods that provide access to a wealth of information about the request Generating a Response Besides the request object the container passes an object that implements the HttpServletResponse interface as an argument to the doGet and doPost methods 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 Listeners The servlet specification defines two component types beside servlets filters and listeners These two types were introduced in the Servlet 2 3 specification so if you re using a container that doesn t yet support this version of the specification I m afraid you re out of luck Filters A 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 makes it easy to centralize tasks that apply to all requests such as access control logging and charging for the content or the services offered by the application A filter has full access to the body 9 and headers of the request and response so it can also perform various transformations One example is compressing the response body if the Accept Language request header indicates that the client can handle a compressed 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 same extension Listeners Listeners 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 saved as a sessionattribute using the HttpSession setAttribute method implement the HttpSessionBindingListener interface With the new interfaces introduced in the 2 3 version of the specification you can create listeners for servlet context and session lifecycle events as well as session activation and passivation events used by a container that temporarily saves session state to disk or migrates a session to another server A new session attribute event listener 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 words a listener is a class that implements one or more of the listener interfaces The interfaces define methods that correspond to events The listener class is registered with the container when the application starts and the container then calls the event methods at the 10 appropriate times Initializing Shared Resources Using a Listener Beans like this typically need to be initialized before they can be used For instance they may need a reference to a database or some other external data source and may create an initial information cache in memory to provide fast access 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 modular approach is to place all this code in one place and let the other parts of the application work on the assumption that the resources are already initialized and available An application lifecycle listener is a perfect tool for this type of resource initialization This type of listener implements the javax servlet ServletContextListener interface with methods called by the container when the application starts and when it shuts down Picking the Right Component Type for Each Task The Project Billboard application introduced is a fairly complex application Half 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 would likely contain even more pages for instance pages for access to a shared document archive time schedules and a set of pages for administration As the application 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 11 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 roles Let s look at the main requirements and see how we can map them to appropriate 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 the 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 component 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 complex logic in the JSP pages Placing all this code in Java classes instead makes it possible to use a regular Java compiler and debugger to fix potential problems 12 Centralized Request Processing Using a Servlet With 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 type of response to generate depending on the outcome of the requested action such as returning a common error page for all requests that fail or different responses 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 preparations and in general encourage a more streamlined approach to request handling When you use a servlet as a Controller you must deal with the following basic requirements All requests for processing must be passed to the single Controller servlet The servlet must be able to distinguish requests for different types of processing Here are other features you will want support for even though they may not be requirements for all applications A strategy for extending the application to support new types of processing requests in a flexible manner A mechanism for changing the page flow of the application without modifying code 13 Mapping Application Requests to the Servlet The 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 bit with servlets previously you re probably used to invoking a servlet with a URI that starts with myApp servlet This is a convention introduced by Suns Java Web Server JWS the first product to support servlets before the API was standardized Most servlet containers support this convention today even though it s not formally defined in the servlet specification 14 将将 Servlet 和和 JSP 组合使用组合使用 Servlet 和 JSP 技术是用 Java 开发服务器端应用的主要技术 是开发商务应用表 示端的标准 Java 开发者喜欢使用它有多种原因 其一是对于已经熟悉 Java 语言的 开发者来说这个技术容易学习 其二是 Java 把 一次编写 到处运行 的理念带入 到 Web 应用中 实现了 一次编写 到处实现 而且更为重要的是 如果遵循一 些良好的设计原则的话 就可以把表示和内容相分离 创造出高质量的 可以复用 的 易于维护和修改的应用程序 比方说 在 HTML 文档中如果嵌入过多的 Java 代 码 scriptlet 就会导致开发出来的应用非常复杂 难以阅读 不容易复用 而且 对以后的维护和修改也会造成困难 事实上 在 CSDN 的 JSP Servlet 论坛中 经常 可以看到一些提问 代码很长 可以逻辑却不是很清晰 大量的 HTML 和 Java 代码 混杂在一起 让人看得一头雾水 这就是随意开发的弊端 早期的动态网页主要采用 CGI Common Gateway Interface 公共网关接口 技 术 你可以使用不同的语言编写 CGI 程序 如 VB C C 或 Delphi 等 虽然 CGI 技术发展成熟且功能强大 但由于编程困难 效率低下 修改复杂等缺点 所以有 逐渐被取代的趋势 在所有的新技术中 JSP Servlet 具备更高效 更容易编程 功 能更强 更安全和具有良好的可移植性 因而被许多人认为是未来最有发展前途的 动态网站技术 与 CGI 相似 Servlet 支持请求 响应模型 当一个客户向服务器递交一个请求时 服务器把请求送给 Servlet Servlet 负责处理请求并生成响应 然后送给服务器 再 由服务器发送给客户 与 CGI 不同的是 Servlet 没有生成新的进程 而是与 HTTP Server 处于同一进程中 它通过使用线程技术 减小了服务器的开销 Servlet 处理 请求的过程是这样的 当收到来自客户端的请求后 调用 service 方法 该方法中 Servlet 先判断到来的请求是什么类型的 GET POST HEAD 然后调用相应的处 理方法 doGet doPost doHead 并生成响应 15 别看这么复杂 其实简单说来 Servlet 就是一个 Java 类 与一般类的不同之处是 这个类运行在一个 Servlet 容器内 可以提供 session 管理和对象生命周期管理 因 而当你使用 Servlet 的时候 你可以得到 Java 平台的所有好处 包括安全性管理 使 用 JDBC 访问数据库以及跨平台的能力 而且 Servlet 使用线程 因而可以开发出 效率更高的 Web 应用 JSP 技术是 J2EE 的一个关键技术 它在更高一级的层次上抽象 Servlet 它可以 让常规静态 HTML 与动态产生的内容相结合 看起来像一个 HTML 网页 却作为 Servlet 来运行 现在有许多商业应用服务器支持 JSP 技术 比如 BEA WebLogic IBM WebSphere JRun 等等 使用 JSP 比用 Servlet 更简单 如果你有 一个支持 JSP 的 Web 服务器 并且有一个 JSP 文件 你可以把它放倒任何静态 HTML 文件可以放置的位置 不用编译 不用打包 也不用进行 ClassPath 的设置 就可以像访问普通网页那样访问它 服务器会自动帮你做好其他的工作 JSP 文件看起来就像一个普通静态 HTML 文件 只不过里面包含了一些 Java 代 码 它使用 jsp 的后缀 用来告诉服务器这个文件需要特殊的处理 当我们访问一 个 JSP 页面的时候 这个文件首先会被 JSP 引擎翻译为一个 Java 源文件 其实就是 一个 Servlet 并进行编译 然后像其他 Servlet 一样 由 Servlet 引擎来处理 Servlet 引擎装载这个类 处理来自客户的请求 并把结果返回给客户 以后再有客户访问这个页面的时候 只要该文件没有发生过更改 JSP 引擎就直 接调用已经装载的 Servlet 如果已经做过修改的话 那就会再次执行以上过程 翻 译 编译并装载 其实这就是所谓的 第一人惩罚 因为首次访问的时候要执行一 系列以上的过程 所以会耗费一些时间 以后的访问就不会这样了 Java servlet 提供了一种强有力的 API 用这个 API 可以访问关于请求 会话和 应用程序的所有信息 将 servlet 和 JSP 页面组合起来使用 可以把应用程序的逻辑 部分和外观呈现部分清楚地分开 换句话 利用这个方式可以对模型 视图和控制 器这三种角色分别使用最合适的组件类型 16 ServletServlet 过滤器和监听器 过滤器和监听器 Servlet 是一种 Java 类 它使得服务器的功能可扩展至处理请求和生成应答 它 是用 Servlet API 定义的类和接口实现的 API 由两个程序包组成 jvavax servlet 程 序包包含独立于协议的类和接口 而 javax servlet http 程序包则提供 HTTP 特定的扩 展的实用程序类 Servlet 的实质是实现了接口 javax servlet Servlet 的类 实现是直接完成或通过 扩展某个支持类来完成的 该接口定义了 Web 容器用来管理 servlet 和与之交互的方 法 用于处理 HTTP 请求的 servlet 一般情况下都会扩展 javax servlet http HttpServlet 类 该类实现了 Servlet 接口 并提供了使用 HTTP 处理的附加方法 ServletServlet 的生命周期的生命周期 Web 容器管理 servlet 生命周期的所有方面 它根据需要创建 servlet 类的实例 将请求传递给实例进行处理 最终删除实例 对于 HttpServlet 来说 容器会在 servlet 生命周期的适当时间调用方法 除了 doGet 和 doPost 方法之外 还有一些对应于其他 HTTP 方法的方 法 doDelete doHead doOptiongs doPut 和 doTrace 一般 情况下不用实现这些方法 因为 HttpServlet 类已经用适用于大多数 servlet 的方法考 虑到了 HEAD OPTIONS 和 TRACE 请求 而且 DELETE 和 PUT 这两种 HTTP 方 法很少用在 Web 应用程序中 容器只为每个 Servlet 创建一个实例非常重要 这意味着 servlet 必须是线程安全 的 即 能够同时处理多个请求 每个处理都通过 servlet 代码作为单独的线程来执 行 如果只在 init 和 destroy 方法中修改参考的对象 而且只在请求处理方 法中读取他们 那么不用丧失任何细节就可以满足关于实例变量的这个要求 编译和安装编译和安装 servletservlet 要编译 servlet 必须首先确保 JAR 文件包含着 CLASSPATH 环境变量中所有 Servlet API 类 该 JAR 文件将随所有的 Web 容器一起发布 Tomcat 中包含了一个 名为 servlet jar 的 JAR 文件 位于 common lib 目录中 在 Windows 平台中 应在 17 CLASSPATH 中包含 JAR 文件 读取请求读取请求 传递到 doG
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 安徽省宣城市郎溪县2024-2025学年高二上学期期末考试化学试题及答案
- 小区农业生产合作合同
- 行政文件归档与资料管理系统
- 工程项目管理计划执行与监控工具
- 高中现代文阅读方法指导与训练教案
- 商业场所监控设备安装合同书
- 时间作息课件
- 时钟认识任意时间课件
- 写劳动最光荣作文(14篇)
- 绿色简约国际礼仪培训
- 肿瘤患者VTE预防治疗
- 南京科远KD200变频器使用手册
- 被迫解除劳动合同通知书范本
- 米粉生产工艺培训
- 《poct院内培训》课件
- 副校长申请书
- 一飞再飞(2024年贵州中考语文试卷记叙文阅读试题)
- DB11-T 806-2022 地面辐射供暖技术规范
- 纯英文初三数学试卷
- 2025年中铁建公路运营有限公司招聘笔试参考题库含答案解析
- 压缩空气流量及管径计算
评论
0/150
提交评论