【 jsp】页面传递

首先介绍一下request共享:
test1.classTest:
package test1;

public class classTest {
    
          
private   String   name   =   null
          
          
public   classTest(){} 
          
          
public   void   setName(String   strName_p){    
          
this.name=strName_p;    
          }
    
          
public   String   getName(){    
          
return   this.name;    
          }
  

}


MyJsp.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@page import="test1.classTest"%>
<%
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 'MyJsp.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>
   
<jsp:useBean id="stringBean"  class="test1.classTest" scope="request"/>
   
<jsp:setProperty name="stringBean" property="name" value="request"/>
   
<%request.setAttribute("aa","aa"); %>
   
<jsp:include page="MyJsp1.jsp"></jsp:include>

  
</body>
</html>

MyJsp1.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@page import="test1.classTest"%>
<%
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 'MyJsp1.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>

 
<br>
 
<%classTest aa= (classTest)request.getAttribute("stringBean") ;%>
 
<%=aa.getName() %>
  
</body>
</html>
  • 如果用request,要注意request的范围仅限于同一个request对象;如果使用了form的"action"或者response.sendRedirect("xxx.jsp")等调转,则不再是同一个request对象,即用request.getAttribute("name")将取不到值。
  • 换成request.getRequestDispatcher("xxx.jsp").forward(request,response);
    才能共享request中数据
        所以关于request的传递必须是<jsp:include或者<jsp:forward以及
        RequestDispatcher dispatcher=request.getRequestDispatcher("../test1/MyJsp.jsp");
       dispatcher.forward(request, response);
       的形式

二session共享:

MyJsp.jsp:
   <jsp:useBean id="stringBean"  class="test1.classTest" scope="session"/>
   
<jsp:setProperty name="stringBean" property="name" value="session"/>
   
<%session.setAttribute("aa","aa"); %>
   
<a href="test2/MyJsp1.jsp">MyJsp1.jsp</a>

MyJsp1.jsp:
 <br>
 
<%classTest aa= (classTest)session.getAttribute("stringBean") ;%>
 
<%=aa.getName() %>
 
<br>
  
<%=session.getAttribute("aa"%>

三application共享:

MyJsp.jsp:
   <jsp:useBean id="stringBean"  class="test1.classTest" scope="application"/>
   
<jsp:setProperty name="stringBean" property="name" value="application"/>
   
<%getServletContext().setAttribute("aa","aa"); %>
   
<a href="test2/MyJsp1.jsp">MyJsp1.jsp</a>

MyJsp1.jsp:
 <br>
 
<%classTest aa= (classTest)getServletContext().getAttribute("stringBean") ;%>
 
<%=aa.getName() %>
 
<br>
  
<%=getServletContext().getAttribute("aa"%>

再介绍一下servlet和jsp页面的共享:

servlet1:
package test1;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class servlet1 extends HttpServlet {

    
/**
     * Constructor of the object.
     
*/

    
public servlet1() {
        
super();
    }


    
public void doGet(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {
           PrintWriter out 
= response.getWriter();
           out.println(
"Aa");
            classTest  value
=new classTest();
          String[] string
={"aa","bb"};
            
            value.setName(
"Request");
            request.setAttribute(
"keyRequest", value);
            request.setAttribute(
"aaa""aaaa");
            request.setAttribute(
"string", string);
            
            classTest  valueSession
=new classTest();
            valueSession.setName(
"Session");
            HttpSession  session
=request.getSession();
            session.setAttribute(
"keySession", valueSession);
            session.setAttribute(
"bbb""bbbb");
            
//session.setAttribute("string", string);
            
            classTest  valueApplication
=new classTest();
            valueApplication.setName(
"Application");
            getServletContext().setAttribute(
"valueApplication", valueApplication);
            getServletContext().setAttribute(
"ccc""cccc");
            
            RequestDispatcher dispatcher
=request.getRequestDispatcher("../test1/MyJsp.jsp");
            dispatcher.forward(request, response);
            
            
    }


}


MyJsp.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'MyJsp.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>
   
<jsp:useBean id="keyRequest" type="test1.classTest" scope="request"></jsp:useBean>
   
<jsp:getProperty name="keyRequest" property="name"/>
   
<br>

  
<jsp:useBean id="valueApplication" type="test1.classTest" scope="application"></jsp:useBean>
   
<jsp:getProperty name="valueApplication" property="name"/>
   
<br>
   $
{aaa}
     
<br>
   $
{bbb}
     
<br>
   $
{ccc}
        
<br>
   $
{keyRequest.name}
          
<br>
   $
{string[0]}
   
<br>
   
<% test1.classTest aa=(test1.classTest)session.getAttribute("keySession"); %>
   
<%=aa.getName() %>
           
<br>
   
<%=keyRequest.getName() %>

  
</body>
</html>


稍改一下serlvet
替换成:
out.println("<a href='../test1/MyJsp.jsp'>MyJsp.jsp</a>");

再看MyJsp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'MyJsp.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>
   
<jsp:useBean id="keyRequest"  type="test1.classTest" scope="request"/>//注意type与class的区别
   
<jsp:getProperty name="keyRequest" property="name"/>
   
  
<jsp:useBean id="valueApplication"  type="test1.classTest" scope="application"/>
  
<jsp:getProperty name="valueApplication" property="name"/>//必须引入userBean
 
<br>
  
<br>
   $
{aaa} //这样的写法简练,可以不需要前面的userBean,直接得到结果
     
<br>
   $
{bbb}
     
<br>
   $
{ccc}
        
<br>
   $
{keyRequest.name}
          
<br>
   $
{string[0]}
   
<br>
   
<% test1.classTest aa=(test1.classTest)session.getAttribute("keySession"); %>//这种形式不需要userBean但是较为麻烦
   
<%=aa.getName() %>
           
<br>
   
<%=keyRequest.getName() %>//这种形式引入上面的userBean
  
</body>
</html>
null Application


bbbb
cccc


Session
null
很明显request的结果就为null,session和application没有影响,和我们所说的jsp间的传递一样,
其实jsp也是sevlet。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值