jsp技术
1.jsp:java server pages,是sun公司定义的一种用来开发动态web资源的技术。
servlet也是用于开发动态web资源的技术。
jsp和servlet都是用于开发动态web:
1.在servlet中对数据进行美化展示比较麻烦,但是适合编写大量的java代码。
2.在jsp页面中不适合编写大量的java代码,但是做数据美化比较方便,写jsp类似写html代码。
所以:
jsp:页面的展示。
servlet:数据的处理。
jsp中的两门技术:
EL表达式。
JSTL标签
两种技术是解决jsp中不写java代码的技术。
jsp的调用和运行原理:
当我们访问一个jsp页面时候,其实是访问servlet,因为容器会将这个jsp页面翻译成servlet.
在servlet中:
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/WEB-INF/jsp/1.jsp");
requestDispatcher.forward(req, resp);
显示1jsp页面,在tomcat容器中,容器将这个1.jsp翻译成:
package org.apache.jsp.WEB_002dINF.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
public final class _1_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
private static java.util.List _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
}
public void _jspDestroy() {
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType("text/html;charset=ISO-8859-1");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write('\r');
out.write('\n');
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.write("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
out.write("<html>\r\n");
out.write(" <head>\r\n");
out.write(" <base href=\"");
out.print(basePath);
out.write("\">\r\n");
out.write(" \r\n");
out.write(" </head>\r\n");
out.write(" \r\n");
out.write(" <body>\r\n");
out.write(" This is my JSP page. <br>\r\n");
out.write(" \r\n");
out.write(" \r\n");
out.write(" ");
Date date = new Date();
out.write(date.toLocaleString());
out.write("\r\n");
out.write(" </body>\r\n");
out.write("</html>\r\n");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
继承的这个类(org.apache.jasper.runtime.HttpJspBase)的源码是:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jasper.runtime;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.HttpJspPage;
import javax.servlet.jsp.JspFactory;
import org.apache.jasper.compiler.Localizer;
/**
* This is the super class of all JSP-generated servlets.
*
* @author Anil K. Vijendran
*/
public abstract class HttpJspBase
extends HttpServlet
implements HttpJspPage
{
protected HttpJspBase() {
}
public final void init(ServletConfig config)
throws ServletException
{
super.init(config);
jspInit();
_jspInit();
}
public String getServletInfo() {
return Localizer.getMessage("jsp.engine.info");
}
public final void destroy() {
jspDestroy();
_jspDestroy();
}
/**
* Entry point into service.
*/
public final void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
_jspService(request, response);
}
public void jspInit() {
}
public void _jspInit() {
}
public void jspDestroy() {
}
protected void _jspDestroy() {
}
public abstract void _jspService(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException;
}
这个类继承了HttpServlet,可见容器是将jsp页面翻译成了servlet,然后进行展示。
服务器给客户端返回的数据是:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="http://localhost:8080/Servlet/">
</head>
<body>
This is my JSP page. <br>
2013-9-10 14:58:43
</body>
</html>
原理是在1_jsp.java中调用out流对象输出(编写的java代码原封输出):
out.write("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
out.write("<html>\r\n");
out.write(" <head>\r\n");
out.write(" <base href=\"");
out.print(basePath);
out.write("\">\r\n");
out.write(" \r\n");
out.write(" </head>\r\n");
out.write(" \r\n");
out.write(" <body>\r\n");
out.write(" This is my JSP page. <br>\r\n");
out.write(" \r\n");
out.write(" \r\n");
out.write(" ");
Date date = new Date();
out.write(date.toLocaleString());
out.write("\r\n");
out.write(" </body>\r\n");
out.write("</html>\r\n");
在jsp页面中我们可以调用out流对象,还有:
request
response
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
JSP语法:eclipse中修改jsp模板(在eclipse中搜索Servlet.java,然后在同目录中的jsp中修改模板)
1.jsp模板元素
jsp页面中的html内容称之为jsp的模板元素。
jsp模板元素定义了网页的基本骨架,即定义了jsp页面的结构和外观。
2.jsp脚本表达式。
<%= %>:作用是用于向浏览器输出内容。翻译成:out.print();
3.脚本片段
<% %> :嵌入代码,将代码原封不动的输出。
4.jsp声明:
<%! %:定义的方法、属性,变量,是在service方法的外面。
5.jsp注释
<%-- --%>:jsp注释不会把内容打给浏览器,而html的注释会把内容打给浏览器。
6.jsp指令(告诉tomcat如何处理这个jsp页面)
page指令
include指令
taglib指令
jsp中的九大隐式对象:
request HttpServletRequest
response HttpServletResponse
config ServletConfig
application ServletContext
exception Throwable
session HttpSession
page this(翻译完的Servlet对象)
out JspWriter
pageContext PageContext
pageContext对象是JSP技术中最重要的一个对象,它代表JSP页面的运行环境,该对象提供了三个功能
获得其他8个隐式对象
作为域对象存储数据,被称为page域
提供了一些常用操作,forward请求转发、include包含
web开发接触到了4个域对象:(之所以他们是域对象,原因就是他们都内置了map集合,都有setAttribute getAttribute方法。)
pageContext(称之为page域) PageContext
request(称之为request域)HttpServletRequest
session(称之为session域)HttpSession
application(称之为application域)ServletContext
代表各个域的常量
APPLICATION_SCOPE、SESSION_SCOPE、REQUEST_SCOPE、PAGE_SCOPE
Web开发中的四个域对象:
由范围小到大:page(jsp有效)
request(一次请求)
session(一次会话)
application(当前web应用)
page域指的是pageContext.
request域指的是request HttpServletRequest
session 域指的是 session HTTPSession
application 域指的是 application ServletContext
之所以他们是域对象,原因就是他们都内置了map集合,都有setAttribute getAttribute方法。
他们都有自己固定的生命周期和作用域。
这4个对象的生命周期:
page:jsp页面被执行,生命周期开始,jsp页面执行完毕,声明周期结束
request:用户发送一个请求,开始,服务器返回响应,请求结束,生命周期结束
session:用户打开浏览器访问,创建session(开始),session超时或被声明失效,该对象生命周期结束
application:web应用加载的时候创建。Web应用被移除或服务器关闭,对象销毁。[结束]。
Page只在当前jsp有效,每次请求风别对应不同的request. Request,只在当前请求有效,每次请求分别对应不同的request域 Session只在一次会话中有效,会话结束就无法取到数 据了。
四个域对象在选择的时候,能用范围小的绝不用范围大的
page:数据只是暂时存在集合,在jsp页面的其他地方要用,用page(页面中自定义的map) (什么时候需要用map了,就用page)
Request:数据只是做显示的,看完了就没用了。就存request域,请求转发,Servlet产生的处理结果(数据)交给jsp显示。 数据转发可以带数据。
Session:数据给用户看完了,一定还要用,会话结束了就没用了 用户登录,用户信息发给客户端看,看完了,一会访问别的页面还要看用户信息。 购物车,购物车成功了,给用户看购物车,待会随时间可以查看购物车 请求重定向,因为是两次请求,每一次请求的数据,第二次请求还要看。
application:数据给一个用户用完了,别人还要用 聊天室,聊天记录,需要给所有的用户看 统计网站在线人数,所有看到的应该是一个数
总结:需要定义Map不如用page,请求转发Servlet,带给jsp的数据存request请求重定向带过去的数据存Session,全局的数据存application。
在自定义标签中,只要将pageContext传出去,这样在自定义标签中就可以或得其他的八大隐式对象。而不需要将所有的对象传过去。