JSP内置对象-Session

1 JSP session内置对象

在JSP中,session是HttpSession类型的内置对象。Java开发人员可以使用此对象设置,获取或删除属性或获取会话信息。

2 session内置对象的示例

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 session内置对象</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 session内置对象</title>
</head>
<body>

<%

    String name=request.getParameter("uname");
    out.print("欢迎 "+name);
    session.setAttribute("user",name);
%>

<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 session内置对象</title>
</head>
<body>
<%

    String name=(String)session.getAttribute("user");
    out.print("你好 "+name);

%>
</body>
</html>

2.4 运行测试

热门文章

优秀文章