当前位置: 首页 > article >正文

setAttribute、getAttribute、getParameter方法的用法 /// Session的getSession()方法的使用小结

setAttribute、getAttribute、getParameter

  • 一、setAttribute、getAttribute、getParameter
    • **例如: servlet从请求页面中通过getParameter来获得参数**
    • **例如:跳转页面通过getAttribute来获得请求的属性
    • 二、Session的getSession()方法的使用小结

一、setAttribute、getAttribute、getParameter

getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型
getParameter表示接收参数,参数为页面提交的参数,包括:表单提交的参数、URL重写(就是xxx?id=1中的id)传的参数等,因此这个并没有设置参数的方法(没有setParameter),而且接收参数返回的不是Object,而是String类型

例如: servlet从请求页面中通过getParameter来获得参数

请求页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>查询点击页面</title>
</head>
<body>
<form action="/userList"><p><input type="text" value="queryAll" name="type"></p><p><input type="submit" value="点我查询所有用户"></p>
</form>
</body>
</html>

servlet类:

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {**String type = req.getParameter("type");**if ("queryAll".equals(type)){queryAll(req,resp);}

**例如:跳转页面通过getAttribute来获得请求的属性

		而该属性必须是要在servlet中通过setAttribute设置过的**

servlet类设置属性:

private void queryAll(HttpServletRequest req, HttpServletResponse resp){
ArrayList users = service.queryAll();
HttpSession session = req.getSession();
session.setAttribute( “users” ,users);
try {
resp.sendRedirect( " /login/userList.jsp");
}catch ( IOException e) {
e.printStackTrace();
}

页面接收属性:

<%
Object users = session.getAttribute("users");
if(null!=users&&users instanceof ArrayList){ArrayList<User> userList=(ArrayList< User>)users;for (User user : userList) {%>
<tr><td><%=user.getUser_name()%></td><td><%=user.getUser_name()%></td><td><%=user.getPassword()%></td>
</tr>
<%}
}
%>

HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别:

(1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法

(2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:

authenticate.jsp

或者:

请输入用户姓名:

在authenticate.jsp中通过request.getParameter(“username”)方法来获得请求参数username:

<% String username=request.getParameter(“username”); %>

(3)当两个Web组件之间为转发关系时,转发目标组件通过getAttribute()方法来和转发源组件共享request范围内的数据。假定 authenticate.jsp和hello.jsp之间为转发关系。authenticate.jsp希望向hello.jsp传递当前的用户名字, 如何传递这一数据呢?先在authenticate.jsp中调用setAttribute()方法:

<%
String username=request.getParameter(“username”);
request.setAttribute(“username”,username);
%>

getAttribute是返回对象,getParameter返回字符串

二、Session的getSession()方法的使用小结

getSession()
HttpServletRequest.getSession(ture) 与 HttpServletRequest.getSession() 是一个意思;

HttpServletRequest.getSession(false) 等同于:如果当前Session没有就为null;

Session在网络应用中被称为会话。

具体到web中的Session指的就是用户在浏览某个网站时,从进入网站到浏览器关闭所经过的这段时间,也就是用户浏览这个网站所花费的时间,因此从概述上我们可以看到,session实际上是一个特定的时间概念。

需要注意的是:一个session的概念需要包括特定的客户端,特定的服务器端以及不中断的操作时间。A用户和C服务器建立连接时所处的session同B用户和C服务器建立连接时所处的Session是两个不同的session。

Session的工作原理:
(1)当一个session第一被启动时,一个唯一的标识被存储与本地的cookie中;
(2)首先使用session_start()函数,从session仓库中加载已经存储的session变量。

HttpRequest对象有两种形式的getSession的方法调用:

getSession()
getSession(boolen isNew)
这样,前者会检测当前时候是否有session存在,如果不存在则创建一个,如果存在就返回当前的。

因此,getSession( )相当于getSession(true);

参数为true时,若存在会话,则返回该会话,否则新建一个会话;
参数为false时,如存在会话,则返回该会话,否则返回NULL;

http://www.lryc.cn/news/2414137.html

相关文章:

  • 开源人脸识别项目 —— face_recognition
  • rx 文件管理器_免费开源资源管理器——RX 文件管理器
  • asp毕业设计——基于asp+access的教师信息管理系统设计与实现(毕业论文+程序源码)——教师信息管理系统
  • 通过JavaEye2.0网站看ruby on rails性能
  • QQ快速登录协议分析
  • 【RDMA】技术详解(一):RDMA概述
  • 面试专区|【60道计算机网络高频题整理(附答案背诵版)】
  • Codeigniter 框架开启PDO查询方式、多库连接实现、多语言网站开发配置、以及捕获页面最后报错
  • java语言技术_Java语言四大核心技术详解
  • WEB前端开发准备-Atom编辑器使用说明 Atom常用插件推荐 Atom快捷键
  • GUI Design Studio 4.5.151.0原型设计工具的使用
  • DOM4J 知识详解
  • 简单了解cms(内容管理系统)
  • [Visual Studio 2022 C#]设置splitContainer拆分器中间分隔条splitter的颜色和宽度
  • 【reverse】通俗易懂的gcc内联汇编入门+示例:实现花指令
  • C#窗体应用程序之CheckListBox复选列表与ListBox列表框控件
  • netframework有什么用_C#初学者教程系列1:什么是.NET Framework?
  • 使用fastdb的感受
  • 对SHFileOperation函数的一点研究
  • Java(API)——正则表达式
  • FieldTrip toolbox教程系列(0)-安装、配置与测试
  • linux 升级 java 8,生产环境 JDK6 升级至 JDK8
  • ExcuteReader详解
  • CImage拷贝到另一个CImage,两个方法
  • 备战数学建模4-MATLAB绘制三维图形
  • MatchPuppy:狗狗相亲网站
  • Request.QueryString
  • 电脑蓝屏怎么办 七大原因及解决办法来帮你
  • MATLAB实现随机数方法合集
  • 1、Intent和IntentFilter详解