Servlet中添加属性

Code:
  1. //向ServletContext中添加属性  
  2. package com.tsinghua;  
  3.   
  4. import javax.servlet.http.*;  
  5. import javax.servlet.*;  
  6. import java.io.*;  
  7.   
  8.   
  9. public class ServletContextTest1 extends HttpServlet{  
  10.   
  11.     //处理get请求  
  12.     //req:用于获得客户端(浏览器)的信息  
  13.     //res:用于向客户端(浏览器)返回信息  
  14.     public void doGet(HttpServletRequest req,HttpServletResponse res){  
  15.         //业务处理  
  16.         try{  
  17.             //处理中文乱码  
  18.             res.setContentType("text/html;charset=gbk");  
  19.             PrintWriter pw=res.getWriter();  
  20.               
  21.             //1、 得到ServletContext  
  22.             ServletContext sc=this.getServletContext();  
  23.             //2、 添加属性  
  24.             sc.setAttribute("myInfo","我是超超");  
  25.             pw.println("给ServletContext中添加一个属性myinfo,该值是一串字符串:我是超超");  
  26.               
  27.       
  28.         }catch(Exception e){  
  29.             e.printStackTrace();  
  30.         }  
  31.     }  
  32.     //处理Post请求  
  33.     public void doPost(HttpServletRequest req,HttpServletResponse res){  
  34.           
  35.         this.doGet(req,res);  
  36.     }  
  37. }  
原文地址:https://www.cnblogs.com/xcxinghai/p/3283913.html