学习error-page的配置
当访问服务器资源返回自己的404提示
输入任意服务器不存在的资源
效果:
:
实力代码:
404.Jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>this is 404 page</title>
</head>
<body>
This is 404 page.你访问的资源不存在 <br>
</body>
</html>
web.xml的配置为:
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
当访问服务器资源返回自己的500提示
输入错误页面服务器转向设定好的500提示页面
效果:
代码:
输入任意服务器不存在的资源
500.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<html>
<head>
<title>My JSP '500.jsp' starting page</title>
</head>
<body>
This is 500 JSP page.管理员不在,请稍候访问<br>
</body>
</html>
Web.xml配置
<error-page>
<error-code>500</error-code>
<location>/500.jsp</location>
</error-page>
错误页面为
Errpagefor500.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<title>使程序出现问题转向500页面</title><%=exception.getMessage()%>
</head>
<body>
使程序出现问题转向500页面<br><%num %>
</body>
</html>
pageContext.findAttribute("rname")
findAttribute(name);中的name值会一个一个的找下去一直找到 ,找不到return null
代码实力:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<%//jsp中的四种范围 演示page范围 page.setAttribute()
pageContext.setAttribute("pname", "pvalue");
request.setAttribute("rname","rvalue");
session.setAttribute("sname", "svalue");
application.setAttribute("aname", "aobject");
%>
<%=pageContext.getAttribute("rname")%><br>
<h3>通过pageContext的findAttribute()方法操作</h3>
<%=pageContext.findAttribute("rname") %> <br>
<%=session.getAttribute("sname") %>
</body>
</html>
jsp内置对象一共九个
Request-----------HttpServletReaquest
Response---------HttpServletReaponse
Session-----------HttpSession
Application-----servletContext
Config---------ServletConfig
Out----------JspWriter
Exception---------Throwable
Page--------------Object
pageContext---------pageContext
1.代码
2.效果:
代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>内置对下像</title>
</head>
<body>
<a href="<%=basePath%>">标点</a> <br>
<%=basePath%><br>
<%=path %>
</body>
</html>
2.include指令
它是用于包含页面.
格式 <%@ include file="" %>
这个属性指示的是当前要包含的页面.
它是将被包含页面整体上添加到包含页面中,一起进行翻译,只有包含页面才会被翻译成java文件。
注意:在开发,一般情况下,被包含的页面中只写要包含内容。
代码实现:
1.Jsp
<%num%>
2.Jsp
<%num1%>
Include.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>this a wab is 1.jsp add 2.jsp </title>
</head>
<body>
<%@ include file="1.jsp"%>
<%@ include file="2.jsp" %>
<%=num+num1%>
</body>
</html>
指令集page
效果:
代码
Page.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'page.jsp' starting page</title>
</head>
<body>
<%=request.getScheme()%><br>
request.getServerName()=<%=request.getServerName() %><br>
request.getServerPort()=<%=request.getServerPort() %><br>
request.getContextPath()=<%=request.getContextPath() %><br>
<%request.setAttribute("name", "xiaoming");%>
<%=request.getAttribute("name") %><br>
${requestScope.name}
</body>
</html>