JSP 基础编程 报告人 : 包亮 邮箱 :rslab@lzb.ac.cn
HTTP 基础 HTTP is a simple, stateless protocol. A client, such as a web browser, makes a request, the web server responds, and the transaction is done.
Client 发出一个请求 : 包括 HTTP 命令和一些附加信息如 C 端用的是什么软件和什么类型的内容. 服务器响应 : 版本号 + 状态码 (200,404) 和服务器的附加信息. 例子 : 见下页
GET /intro.html HTTP/1.0 User-Agent: Mozilla/4.0 (compatible; MSIE 4.0; Windows 95) Accept: image/gif, image/jpeg, text/*, */* HTTP/1.0 200 OK Date: Saturday, 23-May-00 03:25:12 GMT Server: Tomcat Web Server/3.2 MIME-version: 1.0 Content-type: text/html Content-length: 1029 Last-modified: Thursday, 7-May-00 12:15:35 GMT
与服务器交互的方法 把客户端连接服务器并发送的 http 请求叫作方法. 最常见的是 Post 和 Get 方法,POST 是张贴而 GET 是读
Jsp 服务器的搭建 Web 服务器 + 应用程序服务器 + 数据库 Web 服务器 + 独立的 servlet 容器 + 数据库 大多数应用程序服务器和若干独立 servlet 容器都内置了 web 服务.
Apache+tomcat+mysql 安装 j2sdk-1.4.06 安装 apache 安装 tomcat(4.x-5.0.x) apache 和 tomcat 的整合 http://linux.chinaunix.net/doc/netconf/2005-02-02/1058.shtml
一切都是对象 Java 是纯的面向对象程序设计语言.Jsp 继承了 java 的语法和特性. JSP 从这个层次上面可以理解为 : 用 java 的方法来解决 http 协议需求.
Jsp 中的隐含对象 Jsp 的设计开发人员造出了 9 个内置对象, 用于 Http 协议的需求. 内置 : 说明本身 jsp 页面里面就有了. 不需要创建对象. 如 :Request request = new Request();// 不需要.
Hello.jsp 主要向大家展示了 request 内置对象使用方法和页面之间的参数传递. request.getparameter();
Request Response PageContext Session Application Out Config Page Exception
HttpServletRequest request The servlet request. HttpServletResponse response The servlet response. javax.servlet.jsp.jspwriter out The output writer, used like a PrintWriter but it has different buffering characteristics. HttpSession session The user's session. ServletContext application The web application. javax.servlet.jsp.pagecontext pagecontext An object primarily used to abstract the implementation of the server but sometimes used directly to share variables between JSP pages and supporting beans and tags.
幕后的运行机制
经 tomcat 编译过的 hello.jsp 文件 hello.class
JSP 语法 Expressions and Declarations <%! Declaration;[declaration;]+.%> <%=expression%> Saying "Hello" Using a JSP Declaration hello2.jsp 全局变量和局部变量的关系.
Jsp 语法 Directives <%@ directivename attribname= attribvalue %> <%@ page %>
page contenttype Specifies the content type of the generated page. For example: <%@ page contenttype="text/plain" %> The default content type is text/html; charset=8859_1. import Specifies a list of classes and packages the generated servlet should import. Multiple classes can be given in a commaseparated list. For example: <%@ page import="java.io.*,java.util.hashtable" %> The implicit include list is java.lang.*,javax.servlet.*,javax.servlet.http.*,javax.servlet.jsp.*.
buffer Specifies the minimum required size of the response buffer in kilobytes, similar to the servlet method setbuffersize( ). The value should be written as ##kb. A special value of none indicates that content should be passed directly to the underlying PrintWriter in the ServletResponse (which may or may not pass the content directly to the client). For example: <%@ page buffer="12kb" %> The default is 8kb. autoflush Specifies if the buffer should be flushed when it's filled or if instead an IOException should be thrown. A true indicates to flush, a false indicates to throw. For example: <%@ page autoflush="true" %> The default is true. session Indicates the page wants to have access to the user's session. A true puts the session variable in scope and may set a client cookie to manage the session. A false disables access to the session variable. For example: <%@ page session="false" %> The default is true.
errorpage Specifies a page to display if a Throwable is thrown from within the JSP page and is not caught before reaching the server. This proves useful because it's difficult to do try/catch blocks when writing JSP pages. For example: <%@ page errorpage="/error.jsp" %> The default behavior is implementation dependent. The path is context relative, so you don't need to worry about prepending the current context path. The target may be a JSP but doesn't have to be. If the target is a servlet, the servlet may retrieve the Throwable as the request attribute javax.servlet.jsp.jspexception. iserrorpage Indicates the page is intended to be used as the target of an errorpage. If the value is true the page can access an implicit variable named exception to retrieve the Throwable. language Specifies the scripting language used in the code sections of the page. The language used must interact with Java well enough to expose the necessary Java objects to the script environment. For example: <%@ page language="javascript" %> The default is java, the only language blessed by the specification.
JSP 和 JavaBean Javabean 是可重用的类, 使用 <jsp:usebean> 标签可以将 javabeans 直接嵌入到 jsp 页面中或作为一个可供 jsp 调用的操作.
调用 javabean <jsp:usebean id= name scope= page request session application class= classname type= typename > </jsp:usebean>
和 bean 的交互 <jsp:setproperty name= beanname property= * /> <jsp:setproperty name= beanname property= propertyname /> <jsp:setproperty name= beanname propery= propertyname param= paramname />
例 :hello3.jsp 调用了 java 文件, 可以减少 jsp 页面的内容, 把逻辑控制放在 bean 里面,jsp 只起到一个显示作用.
包含和转向 <%@ include file= pathfile %> 其作用范围 :translation time,servlet 创建期 <jsp:include page = pathtodynamicresource flush= true /> 作用范围在请求时间.
<jsp:forward page= pathtodynamicresource /> <jsp:param/>
标签库 让 jsp 页面包含 xml 标签 ( 类似于 html 的标签 ) 最广泛的标签库 apache Taglibs 和 Apache Struts
Jsp 乱码和与数据库连接 Java 用的是 unicode 编码, 本地要依赖于环境系统. Jdbc 数据库联接