`
Kayonlife
  • 浏览: 22404 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

Servlets & JSP Series 5 - Being a Web App

 
阅读更多

Servlets & JSP Series 5 - Being a Web App

  • Every servlet inherits a getServletConfig() method, the getServletConfig method returns a ServletConfig, and one of its methods is getInitParameter(), we cannot use servlet init parameters until the servlet is initialized.
  • When the container initializes a servlet, it makes a unique ServletConfig for the servlet, the Container “reads” the servlet init parameters from the DD and gives them to the ServletConfig, then passes the ServletConfig to the servlet’s init() method.
  • The whole process of ServletConfig survive: 1.Container reads the Deployment Descriptor for this servlet, including the servlet init parameters; 2.Container creates a new ServletConfig instance for this servlet; 3.Container creates a name/value pair of String for each servlet init parameter; 4.Container gives the ServletConfig references to the name/value init parameters; 5.Container creates a new instance of the servlet class; 6.Container calls the servelt’s init() method passing in the reference to the ServletConfig().
  • Serveltconfig’s main job is to give you init parameters, ServletConfig is one per servlet, ServletContext is one per web app.
  • There’s only on ServletContext for an entire web app, and sll the parts of the web app share it, but each servelt in the app has its own ServletConfig, the container makes a ServletContext when a web app is deployed, and makes the context available to each Servlet and JSP(which becomes a servlet) in the web app.
  • To listen for ServletContext events, write a listener class that implements ServletContextListener, put it I your WEB-INF/classes directory, and tell the Container by putting a <listener> element in the Deployment Descriptor.
  • How context listener works: 1.Container reads the Deployment Descriptor for this app, including the <listener> and <context-param> elements; 2.Container creates a new ServletContext for this application, that all parts of the app will share; 3.Container creates a name/value pair of Strings for each context init parameter; 4.Container gives the ServletContext references to the name/value parameters; 5.Container creates a new instance of the Listener class; 6.Container calls the listener’s contextInitialized() method, passing in a new ServletContextEvent, the event object has a reference to the ServletContext, so the event-handling code can get the context from the event, and get the context init parameter from the context; 7.Listener asks ServletContextEvent for a reference to the ServletContext; 8.Listener asks ServletContext for the context init parameter; 9. Listener uses the init parameter to construct a new Dog object; 10.Listener sets the Dog as an attribute in the ServletContext; 11.Container makes a new Servlet; 12.Servlet gets a request, and asks the ServletContext for the attrivute “dog”; 13. Servlet calls the method on this object.
  • Attrivute API: the three attribute scopes-context, request, and session-are handled by the ServletContext, ServletRequest, and HttpSession interfaces, the API methods for attributes are exactly the same in every interface.
  • We don’t need a lock on the servlet, we need the lock on the context, the typical way to protect the context attribute is to synchronize on the context object itself.
  • Protect session attributes by synchronizing on the HttpSession.
  • SingleThreadModel ensures that servlet handle only one request at a time, this interface has no methods, if a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet’s service method, the servlet container can make this guarantee by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet.
  • If you want all the threads to access a value, decide which attribute state makes the most sense, and store the value in an attribute, chances are, you can solve your problems in one of two ways: 1.Declare the variable as a local variable within the service method,rather than as an instance variable; 2.Use an attribute in the most appropriate scope.
  • RequestDispatchers have only two methods-forward() and include(), both take the request and response object, of the two methods, forward() is by far the most popular, it’s very unlikely you will use the include method from a controller servlet, however, hehind the scenes the include method is being used by JSPs in the <jsp:include> standard action, you can get a RequestDispatcher in two ways: from the request or from the context, regardless of where you get it, you have to tell it the web component to which you are fowwarding the request, in orther words, the servlet or JSP that will take over.
  • The include() method of RequestDispatcher is not used much in the real world, the include() method sends the request to something else to do some work and then comes back to the sender.
1
0
分享到:
评论

相关推荐

    Head First Servlets&JSP;-第2版-高清扫描版-带详细书签

    Head First Servlets&JSP;-第2版-高清扫描版-带详细书签 高清扫描版,书签比较详细,和目录一样

    深入浅出Servlets&JSP.

    深入浅出Servlets&JSP,感兴趣的人看一下吧

    Head First Servlets & JSP(完好高清中文版)2.pdf

    Head First Servlets & JSP(完好高清中文版)2.pdf 深入浅出 Servlets & JSP(完好高清中文版)2.pdf

    Head First Servlets & JSP 学习笔记

    《Head First Servlets & JSP》是一本广受欢迎的教材,它深入浅出地讲解了这些概念,帮助开发者通过SCWCD(Sun Certified Web Component Developer)认证。以下是一些关键知识点的详细解释: 1. **Servlet**: - *...

    Head First Servlets & JSP(完好高清中文版).part3

    Head First Servlets & JSP 完好高清中文版 part3 共6部分 Head First系列的书绝对是初学者的首选!风格生动有趣,讲解由浅入深。 发现网上流传的中文版存在有几处部分页面缺失的问题,花一个下午进行了修复。

    jstl&standard&jsp-api&servlet-api.jar

    【标题】"jstl&standard&jsp-api&servlet-api.jar" 涉及到的是四个关键的Java Web开发库,它们在构建基于Java的Web应用程序时起着至关重要的作用。这里,我们主要讨论JSTL(JavaServer Pages Standard Tag Library)...

    Head First Servlets and JSP 中文版 第2版 PDF电子书下载 带书签目录 完整版.zip

    这本书详细介绍了Servlets和JSP(JavaServer Pages)技术,它们是Java EE平台中的核心组件,用于构建动态、交互式的Web应用程序。 Servlets是Java编程语言中的一种服务器端扩展,它允许开发者创建能够处理HTTP请求...

    Head First Servlets & JSP, Second Edition

    《Head First Servlets & JSP, Second Edition》是一本针对初学者的优秀教材,它深入浅出地介绍了Servlet和JSP这两个Java Web开发的核心技术。Servlet是Java平台上的服务器端编程接口,而JSP(JavaServer Pages)则...

    Java-for-the-Web-with-Servlets_JSP_and-EJB.pdf

    ### Java Web 开发技术详解:Servlets、JSP与EJB #### 一、概述 《Java for the Web with Servlets, JSP, and EJB》是一本全面介绍如何使用 Java 进行 Web 应用程序开发的专业书籍。本书不仅涵盖了 Servlet 2.3、...

    Head First Servlets & JSP(完好高清中文版)

    《Head First Servlets&JSP》应了最新的学习理论,能将知识直接送到你的大脑里。你会通过不寻常的方式同Servlet和JSP打交道,可以学得更深入、更快,而且更重要的是,你能真正地学以致用。你可以看看为什么那么多...

    Head First Servlets & JSP(完好高清中文版)1.pdf

    Head First Servlets & JSP(完好高清中文版)1.pdf 深入浅出 Servlets & JSP(完好高清中文版)1.pdf

    JAVA - TUTOR SERVLETS & JSP

    Java Servlets 和 JSP(JavaServer Pages)是Java在Web开发中的两个核心技术,它们用于构建动态、交互式的网页应用程序。本教程将深入讲解这两个概念及其优势、安装配置、基本使用方法,以及如何处理请求和响应。 1...

    head first servlets & JSP(3)

    head first servlets & JSP(3)

    head first servlets & JSP(2)

    head first servlets & JSP(2)

    Jave开发指南Servlets&JSP

    《Java开发指南Servlets&JSP》是一本深入讲解Java Web开发的重要书籍,主要涵盖了Servlets和JSP(JavaServer Pages)这两个核心技术。在Java Web开发领域,Servlets和JSP是构建动态网站和Web应用程序的基础,它们为...

    head first servlet&JSP讲解课件java-web

    【标题】"head first servlet&JSP讲解课件java-web" 涉及的是Java Web开发中的核心组件——Servlet和JSP(JavaServer Pages)技术。Servlet是Java平台上的一个标准,用于创建动态Web应用程序,而JSP是Servlet的一种...

    Head_First_Servlets_&_JSP_习题

    《Head First Servlets & JSP》是一本深受程序员喜爱的学习指南,主要涵盖了Servlets和JSP(JavaServer Pages)这两个核心的Java Web开发技术。Servlets是Java平台上的服务器端编程模型,而JSP则是用于创建动态网页...

    Head First Servlets & JSP 高清中文版 part2

    Head First Servlets & JSP 高清中文版 part2 共5部分

    Core Servlets & JSP_cn

    5. **JSP指令与动作元素**:JSP指令(如`&lt;%@ page %&gt;`, `&lt;%@ include %&gt;`, `&lt;%@ taglib %&gt;`)用于页面级别的配置,而JSP动作元素(如`&lt;jsp:include&gt;`, `&lt;jsp:forward&gt;`, `&lt;jsp:useBean&gt;`)用于在页面间进行控制流...

Global site tag (gtag.js) - Google Analytics