• Servlet中的getServletContext()方法详解


    https://www.cnblogs.com/AXY228412/p/10967875.html

    下面总结servletcontext接口里面的方法:

    1.String getInitParameter(String name)    返回指定上下文范围的初始化参数值。
    2.Object getAttribute(String name)    返回servlet上下文中具有指定名字的对象,或使用已指定名捆绑一个对象。从Web应用的标准观点看,这样的对象是全局对象,因为它们可以被同一servlet在另一时刻访问。或上下文中任意其他servlet访问。
    这个方法我用过例子:(我在做计数网站曾经被登录过几次用过这个方法):
    下面是我的程序:
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            //1.获取数据
            String userName = request.getParameter("username");        //httpservletrequest里面的方法。获取请求头中的参数,
            String password = request.getParameter("password");
    //        System.out.println("userName="+userName+"password="+password);
            
            //2.校验数据
            
            PrintWriter pw = response.getWriter();    //用了这个输出流可以直接将文字写在浏览器上        
            
            
            if("admin".equals(userName) && "123".equals(password)) {
                pw.write("login success.....");
                //如果成功的话,就跳转到login_success.html那里
                
                //成功的次数累加:
                //保留之前存的值,然后在旧的值的基础上+1
                Object obj = getServletContext().getAttribute("count");        //getAttribute是servlet里面的方法
                
                
                //默认就是0次
                int totalCount = 0;
                if(obj != null) {
                    totalCount = (int)obj;
                }
                
                System.out.println("已登录成功的次数是:"+totalCount);
                
                
                //给这个count赋新的值
                getServletContext().setAttribute("count", totalCount+1);
                
                
                //跳到成功的页面
                //设置状态码,进行 重新定位状态码
                
                
                response.setStatus(302);
                
                //定位跳转的位置是哪一个页面
                response.setHeader("Location", "login_success.html");
                
            } else {
                pw.write("login failed.....");
            }
            
        }
    3.String getRealPath(String path)    给定一个URI,返回文件系统中URI对应的绝对路径。如果不能进行映射,返回null。
    下面是我写过的程序代码:
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            //获取servletcontext对象
            ServletContext context = this.getServletContext();
            //获取给定的文件在服务器上面的绝对路径
            String path = context.getRealPath("");
            
            System.out.println(path);    
     
    这个实际上输出的就是文件在电脑中的绝对路径,也就是读取web中的资源文件;
     
    3.void setAttribute(String name,Object obj)    设置servlet上下文中具有指定名字的对象。
     
    下面几个我还没用过,我用了以后有时间再补充吧:
    4.Enumeration getAttributeNames()    返回保存在servlet上下文中所有属性名字的枚举。       
    5.ServletContext getContext(String uripath)    返回映射到另一URL的servlet上下文。在同一服务器中URL必须是以“/”开头的绝对路径。
    6.Enumeration getInitParameterNames()    返回(可能为空)指定上下文范围的初始化参数值名字的枚举值。       
    7.int getMajorVersion()    返回此上下文中支持servlet API级别的最大和最小版本号。       
    8.int getMinorVersion()           
    9.String getMimeType(String fileName)    返回指定文件名的MIME类型。典型情况是基于文件扩展名,而不是文件本身的内容(它可以不必存在)。如果MIME类型未知,可以返回null。       
    10.RequestDispatcher getNameDispatcher(String name)    返回具有指定名字或路径的servlet或JSP的RequestDispatcher。如果不能创建RequestDispatch,返回null。如果指定路径,必须心“/”开头,并且是相对于servlet上下文的顶部。       
    11.RequestDispatcher getNameDispatcher(String path)           
    13.URL getResource(String path)    返回相对于servlet上下文或读取URL的输入流的指定绝对路径相对应的URL,如果资源不存在则返回null。       
    14.InputStream getResourceAsStream(String path)           
    15.String getServerInfo()    返顺servlet引擎的名称和版本号。       
    16.void log(String message)
    17.void log(String message,Throwable t)    将一个消息写入servlet注册,如果给出Throwable参数,则包含栈轨迹。       
    18.void removeAttribute(String name)    从servlet上下文中删除指定属性。
     
     
    --------------------- 
    作者:rnzhiw 
    来源:CSDN 
    原文:https://blog.csdn.net/rnzhiw/article/details/83349325 
    版权声明:本文为博主原创文章,转载请附上博文链接!
  • 相关阅读:
    【Qt开发】QTableWidget设置根据内容调整列宽和行高
    【Qt开发】Qt在QLabel(QWidget)鼠标绘制直线和矩形框
    【Qt开发】Qt在QLabel(QWidget)鼠标绘制直线和矩形框
    【Qt开发】Qt5.7串口开发
    【Qt开发】Qt5.7串口开发
    Oracle监听配置、数据库实例配置等
    SqlMapConfig.xml中的setting属性 Ibatis mybatis
    Hibernate的like用法
    eclipse逆向生成实体类注解方式或者xml方式
    struts2 ValueStack详解,页面获取值el表达式、ognl表达式
  • 原文地址:https://www.cnblogs.com/zaks/p/12859017.html
Copyright © 2020-2023  润新知