一.JSP页面中的隐含对象
1.输入输出对象
(1)request
代表客户端的请求,获取任何来自客户端的信息
(2)response
代表对客户端响应,利用response可以向客户端发消息
(3)out
2.作用域通信对象
(1)session
(2)application
(3)pageContext
3.Servlet对象
(1)page
(2)config
4.异常对象
(1)exception
二.JSP的生存周期管理
web 容器(也称为JSP容器 Servlet容器):
用于承载执行web组件(Servlet/JSP)的容器软件成为web容器(Tomcat是web容器中的一种)
1.第一次请求时候,web容器会找到JSP文件,并且将JSP翻译为java(Servlet),再将Java文件编译为class,然后创建为对象
2.执行对象中的方法,响应用户请求,相当于执行Servlet的doGet或doPost
3.第二次请求同一个JSP时候,直接找到JSP对象并且执行其方法
4.在JSP文件更新以后,重新开始第一步
(1)JSP是自动热部署的,修改了JSP不用重启web容器
三.Servlet对象生存管理:
1.当前用户第一次请求Servlet时候,Web容器会找到Servlet类,并且创建Servlet对象
2.Servlet创建以后,立即调用其 init() 方法(单线程调用)
3.执行service()方法响应用户的请求
(1)service方法的底层会根据请求类型调用doGet/doPost
(2)重新doGet/doPost执行业务逻辑
(3)service()可以被多个用户发起的线程并发多次执行
4.当容器关闭时候,会销毁Servlet对象,在销毁之前执行其destroy()方法
(1)destroy也是单线程调用
obj.jsp:
el.jsp:
MyServlet:
DemoServlet:
1.输入输出对象
(1)request
代表客户端的请求,获取任何来自客户端的信息
(2)response
代表对客户端响应,利用response可以向客户端发消息
(3)out
2.作用域通信对象
(1)session
(2)application
(3)pageContext
3.Servlet对象
(1)page
(2)config
4.异常对象
(1)exception
二.JSP的生存周期管理
web 容器(也称为JSP容器 Servlet容器):
用于承载执行web组件(Servlet/JSP)的容器软件成为web容器(Tomcat是web容器中的一种)
1.第一次请求时候,web容器会找到JSP文件,并且将JSP翻译为java(Servlet),再将Java文件编译为class,然后创建为对象
2.执行对象中的方法,响应用户请求,相当于执行Servlet的doGet或doPost
3.第二次请求同一个JSP时候,直接找到JSP对象并且执行其方法
4.在JSP文件更新以后,重新开始第一步
(1)JSP是自动热部署的,修改了JSP不用重启web容器
三.Servlet对象生存管理:
1.当前用户第一次请求Servlet时候,Web容器会找到Servlet类,并且创建Servlet对象
2.Servlet创建以后,立即调用其 init() 方法(单线程调用)
3.执行service()方法响应用户的请求
(1)service方法的底层会根据请求类型调用doGet/doPost
(2)重新doGet/doPost执行业务逻辑
(3)service()可以被多个用户发起的线程并发多次执行
4.当容器关闭时候,会销毁Servlet对象,在销毁之前执行其destroy()方法
(1)destroy也是单线程调用
request.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Request</title>
</head>
<body>
<h1>Request代表客户端的请求</h1>
<%
//利用Request处理客户端发送的请求信息
String uri = request.getRequestURI();
String ctxPath = request.getContentType();
out.print("uri:"+uri +"<br>");
out.print("contextPath:"+ctxPath);
//利用request的setAttribute和getAttribute
//方法在多个Servlet和JSP之间传递数据
%>
</body>
</html>
obj.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>隐含对象演示</title>
</head>
<body>
<h1>JSP中的隐含对象</h1>
<p>就是可以直接使用的对象,其目的就是减少声明变量的环节,提高开发效率;隐含对象都固定的变量名,变量名不能改变</p>
<p>request:<%=request.getClass() %>实现HttpServletRequest接口</p>
<%
if(request instanceof HttpServletRequest){
out.println("实现HttpServletRequest接口");
}
%>
<p>response:<%=response.getClass() %></p>
<%
if(response instanceof HttpServletResponse){
out.println("实现HttpServletResponse接口");
}
%>
<p>out: <%=out.getClass() %></p>
<%
if(out instanceof JspWriter){
out.println("实现JspWriter接口");
}
%>
<p>session: <%=session.getClass() %></p>
<%
if(session instanceof HttpSession){
out.println("HttpSession类型");
}
%>
<p>application(应用): <%=application.getClass() %></p>
<%
if(application instanceof ServletContext){
out.println("ServletContext类型");
}
%>
<p>pageContext(页面上下文):<%=pageContext.getClass() %></p>
<%
if(pageContext instanceof JspContext){
out.println("JspContext类型");
}
%>
<p>page(页面):<%=page.getClass() %></p>
就是当前JSP对应的Servlet对象:
<%
if(page instanceof HttpServlet){
out.println("HttpServlet类型");
}
%>
<p>config: <%=config.getClass() %></p>
<%
if(config instanceof ServletConfig){
out.println("ServletConfig类型");
}
%>
<p>exception: 在错误处理页面中存在!</p>
</body>
</html>
el.jsp:
<%@ page import="java.util.*,demo.*" language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>EL表达式演示</title>
</head>
<%
//这个代码块代表Servlet控制器
request.setAttribute("name","Tom");
request.setAttribute("number",3.1415926);
//EL 可以读取数组数据
int[] ary = {4,1,7};
String[] names = {"Tom","熊大"};
request.setAttribute("ary",ary);
request.setAttribute("names", names);
//EL 可以读取线性表List
List<String> list = new ArrayList<String>();
list.add("rose");
list.add("lily");
request.setAttribute("actor",list);
//EL 读取Map
Map map = new HashMap();
map.put("java","Java 核对技术");
map.put("PHP","最好的编程语言");
map.put("Python", "人生苦短我学Python");
map.put("class", "类型");
request.setAttribute("language",map);
//EL 读取对象Bean属性
Product p = new Product(1,"手机",8888.0);
request.setAttribute("product",p);
//复合读取
Product[] arry={new Product(1,"rose",4.0),
new Product(2,"lily",9.0),
new Product(3,"apple",3.0)};
request.setAttribute("products",arry);
%>
<body>
<h1>EL表达式演示</h1>
<h2>EL 读取基本的数据</h2>
<p>EL读取name: ${name}</p>
<p>EL读取number: ${number}</p>
<h2>读取数组类型</h2>
<p>读取整数数组:${ary[1]}</p>
<p>读取字符串数组:${names[1]}</p>
<h2>EL读取List</h2>
<p>著名演员:${actor[1]}</p>
<h2>EL读取Map</h2>
<p>php: ${language.PHP}</p>
<p>php: ${language['PHP']}</p>
<p>class: ${language["class"]}</p>
<h2>EL读取对象的Bean属性</h2>
<p>Bean属性:是指对象的getXxx方法</p>
<p>读取方式: ${对象.xxx}</p>
<p>商品名: ${product.name}</p>
<h2>EL 可以复合使用</h2>
<p>案例: ${products[2].name}</p>
</body>
</html>
MyServlet:
package demo;
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;
/**
* 测试Servlet对象的生命周期
* @author soft01
*
*/
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* 创建对象以后执行,只执行一次!用于初始化资源
*/
public void init(ServletConfig config) throws ServletException {
System.out.println("Servlet创建了");
}
/**
* 每次请求时候执行的方法
*/
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet服务");
resp.setContentType("text/html");
resp.getWriter().print("OK");
}
/**
* 关闭容器,销毁Servlet对象之前执行的方法
*/
public void destroy() {
System.out.println("我还会回来的!");
}
}
DemoServlet:
package demo;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* 利用Servlet接口实现Servlet
* @author soft01
*
*/
public class DemoServlet implements Servlet {
public ServletConfig getServletConfig() {
return null;
}
public String getServletInfo() {
return null;
}
public void init(ServletConfig arg0) throws ServletException {
System.out.println("创建!");
}
public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
System.out.println("Service()");
resp.setContentType("text/html;charset=utf-8");
// resp.setCharacterEncoding("utf-8");
resp.getOutputStream().write("追逐的敏".getBytes());
}
public void destroy() {
System.out.println("I will be back!");
}
}