JSP内置对象-PageContext

1 JSP pageContext内置对象

在JSP中,pageContext是PageContext类型的内置对象。pageContext对象可用于设置,获取或删除以下作用域范围的属性:

  • page
  • request
  • session
  • application

在JSP中,page域范围是默认范围。

2 pageContext内置对象的示例

2.1 编写index.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<html>
<head>
    <meta charset="utf-8">
    <title>一点教程网-JSP pageContext内置对象</title>
</head>
<body>
<form action="welcome.jsp">
    <input type="text" name="uname">
    <input type="submit" value="提交"><br/>
</form>
</body>
</html>

2.2 编写welcome.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>一点教程网-JSP pageContext内置对象</title>
</head>
<body>

<%

    String name=request.getParameter("uname");
    out.print("欢迎 "+name);

    pageContext.setAttribute("user",name,PageContext.SESSION_SCOPE);

%>

<a href="second.jsp">second.jsp页面</a>

</body>
</html>

2.3 编写second.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>一点教程网-JSP pageContext内置对象</title>
</head>
<body>
<%

    String name=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE);
    out.print("你好 "+name);

%>
</body>
</html>

2.4 运行测试

热门文章

优秀文章