JAVA-WEB 动态页面技术(JSP/EL/JSTL)--- jsp代码演示

demo页面

<%--报错如何跳转到错误页面 --%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" errorPage="error.jsp" isErrorPage="true"%>
<%--错误页面跳转 --%>

<%--演示如何导入java包,--%>
<%@ page import="java.util.*"%>
<!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>demo1</title>
</head>
<body>

	<%--静态包含一个jsp文件 --%>
	<%@ include file="/header.jsp"%>

	<%--报错如何跳转到错误页面 --%>
	<%
		List list = new ArrayList();
		request.setAttribute("test", "testTex1t");
	%>
	<%--演示错误页面 --%>
	<%--<%=1/0 --%>


	<%--jsp 9个内置/隐式对象 --%>
	1.out
	<%
		out.println("out对象");
	%>
	<br>
	<hr>
	2.pageContext
	<%
		//向pagecontext域存取数据
		pageContext.setAttribute("pageContextText", "pageContextText");
		pageContext.setAttribute("name", "alex");
	%>
	<%=pageContext.getAttribute("pageContextText")%>

	<%
		//向其他域存取数据
		pageContext.setAttribute("sessionTest", "sessionTest", pageContext.SESSION_SCOPE);
		pageContext.setAttribute("name", "alex2", pageContext.SESSION_SCOPE);
	%>
	<%=pageContext.getAttribute("sessionTest", pageContext.SESSION_SCOPE)%>
	<%=session.getAttribute("sessionTest")%>

	<%
		//findAttribute(Stringname)---依次从pageContext域,request域,session域,application域中获	取属性,在某个域中获取后将不在向后寻找
		String name = (String) pageContext.findAttribute("name");
	%>
	<%=name%>

	<%
		//获得其他域对象,例如
		HttpSession isession = pageContext.getSession();
		ServletRequest irequest = pageContext.getRequest();
	%>
	<br>
	<hr>
	3.request
	<%
		request.setAttribute("requestTest", "requestValue");
	%>
	<br>
	<hr>
	4.response
	<%
		response.setCharacterEncoding("UTF-8");
	%>
	<br>
	<hr>
	5.config
	<%=config.getServletContext().getInitParameter("initTestText")%>
	<br>
	<hr>
	6.session
	<%=session.getAttribute("sessionTest")%>
	<br>
	<hr>
	7.application
	<%
		//application=ServletContext
		application.setAttribute("appSet", "appSetValue");
		application.getAttribute("appSet");
	%>
	<br>
	<hr>
	8.page
	<%=page.toString()%>
	<br>
	<hr>
	9.exception 设置isErrorPage="true"才可获得
	<br>
	<hr>
	<%
		application.setAttribute("jspInclude", "jspIncludeValue");
	%>	
	<br>
	<hr>
	动态包含,适合jsp文件	<br>
	<jsp:include page="/jsp_include.jsp" />


</body>
</html>


错误跳转页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
    <%--设置isErrorPage="true" jsp内置/隐式对象会出现exception对象 --%>
<!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>error</title>
</head>
<body>
	<h1>错误页面!</h1>
	<%=exception.fillInStackTrace()%>
</body>
</html>

测试动态包含页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

	<%=application.getAttribute("jspInclude") %>


猜你喜欢

转载自blog.csdn.net/alexzt/article/details/80894027
今日推荐