JSP实验6:JavaBean的应用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/jjsjsjjdj/article/details/102594966

一、JavaBean

1.Count.java

package bean;
public class Counter{
	public Counter(){		
	}
	private int Count=0;
	public void setCounter(int count) {
		Count = count;
	}
	
	public int getCounter( ) {
	    return ++Count;
	}
}

二、page、request、session、application

1.page.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'usingCounter.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
     <div><h2><jsp:useBean id="count" scope="page" class="bean.Counter"></jsp:useBean> 
     <font color="red">演示:page</font></h2></div>
     <br>
     <br>
          您是本站第
          <font color=blue>
              <jsp:getProperty name="count" property="counter"/>
          </font>
          位参观者
  </body>
</html>

2.request

2.1 request.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'usingCounter.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
     <div><h2><jsp:useBean id="count" scope="request" class="bean.Counter"></jsp:useBean> 
     <font color="red">演示:request</font></h2></div>
     <br>
     <br>
          您是本站第
          <font color=blue>
              <jsp:getProperty name="count" property="counter"/>
          </font>
          位参观者
     <jsp:forward page="2.request2.jsp"></jsp:forward>
  </body>
</html>

2.2 request2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'usingCounter.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
     <div><h2><jsp:useBean id="count" scope="request" class="bean.Counter"></jsp:useBean> 
     <font color="red">演示:request</font></h2></div>
     <br>
     <br>
          您是本站第
          <font color=blue>
              <jsp:getProperty name="count" property="counter"/>
          </font>
          位参观者
     <jsp:forward page="2.request2.jsp"></jsp:forward>
     <br>
     <!-- <a href="2.request2.jsp">链接转跳到2.request2.jsp</a> -->
  </body>
</html>

3.session.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'usingCounter.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
     <div><h2><jsp:useBean id="count" scope="session" class="bean.Counter"></jsp:useBean> 
     <font color="red">演示:session</font></h2></div>
     <br>
     <br>
          您是本站第
          <font color=blue>
              <jsp:getProperty name="count" property="counter"/>
          </font>
          位参观者
  </body>
</html>

4.application.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'usingCounter.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
     <div><h2><jsp:useBean id="count" scope="application" class="bean.Counter"></jsp:useBean> 
     <font color="red">演示:application</font></h2></div>
     <br>
     <br>
          您是本站第
          <font color=blue>
              <jsp:getProperty name="count" property="counter"/>
          </font>
          位参观者
  </body>
</html>

三、运用javaBean比较page、request、session、application的生存周期

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/jjsjsjjdj/article/details/102594966