JSP传值方式


index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" session="true" isErrorPage="false" import="java.text.SimpleDateFormat"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="refresh" content="3">
</head>
<body>
当前日期是:<%@ include file="time.jsp" %>
<br>
<%=pageContext.getAttribute("admin") %>
<%=application.getAttribute("stu") %>
<a href="secondtime.jsp">第二个页面获取pagecontext</a>
<br>
<a href="thirdtime.jsp">第二个页面获取application</a>
<br>
<a href="4th.jsp">错误页面测试</a>
<br>
<a href="5th.jsp">插入页面测试</a>
<br>
<a href="6th.jsp">forward测试</a>
</body>
</html>

time.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" import="java.util.Date" import="java.text.SimpleDateFormat"%>
<%
Date date = new Date();
String pattern="yyyy-MM-dd hh:mm:ss";
SimpleDateFormat sdf=new SimpleDateFormat(pattern);
String strDate = sdf.format(date);

response.addIntHeader("Refresh",1);
pageContext.setAttribute("admin","123");
application.setAttribute("stu","456");
out.write("你好");
%>

<%=strDate %>

secondtime.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 'secondtime.jsp' starting page</title>
</head>
<body>
pageContext传值
<%=pageContext.getAttribute("admin") %>
</body>
</html>

thirdtime.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 'thirdtime.jsp' starting page</title>
</head>
<body>
application传值
<%=application.getAttribute("stu") %>
</body>
</html>

4th.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" errorPage="error.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP '4th.jsp' starting page</title>
</head>
<body>
错误页面
<%="错误页面" %>
<%Thread.sleep(3000); %>
<% 
int a=1;
int b=0;
int c=a/b;
%>
</body>
</html>

5th.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" errorPage="error.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP '5th.jsp' starting page</title>
</head>
<body>
<jsp:include page="form.jsp"></jsp:include>
</body>
</html>

6th.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" errorPage="error.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP '6th.jsp' starting page</title>
</head>
<body>
<jsp:forward page="7th.jsp"></jsp:forward>
</body>
</html>

7th.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" errorPage="error.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP '7th.jsp' starting page</title>
</head>
<body>
第七个页面
</body>
</html>

error.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 'error.jsp' starting page</title>
</head>
<body>
错误,大侠请重新来过
<a href="index.jsp">返回</a>
</body>
</html>

form.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 'form.jsp' starting page</title>
</head>
<body>
<form action="#" method="get">
姓名:<input type="text" name="name">
<br>
密码:<input type="text" name="pwd">
<br>
<input type="submit" value="提交">
</form>
</body>
</html>

声明:子客|版权所有,违者必究|如未注明,均为原创|网站采用BY-NC-SA协议授权

转载:请注明原文链接 - JSP传值方式


Carpe Diem and Do what I like