java web中align命令_javaweb之jsp指令

1.JSP指令简介

JSP指令是为JSP引擎设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分。

在JSP 2.0规范中共定义了三个指令:page指令,Include指令,taglib指令。

JSP指令的基本语法格式:

例如:

如果一个指令有多个属性,这多个属性可以写在一个指令中,也可以分开写。

例如:

也可以写作:

2.page指令

page指令用于定义JSP页面的各种属性,无论page指令出现在JSP页面中的什么地方,它作用的都是整个JSP页面,为了保持程序的可读性和遵循良好的编程习惯,page指令最好是放在整个JSP页面的起始位置。

JSP 2.0规范中定义的page指令的完整语法:

[ language="java" ]

[ extends="package.class" ]

[ import="{package.class | package.*}, ..." ]

[ session="true | false" ]

[ buffer="none | 8kb | sizekb" ]

[ autoFlush="true | false" ]

[ isThreadSafe="true | false" ]

[ info="text" ]

[ errorPage="relative_url" ]

[ isErrorPage="true | false" ]

[ contentType="mimeType [ ;charset=characterSet ]" | "text/html ; charset=ISO-8859-1" ]

[ pageEncoding="characterSet | ISO-8859-1" ]

[ isELIgnored="true | false" ]

%>

2.1 import属性

在jsp页面中,jsp引擎会自动导入下面的包和类:

java.lang.*

javax.servlet.*

javax.servlet.jsp.*

javax.servlet.http.*

可以在一条page指令引入多个类和包,其中的每个包和类之间使用逗号分隔开,例如,

2.2 errorPage属性

errorPage属性的设置值必须使用相对路径,如果以“/”开头,表示相对于当前Web应用程序的根目录(注意不是站点根目录),否则,表示相对于当前页面。

可以在web.xml文件中使用元素为整个Web应用程序设置错误处理页面。

元素有3个子元素,、、

子元素指定错误的状态码,例如:404

子元素指定异常类的完全限定名,例如:java.lang.ArithmeticException

子元素指定以“/”开头的错误处理页面的路径,例如:/ErrorPage/404Error.jsp

如果设置了某个JSP页面的errorPage属性,那么在web.xml文件中设置的错误处理将不对该页面起作用。

jsperrorPage的相对路径,“/”表示当前web应用程序的根目录(WebRoot),“./”代表当前目录(即当前文件所在的目录),“../”代表当前文件所在目录的上一级目录。

例如有以下的工程目录结构:

e343586a381914a72dcc24130cf1a845.png

testA.jsp中page指令的errorPage路径为:

即路径"/jspTest/error.jsp"为“WebRoot/jspTest/error.jsp”。

使用errorPage属性可以指明出错后跳转的错误页面,比如如下的testA.jsp代码:

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

My JSP 'testA.jsp' starting page

int i=2/0;

%>

int i=2/0,显然出错,第二行page指令的errorPage属性指明出错后跳转到error.jsp文件,error.jsp的内容为:

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

My JSP 'error.jsp' starting page

PrintWriter outs=response.getWriter();

outs.write("出错啦!");

%>

运行结果如下:

195831ee31c49f8a194a1174a6a8b0c6.png

2.3 在web.xml中使用标签为整个web应用设置错误处理页面

例如,使用标签配置针对404错误的处理页面,在web.xml中的配置如下:

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

index.jsp

404

/jspTest/error.jsp

要跳转的error.jsp的代码如下:

404错误友好提示页面

404错误

3秒钟后自动跳转回首页,如果没有跳转,请点击这里

当访问一个不存在的web资源时,就会跳转到在web.xml中配置的404错误处理页面error.jsp,

38dae11e36aceecb098715b0ea183fc4.png

2.4 使用isErrorPage属性显示声明页面为错误

如果某一个jsp页面是作为系统的错误处理页面,那么建议将page指令的isErrorPage属性(默认为false)设置为“true”来显示声明这个jsp页面是一个错误处理页面。将error.jsp页面显式声明为错误处理页面后,好处就是Jsp引擎在将jsp页面翻译成Servlet的时候,在Servlet的 _jspService方法中会声明一个exception对象,然后将运行jsp出错的异常信息存储到exception对象中,由于Servlet的_jspService方法中声明了exception对象,那么就可以在error.jsp页面中使用exception对象,这样就可以在Jsp页面中拿到出错的异常信息了。如果没有设置isErrorPage="true",那么在jsp页面中是无法使用exception对象的。

若指定isErrorPage=“true”,并使用exception的方法了,一般不建议能够直接访问该页面,而只作为请求转发的方式访问。

Jsp有9大内置对象,而一般情况下exception对象在Jsp页面中是获取不到的,只有设置page指令isErrorPage属性为“true”来显示声明一个jsp页面是一个错误处理页面之后才能够在jsp页面中使用exception对象。

3.include指令

在JSP中对于包含有两种语句形式:@include指令和指令

3.1 @include指令

include指令用于引入其它JSP页面,如果使用include指令引入了其它JSP页面,那么JSP引擎将把这两个JSP翻译成一个servlet。所以include指令引入通常也称之为静态引入。

语法:,其中的file属性用于指定被引入文件的路径。路径以“/”开头,表示代表当前web应用。

例如:

includeTest1.jsp

String path1 = request.getContextPath();

String basePath1 = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path1+"/";

%>

My JSP 'includeTest.jsp' starting page

"includeTest1.jsp's content"

includeTest2.jsp

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

My JSP 'includeTest2.jsp' starting page

"includeTest2.jsp's content"

includeTest1.jsp使用将includeTest2.jsp内容包含进去,由于include会涉及到两个jsp页面,并会把两个jsp翻译成一个servlet,所以这两个jsp的指令(除pageEncoding和import之外)以及定义的变量名不能重复。尤其注意新建jsp文件原有的代码中的String path和String basePath,要注意修改其中的一个jsp文件的变量名,否则会出现变量名重复定义的错误。如下就是include includeTest2.jsp之后转换成的includeTest1_jsp类的源代码。

/*

* Generated by the Jasper component of Apache Tomcat

* Version: Apache Tomcat/8.5.9

* Generated at: 2018-10-20 13:08:44 UTC

* Note: The last modified time of this file was set to

* the last modified time of the source file after

* generation to assist with modification tracking.

*/

package org.apache.jsp.jspTest;

import javax.servlet.*;

import javax.servlet.http.*;

import javax.servlet.jsp.*;

import java.util.*;

import java.util.*;

public final class includeTest1_jsp extends org.apache.jasper.runtime.HttpJspBase

implements org.apache.jasper.runtime.JspSourceDependent,

org.apache.jasper.runtime.JspSourceImports {

private static final javax.servlet.jsp.JspFactory _jspxFactory =

javax.servlet.jsp.JspFactory.getDefaultFactory();

private static java.util.Map _jspx_dependants;

static {

_jspx_dependants = new java.util.HashMap(1);

_jspx_dependants.put("/jspTest/includeTest2.jsp", Long.valueOf(1540040843018L));

}

private static final java.util.Set _jspx_imports_packages;

private static final java.util.Set _jspx_imports_classes;

static {

_jspx_imports_packages = new java.util.HashSet<>();

_jspx_imports_packages.add("javax.servlet");

_jspx_imports_packages.add("java.util");

_jspx_imports_packages.add("javax.servlet.http");

_jspx_imports_packages.add("javax.servlet.jsp");

_jspx_imports_classes = null;

}

private volatile javax.el.ExpressionFactory _el_expressionfactory;

private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;

public java.util.Map getDependants() {

return _jspx_dependants;

}

public java.util.Set getPackageImports() {

return _jspx_imports_packages;

}

public java.util.Set getClassImports() {

return _jspx_imports_classes;

}

public javax.el.ExpressionFactory _jsp_getExpressionFactory() {

if (_el_expressionfactory == null) {

synchronized (this) {

if (_el_expressionfactory == null) {

_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();

}

}

}

return _el_expressionfactory;

}

public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {

if (_jsp_instancemanager == null) {

synchronized (this) {

if (_jsp_instancemanager == null) {

_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());

}

}

}

return _jsp_instancemanager;

}

public void _jspInit() {

}

public void _jspDestroy() {

}

public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)

throws java.io.IOException, javax.servlet.ServletException {

final java.lang.String _jspx_method = request.getMethod();

if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {

response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD");

return;

}

final javax.servlet.jsp.PageContext pageContext;

javax.servlet.http.HttpSession session = null;

final javax.servlet.ServletContext application;

final javax.servlet.ServletConfig config;

javax.servlet.jsp.JspWriter out = null;

final java.lang.Object page = this;

javax.servlet.jsp.JspWriter _jspx_out = null;

javax.servlet.jsp.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 path1 = request.getContextPath();

String basePath1 = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path1+"/";

out.write("\r\n");

out.write("\r\n");

out.write(""-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");

out.write("\r\n");

out.write("

\r\n");

out.write("

out.print(basePath1);

out.write("\">\r\n");

out.write(" \r\n");

out.write("

My JSP 'includeTest.jsp' starting page\r\n");

out.write(" \r\n");

out.write("\t\r\n");

out.write("\t\r\n");

out.write("\t \r\n");

out.write("\t\r\n");

out.write("\t\r\n");

out.write("\t\r\n");

out.write("\r\n");

out.write(" \r\n");

out.write(" \r\n");

out.write("

\r\n");

out.write("

\"includeTest1.jsp's content\"

\r\n");

out.write(" ");

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(""-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");

out.write("\r\n");

out.write("

\r\n");

out.write("

out.print(basePath);

out.write("\">\r\n");

out.write(" \r\n");

out.write("

My JSP 'includeTest2.jsp' starting page\r\n");

out.write(" \r\n");

out.write("\t\r\n");

out.write("\t\r\n");

out.write("\t \r\n");

out.write("\t\r\n");

out.write("\t\r\n");

out.write("\t\r\n");

out.write("\r\n");

out.write(" \r\n");

out.write(" \r\n");

out.write("

\r\n");

out.write("

\"includeTest2.jsp's content\"

\r\n");

out.write(" \r\n");

out.write("\r\n");

out.write("\r\n");

out.write(" \r\n");

out.write("\r\n");

} catch (java.lang.Throwable t) {

if (!(t instanceof javax.servlet.jsp.SkipPageException)){

out = _jspx_out;

if (out != null && out.getBufferSize() != 0)

try {

if (response.isCommitted()) {

out.flush();

} else {

out.clearBuffer();

}

} catch (java.io.IOException e) {}

if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);

else throw new ServletException(t);

}

} finally {

_jspxFactory.releasePageContext(_jspx_page_context);

}

}

}

可以看到,includeTest1.jsp和includeTest2.jsp页面的内容都使用out.write输出到浏览器了。运行includeTest1.jsp后,显示如下的结果:

f74ab8903a6eb7c77a3cf1feb14f583c.png

使用@include可以包含任意的内容,文件的后缀是什么都无所谓。这种把别的文件内容包含到自身页面的@include语句就叫作静态包含,作用只是把别的页面内容包含进来,属于静态包含。

3.2 jsp:include指令

接jsp标签。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值