通过servletCOnfig获取servlet配置的初始化参数
<servlet> <servlet-name>ServletDemo4</servlet-name> <servlet-class>cn.itcast.web.servlet.ServletDemo4</servlet-class> <init-param> <!-- servletConfig --> <param-name>charset</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <!-- servletConfig --> <param-name>xxx</param-name> <param-value>yyy</param-value> </init-param> </servlet>
ServletConfig config = this.getServletConfig(); //获取配置的初始化参数的方式1 //String value = config.getInitParameter("charset"); Enumeration e = config.getInitParameterNames(); while(e.hasMoreElements()){ String name = (String) e.nextElement(); String value = config.getInitParameter(name); System.out.println(name + "=" + value); }
通过servletContexg,获取为web应用配置的初始化参数
<!-- 为web应用设置初始化参数,这些设置的初始化参数可以通过servletContext对象的getInitParameter()方法获取 --> <context-param> <param-name>url</param-name> <param-value>jdbc:mysql://localhost:3306/test</param-value> </context-param> <context-param> <param-name>username</param-name> <param-value>root</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>root</param-value> </context-param>
//String value = this.getServletContext().getInitParameter("xxx"); String url = this.getServletContext().getInitParameter("url"); String username = this.getServletContext().getInitParameter("username"); String password = this.getServletContext().getInitParameter("password");
通过servletContext获取文件的mime类型
String filename = "1.jpg"; ServletContext context = this.getServletContext(); System.out.println(context.getMimeType(filename)); response.setHeader("content-type", "image/jpeg");
获取web.xml文件中配置的web应用的显示名称
<display-name>ProjectName</display-name>
String name = this.getServletContext().getServletContextName();
//设置浏览器的缓存
long expriestime = System.currentTimeMillis() + 1*24*60*60*1000;(one day) response.setDateHeader("expires", expriestime); String data = "adsdfsdfsdfsdfdsf"; response.getWriter().write(data);
--------------------------------------------------------------------------------
用servletContext读取web工程不同位置的资源文件
url=jdbc:mysql://localhost:3306/test username=root password=root
//读取配置文件的第一种方式 ServletContext context = this.getServletContext(); InputStream in = context.getResourceAsStream("/db.properties"); Properties prop = new Properties(); //map prop.load(in); String url = prop.getProperty("url"); String username = prop.getProperty("username"); String password = prop.getProperty("password");
//读取配置文件的第二种方式 ServletContext context = this.getServletContext(); String realpath = context.getRealPath("/db.properties"); //获取到操作文件名 String filename = realpath.substring(realpath.lastIndexOf("\\")+1); System.out.println("当前读到的文件是:" + filename); FileInputStream in = new FileInputStream(realpath); Properties prop = new Properties(); prop.load(in); String url = prop.getProperty("url"); String username = prop.getProperty("username"); String password = prop.getProperty("password");
//读取配置文件的第三种方式 ServletContext context = this.getServletContext(); URL url = context.getResource("/resource/db.properties"); InputStream in = url.openStream(); Properties prop = new Properties(); prop.load(in); String urlStr = prop.getProperty("url"); String username = prop.getProperty("username"); String password = prop.getProperty("password");
//读取src下面的配置文件 InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties"); Properties prop = new Properties(); //map prop.load(in); String url = prop.getProperty("url"); String username = prop.getProperty("username"); String password = prop.getProperty("password");
在web工程的普通java程序中如何读取资源文件
ClassLoader loader = StudentDao.class.getClassLoader(); URL url = loader.getResource("com/dao/db.properties"); String filepath = url.getPath(); FileInputStream in = new FileInputStream(filepath); Properties prop = new Properties(); //map prop.load(in); String dburl = prop.getProperty("url"); String username = prop.getProperty("username"); String password = prop.getProperty("password");