① ASP.NET MVC 4中如何讀取web.config中的配置
用ConfigurationManager這個類。
② web項目中如何讀取配置文件config.xml
servlet初始化的時候會去讀取web.xml,把這個文件的路徑配置到web.xml里。或者你在web.xml里載入個初始化類,這個類去載入config.xml
③ 為什麼就是獲取不到javaweb工程下的配置文件所在路徑
在Java web項目中經常會用屬性文件作為配置文件,而其一般放在src的根目錄下,讀取文件時一般會有以下兩種情況:
方式一、在servlet中讀取:
// action配置文件路徑
public static final String ACTIONPATH = "WEB-INF/classes/actions.properties";
// 屬性文件
public static final Properties prop = new Properties();
// 獲取servlet上下文的絕對路徑,如:C:\Program Files\Apache\Tomcat 6.0\webapps\fee\
String path = getServletContext().getRealPath("\\");
// 把文件讀入文件輸入流,存入內存中
FileInputStream fis = new FileInputStream(new File(path + ACTIONPATH));
//載入文件流的屬性
prop.load(fis);
方式二、在一般的類中讀取:
// action配置文件路徑
public static final String ACTIONPATH = "actions.properties";
// 屬性文件
public static final Properties prop = new Properties();
// 獲取當前類載入的根目錄,如:/C:/Program Files/Apache/Tomcat 6.0/webapps/fee/WEB-INF/classes/
String path = UriFilter.class.getClassLoader().getResource("").toURI().getPath();
// 把文件讀入文件輸入流,存入內存中
FileInputStream fis = new FileInputStream(new File(path + ACTIONPATH));
//載入文件流的屬性
prop.load(fis);
④ webpack打包VUE項目讀取外部配置文件,靈活配置域名
1.在index.html中引入js
<script src="./static/config.js"></script>
2.man.js
⑤ java web工程,讀取配置文件路徑問題
讀取復src下的文製件,可以用下面的方式
publicclassTest1{
publicstaticvoidmain(String[]args){
Propertiespro=newProperties();
InputStreamin=Test1.class.getResourceAsStream("/config.properties");
try{
pro.load(in);
pro.getProperty("aa");
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
⑥ web.xml中如何讀取properties配置文件中的值
方法如下:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/config/log4j.properties</param-value>
</context-param>
⑦ JAVA maven創建web把Spring放在src/main/讀取不到配置文件是怎麼回事
classpath:是從類路徑里查找配置文件,也就是/WEB-INF/classes目錄下找SpringMVC-servlet.xml。
你寫了classpath了,不會從web-info下找,而是去web-inf/classes下面找,所以找不到。
⑧ myeclipse做webservice開發,如何讀取配置文件中web.xml中設置的參數
給個小例子參考參考~~~
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Loginv1Servlet extends HttpServlet {
private String xmlName,xmlPass;
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
doPost(arg0, arg1);
}
public void init(ServletConfig config) throws ServletException {
// 初始化時候對xml文檔的內容進行讀取 //這個是重寫超類方法
xmlName=config.getInitParameter("name");
xmlPass=config.getInitParameter("pass");
/*
* <init-param>
* <param-name>pass</param-name>
* <param-value>123456</param-value>
*xml文件中的寫法,寫在<servlet> 塊內
* </init-param>
*/
}
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//首先對客戶端的數據流編碼 解決中文問題
req.setCharacterEncoding("GB2312");
// 得到客戶傳遞的數據
String str= req.getParameter("action");
String id=req.getParameter("id");
String name=req.getParameter("name");
String amount=req.getParameter("amount");
res.setContentType("text/html;charset=gb2312");
PrintWriter pw=res.getWriter();
pw.println("<html>");
pw.println("<title>准備購買的商品信息</title><body>");
pw.println("編號:"+ id);
pw.println("名稱:"+ name);
pw.println("數量:"+ amount);
if("1".equals(str)){
//電子支付
pw.println("<a href='http://www.post.com.cn'target=_blank> 進入網上銀行</a>");
}else{
//匯款
pw.println("<a href='info.html'>填寫詳細信息</a>");
}
pw.println("</body>");
pw.println("</html>");
}
}
⑨ java web項目 web.xml中如何讀取properties配置文件中的值
<util:properties id="config" location="classpath:test.properties" />
其中id為讀取文件以後的bean id,可以通過這個id獲取文件中對應屬性的值,如config.test代表獲取文件中test屬性的值
⑩ java 怎麼讀取web jar中的某個配置文件
項目遷移的過程中發現以前的代碼維護性實在是差。
我把問題簡化為以下這專些簡單的代碼:屬
項目M
引用了項目
A.jar,這個A在lib目錄裡面
在A裡面放置了一個配置文件test.properties,
就放在jar的根目錄下。
A.jar
|___test.properties
在M中有一段代碼回去讀取這個A.jar里的配置文件,簡單一點就用下面這句話來調用。
Java
code
public
class
ConfigUtil
{
public
static
String
getInstance()
throws
Exception{
String
path
=
ConfigUtil.class.getResource("/").toString();
path
=
path.substring(0,
path.length()-8);//
System.out.println(path);//這里列印的結果顯示可以拿到當前類的絕對路徑
InputStream
f
=
new
FileInputStream("jar:"+path+"lib!/A.jar/"+"test.properties");
return
"xxx";
}
}