JSP
(1)JSP页面是由HTML语句和嵌套在其中的Java代码组成的一个普通的文本文件,JSP文件的扩展名必须为.jsp
(2)jsp页面的java代码必须嵌套在<% %>中;
(3)jsp文件就像普通的html文件一样,可以放置在WEB应用程序中的除了WEB-INF以及其子目录外的其他的任何目录中,jsp页面的访问路径与普通的html页面的访问李静的形式
也是完全一样。
(4)jsp的9个隐含变量
PageContext pageContext=null;
HttpSession httpSession=null;
ServletContext application=null;
ServletConfig config=null;
JspWriter out=null;
Object page=this;
使用<%%>编写的代码在此位置,可以用到request response pageContext session
application config out page 这8个隐含对象(实际上还可以使用一个叫exception的隐含变量)
①request:HttpRequest的一个对象。
<%
String name=request.getParameter("name");
out.print(name);
%>
请求:http://localhost:8090/javaWeb_0004/index.jsp?name=zhangsan
页面显示:
zhangsan
②response:HttpServletResponse的一个对象,在jsp页面几乎不会调用response的任何方法。
③pageContext:页面上下文,是PageContext的一个对象,可以从中获取其他的8个隐含对象,也可以从中获取到当前页面的其他信息(学习自定义标签时使用)
System.out.print(pageContext.getRequest()==request);
打印:true
④session:代表浏览器和服务器的一次会话,是HttpSession的一个对象,以后详细学习。
⑤application:代表当前web应用,是ServletContext对象。
<%
System.out.println(application.getInitParameter("password"));
%>
并在web.xml中初始化参数:
<context-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</context-param>
打印:
123456
⑥config:当前jsp对应的servlet的ServletConfig对象(几乎不用)
如果需要访问当前的jsp配置的初始化参数,需要映射地址才可以。
在web.xml中配置:
<servlet>
<servlet-name>indexjsp</servlet-name>
<jsp-file>/index.jsp</jsp-file>
<init-param>
<param-name>age</param-name>
<param-value>18</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>indexjsp</servlet-name>
<url-pattern>/indexjsp</url-pattern>
</servlet-mapping>
在index.jsp中定义
<%
System.out.println(config.getInitParameter("age"));
%>
打印:
18
⑦out:JsWriter对象,调用out.println()可以直接将文字打印到浏览器上。
out.print(config.getInitParameter("age"));
页面打印:
18
⑧page:指向当前jsp对应的servlet对象的引用,但是为object类型,只能调用Object类的方法(几乎不用)
⑨exception:在声明了page指令为isErrorPage=“true” 时,才可以使用。
<%@page isErrorPage="true"%>
总结:
pageContext request session application(对属性的作用范围从小到大)
out response config page
exception
(1)JSP页面是由HTML语句和嵌套在其中的Java代码组成的一个普通的文本文件,JSP文件的扩展名必须为.jsp
(2)jsp页面的java代码必须嵌套在<% %>中;
(3)jsp文件就像普通的html文件一样,可以放置在WEB应用程序中的除了WEB-INF以及其子目录外的其他的任何目录中,jsp页面的访问路径与普通的html页面的访问李静的形式
也是完全一样。
(4)jsp的9个隐含变量
PageContext pageContext=null;
HttpSession httpSession=null;
ServletContext application=null;
ServletConfig config=null;
JspWriter out=null;
Object page=this;
使用<%%>编写的代码在此位置,可以用到request response pageContext session
application config out page 这8个隐含对象(实际上还可以使用一个叫exception的隐含变量)
①request:HttpRequest的一个对象。
<%
String name=request.getParameter("name");
out.print(name);
%>
请求:http://localhost:8090/javaWeb_0004/index.jsp?name=zhangsan
页面显示:
zhangsan
②response:HttpServletResponse的一个对象,在jsp页面几乎不会调用response的任何方法。
③pageContext:页面上下文,是PageContext的一个对象,可以从中获取其他的8个隐含对象,也可以从中获取到当前页面的其他信息(学习自定义标签时使用)
System.out.print(pageContext.getRequest()==request);
打印:true
④session:代表浏览器和服务器的一次会话,是HttpSession的一个对象,以后详细学习。
⑤application:代表当前web应用,是ServletContext对象。
<%
System.out.println(application.getInitParameter("password"));
%>
并在web.xml中初始化参数:
<context-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</context-param>
打印:
123456
⑥config:当前jsp对应的servlet的ServletConfig对象(几乎不用)
如果需要访问当前的jsp配置的初始化参数,需要映射地址才可以。
在web.xml中配置:
<servlet>
<servlet-name>indexjsp</servlet-name>
<jsp-file>/index.jsp</jsp-file>
<init-param>
<param-name>age</param-name>
<param-value>18</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>indexjsp</servlet-name>
<url-pattern>/indexjsp</url-pattern>
</servlet-mapping>
在index.jsp中定义
<%
System.out.println(config.getInitParameter("age"));
%>
打印:
18
⑦out:JsWriter对象,调用out.println()可以直接将文字打印到浏览器上。
out.print(config.getInitParameter("age"));
页面打印:
18
⑧page:指向当前jsp对应的servlet对象的引用,但是为object类型,只能调用Object类的方法(几乎不用)
⑨exception:在声明了page指令为isErrorPage=“true” 时,才可以使用。
<%@page isErrorPage="true"%>
总结:
pageContext request session application(对属性的作用范围从小到大)
out response config page
exception