zoukankan      html  css  js  c++  java
  • 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. }  
  • 相关阅读:
    Java设计模式(Design Patterns)
    P2213 [USACO14MAR]懒惰的牛The Lazy Cow_Sliver
    P3120 [USACO15FEB]牛跳房子(金)Cow Hopscotch (Gold)
    P4818 [USACO15DEC]Bessie's Dream 贝西的梦
    P3667 [USACO17OPEN]Bovine Genomics
    P4379 [USACO18OPEN]Lemonade Line
    P4378 [USACO18OPEN]Out of Sorts S
    P4089 [USACO17DEC]The Bovine Shuffle
    P4269 [USACO18FEB]Snow Boots G
    P4086 [USACO17DEC]My Cow Ate My Homework
  • 原文地址:https://www.cnblogs.com/xcxinghai/p/3283913.html
Copyright © 2011-2022 走看看