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

Servlets & JSP Series 7 - Being a JSP

阅读更多

Servlets & JSP Series 7 - Being a JSP

 

  • A JSP becomes a servlet: a sevlet that you do not create, the container looks at your JSP, translates it into Java source code, and compiles it into a full-fledged Java servlet class.
  • The Container takes what you have written in your JSP, translates it into a servlet class source file, then compiles that into a Java servlet class, after that, it’s just servlets all the way down, and the servlet runs in exactly the same way it would if you had written and compiled the code yourself, in other words the Container loads the servlet class, instantiates and initializes it, makes a separate thread for each request, and calls the servlet’s service() method.
  • We can put regular old Java code in a JSP using a scriplet-which just means Java code within a <% … %> tag. Directives come in three flavors: page; include; taglib.
  • The Java code is between angle brackets with percent signs: <% and %>, but the directive adds an additional character to the start of the element-the @ sign; <% out.println %> equals <%= %>; three different JSP element types: Scriptlet <% %>; Directive <%@ %>; Expression <%= %>.
  • The container takes everything you type between the <%= %> and puts it in as the argument to a statement that prints to the implicit response PrintWriter out, when the container sees this: <%= Counter.getCount() %> it turns it into this: out.print(Counter.getCount());. So Please NEVER end an expression with a semicolon.
  • In an expression, if the method does not return anything, you will get an error, you cannot, MUST NOT use a method with a void return type as an expression, the Container is smart enough to figure out that there won’t be anything to print if the method has a void return type.
  • The page directive is about giving the Container information it needs when translating your JSP into a servlet, the attributes we care about are import, session, content-Type, and isELIgnored.
  • ALL scriptlet and expression code lands in a service method, that means variables declared in a scriptlet are always LOCAL variables.
  • There is another JSP element called a declaration: <%! int count=0 %>; JSP declarations are for declaring members of the generated servlet class, that means both variables and methods, in orther words, anything between the <%! And %> tag is added to the class outside the service method, that means you can declare both static variables and methods. A JSP declaration is always defined inside the class but outside the service method.
  • What the container does with your JSP: 1.Looks at the directives, for information it might need during translation; 2.Creates an HttpServlet subclass; 3.If there is a page directive with an import attribute, it writes the import statements at the top of the class file; 4.If there are declarations, it writes them into the class file, usually just below the class declaration and before the service method; 5.Builds the service method, the service method’s actual name is _jspServece(), it’s called by the servlet superclass’s overridden service() method, and receives the HttpServletRequest and HttpServletResponse, as part of building this method, the Container declares and initializes all the implicit objects; 6.Combines the plain old HTML, scriptlets, and expressions into the service method, formatting everything and writing it to the PrintWriter response output.
  • The key methods of generated servlet from JSP: 1.jspInit()(this method is called from the init() method, we can override this method); 2.jspDestroy()(this method is called from the servlet’s destroy() method, we also can override this method);3. _jspService()(this method is call from the servlet’s service() method which means it runs in a separate thread for each request, we cannot override this method, we cannot do anything with this method ourselves, and it’s up to the Container vender to take our JSP code and fashion the  _jspService() method that uses it.
  • You write the .jsp file, the Container writes the .java file for the servlet your JSP becomes.
  • Lifecycle of a JSP: 1.You write a .jsp file, and deploys it as part of a web app, the Container “reads” the web.xml(DD) for this app, but does not do anything else with the .jsp file(until the first time it’s requested); 2.The client hits a link that asks for the .jsp, the Container tries to TRANSLATE the .jsp into .java source code for a servlet class; 3.The Container tries to COMPLE the servlet .java source into .class file; 4.The Container LOADS the newly-generated servlet class; 5.The Container instantiates the servlet and causes the servlet’s jspInit() method to run, the object is now a full-fledged servlet, ready to accept client requests; 6.The Container creates a new thread to handle this lient’s request, and the servlet’s _jspService() method runs, everything that happens after this is just plain old servlet request-handling, eventually the servlet sends a response back to the client.
  • Translation and compilation happens only ONCE, when you deploy a web app with a JSP, the whole translation and compilation step happens only once in theJSP’s life, once it’s been translated and compiled, it’s just like any other servlet, and just like any other servlet, once that servlet has been loaded and initialized, the only thing that happens at request time is creation or allocation of a thread for the service method.
  • We configure servlet init params for our JSP virtually the same way we configure them for a normal servlet, the only difference is that we have to add a <jsp-file> element within the <servlet> tag.
  • We can use a PageContext reference to get attributes from any scope, including the page scope for attributes bound to the PageContext, the methods that work with other scopes take an int argument to indicate the scope, although the attribute access methods come from JspContext, we will find the constants for the scopes inside the PageContext class.
  • Page directory contains 13 kinds of attributes: 1.import; 2.isThreadSafe; 3.contentType; 4.isELIgnored; 5.isErrorPage; 6.errorPage; 7.language; 8.extends; 9.session; 10.buffer; 11.autoFlush; 12.info; 13.pageEncoding.
  • An EL expression always looks like this: ${something}, in other words, the expression is always enclosed in curly braces, and prefixed with a dollar ($) sign.
  • It’s simple to make scriptlets invalid for a JSP to have scripting elements(scriptlets, Java expressions, or declarations) by putting a <scripting-invalid> tag in the DD: <scripting-web-app ...> ... <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <scripting-invalid> true </scripting-invalid> </jsp-property-group> </jsp-config> ...</web-app>
  • If you want EL-looking things in your JSP to be ignored, you have to say so explicitly, either through a page directive or a DD element (two methods): Putting <el-ignored> in the DD-<web-app ...> ... <jsp-confi g> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <el-ignored> true </el-ignored> </jsp-property-group> </jsp-confi g> ... </web-app>; OR Using the isELIgnored page directive attribute-<%@ page isELIgnored=”true” %>.
  • The page directive takes priority over the DD setting, if there is a conflict between the <el-ignored> setting in the DD and the isELIgnored page directive attribute, the directive always wins, that lets you specify the default behavior in the DD, but override if for a specific page using a page directive.
  • The page directive defines page-specific properties such as character encoding; The taglib directive defines tag libraries available to the JSP; The include directive defines text and code that gets added into the current page at translation time.
  • JSP implicit objects contains: application, out, request, response, session.

 

分享到:
评论
1 楼 aboutnull 2013-12-02  
英文没学好

相关推荐

    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

    《Head First Servlets and JSP》是学习Java服务器端编程的经典教材,中文版的第二版为读者提供了深入浅出的学习路径,特别适合初学者和有经验的开发者进行自我提升。这本书详细介绍了Servlets和JSP(JavaServer ...

    Head First Servlets & JSP, Second Edition

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

    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

    head first servlets & JSP(3)

    head first servlets & JSP(3)

    head first servlets & JSP(2)

    head first servlets & JSP(2)

    JAVA - TUTOR SERVLETS & JSP

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

    Head_First_Servlets_&_JSP_习题

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

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

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

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

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

    Head First Servlets & JSP 高清中文版 part2

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

    Jave开发指南Servlets&JSP

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

    Core Servlets & JSP_cn

    《Core Servlets & JSP_cn》是一本专为Java初学者设计的教程,全面涵盖了Servlets和JSP(JavaServer Pages)的核心技术。Servlets是Java Web开发中的基础组件,用于处理HTTP请求并生成动态响应,而JSP则是用于创建...

Global site tag (gtag.js) - Google Analytics