我们通过一个请求某个页面,返回500错误,跳转到一个友好页面来了解下Page指令
a.jsp中,<%@ page errorPage=”/error.jsp”%>说明如果此页面发生异常错误,会跳转到根目录下的error.jsp友好提示页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page errorPage="/error.jsp"%>
<!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>Insert title here</title>
</head>
<body>
<%
int i = 10 / 0;
%>
</body>
</html>
error.jsp中,isErrorPage=”true”利用这page指令,说明可以使用exception类,来获取错误的信息
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<!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>Insert title here</title>
</head>
<body>
this is error page
<%= exception.getMessage() %>
</body>
</html>
浏览器输入:
http://localhost:8080/day01/a.jsp
输出如下:
this is error page / by zero
所以说也就是一个转发的页面
以上的错误友好提示页面,因为error.jsp在WebContent目录下,所以说可以通过某个url进行直接访问error.jsp,但是呢?我们之所以设置一个error.jsp友好页面,主要是访问某个页面在异常出错的情况下,才跳转到该友好提示页面,所以呢?我们不能让用户直接进行访问
我们需要放在WEB-INF目录下,该目录是隐私的
我们只需要在a.jsp中,改成如下即可:
<%@ page errorPage="/WEB-INF/error.jsp"%>
我们再来测试下:
http://localhost:8080/day01/a.jsp
输出:
this is error page / by zero
哪怕我们在浏览器输入:
http://localhost:8080/day01/WEB-INF/error.jsp
也是无法访问的该友好页面的,这样就只能在异常情况下,去转发error.jsp页面了
以上的方法,我们还可以在下面这样配置:
web.xml
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/error.jsp</location>
</error-page>
a.jsp取消<%@ page errorPage=”/error.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>Insert title here</title>
</head>
<body>
<%
int i = 10 / 0;
%>
</body>
</html>
error.jsp不做变化
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<!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>Insert title here</title>
</head>
<body>
this is error page
<%= exception.getMessage() %>
</body>
</html>
我们再来测试下:
http://localhost:8080/day01/a.jsp
输出:
this is error page java.lang.ArithmeticException: / by zero
哪怕我们在浏览器输入:
http://localhost:8080/day01/WEB-INF/error.jsp
也是无法访问的该友好页面的,这样就只能在异常情况下,去转发error.jsp页面了
静态包含:
a.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>Insert title here</title>
</head>
<body>
a page
<%@ include file="b.jsp"%>
</body>
</html>
b.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>Insert title here</title>
</head>
<body>
b page
</body>
</html>
http://localhost:8080/day01/a.jsp
a page b page
动态包含:
a.jsp
<body>
a page
<jsp:include page="b.jsp"></jsp:include>
</body>
b.jsp
<body>
b page
</body>
http://localhost:8080/day01/a.jsp
a page b page
静态引入,动态引入的区别如下:
a.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>Insert title here</title>
</head>
<body>
<jsp:forward page="/b.jsp">
<jsp:param value="abcd" name="username" />
</jsp:forward>
</body>
</html>
b.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>Insert title here</title>
</head>
<body>
b page
<%=request.getParameter("username") %>
</body>
</html>
http://localhost:8080/day01/a.jsp
b page abcd
a.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>Insert title here</title>
</head>
<body>
<form action="b.jsp" method="post">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>
</body>
</html>
b.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>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
%>
username:<%=request.getParameter("username")%>
</body>
</html>
post方式request.setCharacterEncoding(“UTF-8”);
a.jsp
<body>
<form action="b.jsp" method="get">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>
</body>
b.jsp
<body>
<%
String val = request.getParameter("username");
String username = new String(val.getBytes("iso-8859-1"),"utf-8");
out.print(username);
%>
</body>
get方式,先解码,在编码,中文默认在传输的过程中使用的编码为ISO-8859-1
form方式提交既不是重定向也不是请求转发,因为它不是通过request.getRequestDispatcher() 或者 response.sendRedirect() 跳转的,就是一个普通的和超链接一样的跳转方式,不同的是 它会把form内所有的表单元素 全部提交到指定的页面所有就可以通过request.getParameter()获取参数了,同样 超链接后面的?的参数也是一样一样的。