47session 方法

本文详细介绍了在Java Web开发中使用Session对象进行会话管理的过程。通过两个页面的示例,展示了如何设置Session的最大期限,如何在Session中存储和读取属性,以及如何在不同页面间共享Session数据。

定义一个page1

<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>session</title>
</head>
<body>
<h1>session对象</h1>
<%
	SimpleDateFormat ndf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
	 Date d=new Date(session.getCreationTime());
	 session.setAttribute("username","admin");
	 session.setAttribute("password","123456");
	 session.setAttribute("age","geyao");
	 //设置session的最大期限为十秒
	 session.setMaxInactiveInterval(10);
%>
<hr>
Session创建时间: <%=ndf.format(d) %><br>
Session的ID编号:<%=session.getId() %><br>
从Session中获取用户名:<%=session.getAttribute("username") %><br>
<a href="session_page2.jsp" target="_blank">跳转到page2</a>
</body>
</html>

定义一个page2

<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>session</title>
</head>
<body>
<h1>session对象</h1>

<hr>

Session的ID编号:<%=session.getId() %><br>
从Session中获取用户名:<%=session.getAttribute("username") %><br>
Session中保存的属性:<%
	String[] name=session.getValueNames();
	for(int i=0;i<name.length;i++){
		out.println(name[i]+"&nbsp;"); 
	}
%>
</body>
</html>

运行结果

过十秒

代码中 `session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").setCurrentCell 2, "ISMNE"` 报错可能有以下几种原因及对应的解决办法: ### 原因1:对象未正确加载 在执行 `setCurrentCell` 方法时,可能对象还未完全加载到界面上,导致无法找到该对象。 解决办法:在调用 `setCurrentCell` 方法之前,添加适当的等待时间,确保对象已经加载完成。可以使用 `Application.Wait` 函数来实现等待。 ```vba Application.ScreenUpdating = True Application.WindowState = xlMinimized session.findById("wnd[0]").maximize session.findById("wnd[0]/tbar[0]/okcd").Text = "iw47" session.findById("wnd[0]").sendVKey 0 session.findById("wnd[0]/usr/chkDY_IAR").Selected = True session.findById("wnd[0]/usr/chkDY_ABG").Selected = True session.findById("wnd[0]/usr/ctxtAUFNR_O-LOW").SetFocus session.findById("wnd[0]/usr/ctxtAUFNR_O-LOW").caretPosition = 0 session.findById("wnd[0]/usr/btn%_AUFNR_O_%_APP_%-VALU_PUSH").press session.findById("wnd[1]/tbar[0]/btn[24]").press session.findById("wnd[1]/tbar[0]/btn[8]").press session.findById("wnd[0]/usr/ctxtERSDA_C-LOW").Text = "" session.findById("wnd[0]/usr/ctxtERSDA_C-HIGH").Text = "" session.findById("wnd[0]/usr/ctxtERSDA_C-HIGH").SetFocus session.findById("wnd[0]/usr/ctxtERSDA_C-HIGH").caretPosition = 0 session.findById("wnd[0]/tbar[1]/btn[8]").press ' 等待一段时间,确保对象加载完成 Application.Wait (Now + TimeValue("0:00:05")) session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").setCurrentCell 2, "ISMNE" session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItem "&XXL" session.findById("wnd[1]/tbar[0]/btn[0]").press ``` ### 原因2:列名或行索引无效 `setCurrentCell` 方法的参数可能存在问题,例如指定的列名 `"ISMNE"` 不存在,或者行索引 `2` 超出了表格的范围。 解决办法:在调用 `setCurrentCell` 方法之前,先检查表格的列名和行数,确保参数的有效性。可以通过打印表格的列名和行数来进行调试。 ```vba Application.ScreenUpdating = True Application.WindowState = xlMinimized session.findById("wnd[0]").maximize session.findById("wnd[0]/tbar[0]/okcd").Text = "iw47" session.findById("wnd[0]").sendVKey 0 session.findById("wnd[0]/usr/chkDY_IAR").Selected = True session.findById("wnd[0]/usr/chkDY_ABG").Selected = True session.findById("wnd[0]/usr/ctxtAUFNR_O-LOW").SetFocus session.findById("wnd[0]/usr/ctxtAUFNR_O-LOW").caretPosition = 0 session.findById("wnd[0]/usr/btn%_AUFNR_O_%_APP_%-VALU_PUSH").press session.findById("wnd[1]/tbar[0]/btn[24]").press session.findById("wnd[1]/tbar[0]/btn[8]").press session.findById("wnd[0]/usr/ctxtERSDA_C-LOW").Text = "" session.findById("wnd[0]/usr/ctxtERSDA_C-HIGH").Text = "" session.findById("wnd[0]/usr/ctxtERSDA_C-HIGH").SetFocus session.findById("wnd[0]/usr/ctxtERSDA_C-HIGH").caretPosition = 0 session.findById("wnd[0]/tbar[1]/btn[8]").press ' 获取表格对象 Dim grid As Object Set grid = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell") ' 检查列名是否存在 Dim colNames As Variant colNames = grid.ColumnNames Dim colExists As Boolean colExists = False For Each colName In colNames If colName = "ISMNE" Then colExists = True Exit For End If Next colName If Not colExists Then MsgBox "列名 'ISMNE' 不存在!" Exit Sub End If ' 检查行索引是否有效 Dim rowCount As Long rowCount = grid.RowCount If 2 > rowCount Then MsgBox "行索引 2 超出了表格的范围!" Exit Sub End If grid.setCurrentCell 2, "ISMNE" grid.contextMenu grid.selectContextMenuItem "&XXL" session.findById("wnd[1]/tbar[0]/btn[0]").press ``` ### 原因3:对象不支持 `setCurrentCell` 方法 `session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell")` 返回的对象可能不支持 `setCurrentCell` 方法。 解决办法:检查对象的类型,确保该对象支持 `setCurrentCell` 方法。可以通过打印对象的类型来进行调试。 ```vba Application.ScreenUpdating = True Application.WindowState = xlMinimized session.findById("wnd[0]").maximize session.findById("wnd[0]/tbar[0]/okcd").Text = "iw47" session.findById("wnd[0]").sendVKey 0 session.findById("wnd[0]/usr/chkDY_IAR").Selected = True session.findById("wnd[0]/usr/chkDY_ABG").Selected = True session.findById("wnd[0]/usr/ctxtAUFNR_O-LOW").SetFocus session.findById("wnd[0]/usr/ctxtAUFNR_O-LOW").caretPosition = 0 session.findById("wnd[0]/usr/btn%_AUFNR_O_%_APP_%-VALU_PUSH").press session.findById("wnd[1]/tbar[0]/btn[24]").press session.findById("wnd[1]/tbar[0]/btn[8]").press session.findById("wnd[0]/usr/ctxtERSDA_C-LOW").Text = "" session.findById("wnd[0]/usr/ctxtERSDA_C-HIGH").Text = "" session.findById("wnd[0]/usr/ctxtERSDA_C-HIGH").SetFocus session.findById("wnd[0]/usr/ctxtERSDA_C-HIGH").caretPosition = 0 session.findById("wnd[0]/tbar[1]/btn[8]").press ' 获取表格对象 Dim grid As Object Set grid = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell") ' 打印对象的类型 Debug.Print TypeName(grid) ' 检查对象是否支持 setCurrentCell 方法 On Error Resume Next Call grid.setCurrentCell(2, "ISMNE") If Err.Number <> 0 Then MsgBox "对象不支持 setCurrentCell 方法!" Exit Sub End If On Error GoTo 0 grid.contextMenu grid.selectContextMenuItem "&XXL" session.findById("wnd[1]/tbar[0]/btn[0]").press ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端小歌谣

放弃很容易 但是坚持一定很酷

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值