servlet的 属性_Servlet:属性简介

servlet的 属性

An attribute is an object that is used to share information in a web app. Attribute allows Servlets to share information among themselves. Attributes can be SET and GET from one of the following scopes :

属性是用于在Web应用程序中共享信息的对象。 该属性允许Servlet之间共享信息。 可以从以下范围之一来设置和设置属性:

  1. request

    请求

  2. session

    会议

  3. application

    应用

setting and getting an attribute

Servlet:如何设置属性 (Servlet: How to SET an Attribute)

public void setAttribute(String name, Object obj) method is used to SET an Attribute.

public void setAttribute(String name, Object obj)方法用于设置属性。

Example demonstrating Setting Attribute

演示设置属性的示例

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class First extends HttpServlet {

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        ServletContext sc = getServletContext();
        sc.setAttribute("user","Abhijit");	//setting attribute on context scope
    }
}

Servlet:如何获取属性 (Servlet: How to GET an Attribute)

Object getAttribute(String name) method is used to GET an attribute.

Object getAttribute(String name)方法用于获取属性。

Example demonstrating getting a value of set Attribute

演示获取set Attribute值的示例

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Second extends  HttpServlet {

  protected void  doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        ServletContext sc = getServletContext();
 
        String str = sc.getAttribute("user");  //getting attribute from context scope
	
        out.println("Welcome"+str);  // Prints : Welcome Abhijit   
    }
}

翻译自: https://www.studytonight.com/servlet/attribute.php

servlet的 属性

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值