zoukankan      html  css  js  c++  java
  • java web实例训练知识错误总结(二)

    一、servletContext理解

    1.点这里(详细)

    2.便于理解

    ServletContext详解
    2.ServletContext详解

    ServletContext详解

    1. //获取servletcontext,
    ServletContext servletcontext=this.getServletContext();
    //设置属性
    servletcontext.setAttribute("name", "ru");
    out.println("传递参数‘南成如’到name属性中");


    2.//获取servletcontext

    ServletContext servletcontext=this.getServletContext();
    //获取属性对象。
    String sc=(String)servletcontext.getAttribute("name");
    out.println("未删除属性前:"+sc);
    //删除属性对象
    servletcontext.removeAttribute("name");
    sc=(String)servletcontext.getAttribute("name");
    out.println("删除后:"+sc);
    3.用 ServletContext  读取properties文件
    ServletContext详解
    首先在webroot目录下创建properties,(new-file-dbinf.properties)
    //读取文件
    InputStream inputstream=this.getServletContext().getResourceAsStream("dbinf.properties");
    //通过Properties对象获取properties中的信息
    Properties properties=new Properties();
    properties.load(inputstream);
    //读取信息
    out.println("username="+properties.getProperty("username")+"password="+properties.getProperty("password"));
    注:如果dbinf.properties放在了src下,需要使用类加载器去加载。
    InputStream inputstream=classname.class.getClassLoader().getResourceAsStream("dbinf.properties");
    4. 读取文件的实际路径 
    //读取文件的实际路径
    String path=this.getServletContext().getRealPath("/images/IMG_4679.JPG");
    out.println("<img src='images/IMG_4679.JPG' width=600px height=500px>"+path);
    ServletContext详解

    二、requestdispatcher 的使用总结

    点这里

    三、

    对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)

  • 相关阅读:
    Linux的命令2
    运维书
    管理MariaDB
    MariaDB基础命令
    Linux创建桥接网络
    聚合网络
    kickstart
    VLAN原理
    进程优先和ACL
    计划任务at、crontab
  • 原文地址:https://www.cnblogs.com/cxy2016/p/6745382.html
Copyright © 2011-2022 走看看