jsp读取配置文件信息|JSP页面怎样调用Spring配置文件中定义的Bean

Ⅰ jsp页面可以做为配置文件吗

可以 只要是文件就可以

Ⅱ 如何设计jsp页面读取配置文件

package com.ydq.read.propertyfile; 2. 3.import java.io.InputStream; 4.import java.util.MissingResourceException; 5.import java.util.Properties; 6.import java.util.ResourceBundle; 7. 8./** 9. * 读取配置文件中的参数 10. * 11. * 一般情况下,首页的内容是比较比较多,但内容结构大多也有本同的地方,比方说某几块内容都是展示的文章,只是文章来源的类别不同,或者说某几个版面都展示商品, 12. * 只是商品所属分类不同罢了,那么我们在后台取数据时,都需要根据类别ID来取值的。一般做法都是把这些不同的来源ID在代码中写好,这种做法又分为以下三种情况: 13. * 14. * 1、前台写死ID,通过<jsp:include page="xx.do?id=xx">,可以进行动态的包含内容。 15. * 16. * 2、可在service层写一个id的数组,String[] ids = 17. * {“”,"",""};用ids数组的下标来获得id值,这样也可以实现,如果数据库的ID值发生改变,只需要改变service层的ID数组就行。 18. * 19. * 3、用配置文件(这个配置文件以.properties结尾)。将ID值写在配置文件中,表示的方法就和map一样,key=value,这样就OK。 20. * 21. * 下面我要说的是第三种方案,个人觉得第三种方案比第二种方案好,因为第二种方案改数据时,去找的话,有点不太方便。 22. * 23. * @author 姚大庆 24. * 25. */ 26.public class CommonParam { 27. 28. private String propertyFileName; 29. private ResourceBundle resourceBundle; 30. 31. public CommonParam() { 32. propertyFileName = "ydqfile"; 33. resourceBundle = ResourceBundle.getBundle(propertyFileName); 34. } 35. 36. public String getString(String key) { 37. if (key == null || key.equals("") || key.equals("null")) { 38. return ""; 39. } 40. String result = ""; 41. try { 42. result = resourceBundle.getString(key);// 根据key获取value 43. } catch (MissingResourceException e) { 44. e.printStackTrace(); 45. } 46. return result; 47. } 48. 49. public static void main(String[] args) { 50. /* 51. * 方法一:直接通过java.util.ResourceBundle读取配置文件 52. * CommonParam test = new CommonParam(); 53. * System.out.println(test.getString("ydqa")); 54. * System.out.println(test.getString("ydqb")); 55. * System.out.prwww.hnne.comintln(test.getString("ydqc")); 56. */ 57. 58. /* 59. * 方法二:通过java中java.util.Properties对象读取配置文件 60. * */ 61. CommonParam test = new CommonParam(); 62. System.out.println(test.getPara("ydqfile.properties")); 63. 64. } 65. 66. /** 67. * 学习JAVA中java.util.Properties对象读取配置文件 68. * @param fileName 配置文件名 69. * @return 根据key返回value 70. */ 71. public String getPara(String fileName) { 72. Properties prop = new Properties(); 73. try { 74. InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName); 75. prop.load(is); 76. if (is != null) 77. is.close(); 78. } catch (Exception e) { 79. System.out.println(e + " file " + fileName + " not found"); 80. } 81. return prop.getProperty("ydqb"); 82. }

Ⅲ 如何在jsp中读取配置文件的内容

<%Properties pro = new Properties();pro.load(new FileInputStream((c:/jdbc.properties")));//配置文件路径db_userName = pro.getProperty("db_userName");db_passWord = pro.getProperty("db_passWord");%>之后自己用eclipse或者IDEA加try catch

Ⅳ jsp怎么获取spring配置文件properties中的信息

http://hi..com/lzqxiaoqiang09/item/9889b3e0f5cd5d17585dd89d

Ⅳ JSP页面怎样调用Spring配置文件中定义的Bean

jsp页面如果想要根据id直接查询信息的话,可能会需要这样的代码而应用类Spring框架之后如上图的NewsService里面是没有实例化过的NewsDao的,这样上面图中的方法就执行不了那假如想要使用NewsServcie中的方法,就需要去找Spring,在Action因为设置了setter方法注入所以可以直接获得实例化好的对象,那在jsp中呢?首先你需要有一个jar包,形如spring-web-3.2.0.M2.jar,将此包加入build Path并部署或者直接复制到WEB-INF/lib下,这是spring应用在web项目时需要用到的jar包然后在jsp页面中导入相关的工具类:<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%><%@ page import="org.springframework.web.context.WebApplicationContext"%>最后通过以下语句获取配置文件中相应的BeanWebApplicationContext wac = WebApplicationContextUtils.(this.getServletContext()); NewsService service = (NewsService)wac.getBean("newsService");注意getBean()方法中传入的是配置文件中的Bean的id这样就可以在页面中访问Spring的Bean了,同时也可以访问service的方法了

Ⅵ 如何在JSP页面引用配置文件的常量值

用ResourceBundle读取properties文件,然后getString()根据key取得你要的value就行。这个操作推荐在某个Servlet或Filter的init()方法里做,然后把值回存到application属性范围,答这样页面用起来更方便,而且性能更高,省得每次访问页面重新读

Ⅶ 怎样在jsp页面上读取服务器磁盘上的文件

一、配置虚拟路径

如:磁盘上保存的路径为E:/file

虚拟路径配置为/upload

在tomcat的server.xml中配置:

<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"><!– SingleSignOn valve, share authentication between web applicationsDocumentation at: /docs/config/valve.html –><!–<Valve className="org.apache.catalina.authenticator.SingleSignOn" />–><!– Access log processes all example.Documentation at: /docs/config/valve.htmlNote: The pattern used is equivalent to using pattern="common" –><Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="localhost_access_log." suffix=".txt"

pattern="%h %l %u %t &quot;%r&quot; %s %b" />

<!– 在配置文件中加入的内容 –!>

<Context docBase="E:/file/" path="/upload/" reloadable="true"/>

</Host>

二、在jsp页面上:

<img src="http://ip:端口号/upload/example.jpg"/>

Ⅷ jsp里可以读取.properties配置文件吗可以的话怎么读取

可以,用那个spring 容器,在配置文件里配置,要用porps 去装key 和value 因为写两个都是字符串!

Ⅸ Java里如何添加自定义的配置文件,JSP里去读取参数

java里可以再在resources里面新建一个XML file或者 file文件XML file 会自动生成XML头,在下面加入内容就可以了,首先要有一个根节点,然后如果需要用到一些类,如:spring的一些类,就需要引入包,如:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><context:component-scan base-package="com.dist.*.controller" /> </beans >其中<?xml ……就是头,<beans 是根节点,下面的<content:……是内容。如果添加的事properties文件,格式如下:# 连接池配置pool.size = 2pool.max = 50然后jsp调用读取xml文件的方法去读取自重的内容就可以了。

Ⅹ MyEclipse9读取properties配置文件在jsp页面中显示乱码

出现乱码可能是是jsp页面配置的page的charset不是utf-8,同时工程也需要设置为utf-8你看看你的jsp页面上面的设置:如图


赞 (0)