SessionTest

来源:互联网 发布:网络自制综艺节目抄袭 编辑:程序博客网 时间:2024/06/06 02:47

这是我第一次自学JSP 内部对象Session 的例子:
F:/Tomcat 5.0/webapps/ROOT/session
main.htm
session1.jsp
session2.jsp

1。main.htm

<html>
<head><title>session 应用1</title></head>
<body>
<form method=post action="session1.jsp">
请输入出版社名称:
<input type=text name="yourname">
<input type=submit value="确定">
<input type=reset value="重来">
</form>
</body>
</html>


2。session1.jsp

<%--测试application 对象--%>
<%
application.setAttribute("app","你好,application");
%>

<html>
<head><title>session 应用2</title></head>
<body>

<%--测试session 对象--%>
<%
String name1 = request.getParameter("yourname");
session.putValue("username",name1);
%>
出版社的名称是:<%= name1 %>
<p>

<form method=post action = "session2.jsp">
最近新出版的书是什么?
<input type=text  name="book">
<p>
<input type=submit value="确定">
<input type=reset value="重来">
</form>

</body>
</html>


3。session2.jsp

<html>
<head><title>session 应用3</title></head>
<body>

<%! String book = "";%>

<!--测试session对象和application 对象-->
<%
String book = request.getParameter("book");
String name2 =(String) session.getValue("username");

String appname = (String) application.getAttribute("app");
%>

出版社的名称是:<%= name2 %>
<p>
最近新出版的书是:<%= book %>
<p>
Application 对象赋值String 测试:<%= appname %>

</body>
</html>

原创粉丝点击