Head First Servlet & JSP . 慢读与空想笔记

三本镇坑:

Table of Contents (Summary)

Intro xix
1. Why use Servlets & JSPs: an introduction 1
2. Web App Architecture: high-level overview 37
3. Mini MVC Tutorial: hands-on MVC 67
4. Being a Servlet: request AND response 93
5. Being a Web App: attributes and listeners 147
6. Conversational state: session management 223
7. Being a JSP: using JSP 281
    In the end, a JSP is just a servlet
    Making a JSP that displays how many times it's been accessed
    She deploys and tests it
    The JSP doesn't recognize the Counter class
    Use the page directive to import packages
    But then Kim mentions "expressions"
    Expressions become the argument to an out.print()
    Kim drops the final bombshell...
    Declaring a variable in a scriptlet
8. Script-free pages: scriptless JSP 343
9. Custom tags are powerful: using JSTL 439
10. When even JSTL is not enough: custom tag development 499
11. Deploying your web app: web app deployment 601
12. Keep it secret, keep it safe: web app security 649
13. The Power of Filters: wrappers and filters 701
14. Enterprise design patterns: patterns and struts 737
A Appendix A: Final Mock Exam 791
i Index
引子 xix
1 为什么使用Servlets & JSP: 前言与概述
2 Web应用体系结构:高层概述
3 MVC迷你教程:MVC实战
4 作为Servlet:请求和响应
5 作为Web应用:属性和监听者
6 会话状态:会话管理
7 作为JSP:使用JSP
8 没有脚本的页面:无脚本的JSP
9 强大的定制标记:使用JSTL
10 JSTL也有力不能及的时候:定制标记开发
11 部署Web应用:Web应用部署
12 要保密,要安全:Web应用安全
13 过滤器的威力:过滤器和包装器
14 企业设计模式:模式和struts
A 附录A:最终模拟测验
i 索引

7

首先是一个例子:

Counter.java
package com.example.model;

public class Counter{
    private static int count;
    public static synchronized int getCount() {
        count++;
        return count;
    }
}
BasicCounter.jsp
<%@ page import="com.example.model.*" %>
<html>
<body>
    <%= Counter.getCount() %>
</body>
</html>

接下来编译部署

javac -d classes src\com\example\model\Counter.java

将编译后的class文件与jap文件,放在Tomcat中,启动Tomcat,在浏览器中观测结果。

然后问题就来了:

  1. Counter.java写的什么?

因为不懂java语言,只能猜了。

声明,以下编译单元为包com.example.model;
声明并定义公有类Counter;
    声明私有静态整数型类字段count;
    声明并定义公有静态异步整数型方法getCount;
        count自加并返回
  1. package是什么?

A package declaration

包声明出现在普通编译单元中,用于指示编译单元所属的包。

  1. <%@ page import="com.example.model.*" %> 是什么?

import 是 page 众多属性之一,page 是 3 种 directive 之一。

Directives are messages to the JSP container. Directives have this syntax:

<%@ directive { attr=”value” }* %>

指令为你提供了一条途径,可以在页面转换时向容器提供特殊的指示。

The page directive defines a number of page dependent properties and communicates these to the JSP container.

<%@ page page_directive_attr_list %>

page_directive_attr_list ::= { language=”scriptingLanguage”}
                             { extends=”className” }
                             { import=”importList” }
                             { session=”true|false” }
                             { buffer=”none|sizekb” }
                             { autoFlush=”true|false” }
                             { isThreadSafe=”true|false” }
                             { info=”info_text” }
                             { errorPage=”error_url” }
                             { isErrorPage=”true|false” }
                             { contentType=”ctinfo” }
                             { pageEncoding=”peinfo” }
                             { isELIgnored=”true|false” }
                             { deferredSyntaxAllowedAsLiteral=”true|false”}
                             { trimDirectiveWhitespaces=”true|false”}

An import attribute describes the types that are available to the scripting environment. The value is as in an import
declaration in the Java programming language, a (comma separated) list of either a fully qualified Java programming language type name denoting that type, or of a package name followed by the .* string, denoting all the public types
declared in that package.

推荐阅读更多精彩内容