我在META-INF中定义了context.xml,如下所示:
<Context path="/7Restaurant">
<Resource name="datasource"
type="javax.sql.DataSource"
auth="Container"
maxActive="10"
maxIdle="3"
maxWait="10000"
username="work"
password=""
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://127.0.0.1:5432/7restaurant"/>
</Context>
我即将部署我的网络应用程序(Servlet,在Tomcat 7中)。唯一的问题是,如何更改URL,用户名等,以匹配部署WAR包的环境。
所以,实质上,如何修改这样的文件;或任何其他技术,以便用户可以在将部署的环境中精确连接到数据库。
我是web-app eclipse,tomcat,postgresql堆栈的新手;所以我希望我的问题是错的,如果是这样的话;请让我知道任何其他解决方法。
我在Java中的上下文初始化程序:
package com.restaurant.web;
import java.io.File;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;
import javax.naming.*;
import javax.servlet.*;
import javax.sql.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import com.restaurant.dao.dbpostgres.DBDAO;
import com.restaurant.setup.GeneralConfigurerSetup;
import com.restaurant.web.Logger;
import sun.java2d.loops.DrawGlyphListAA.General;
public class Database implements ServletContextListener {
private void contextInitialized2(ServletContext servletContext) throws Exception
{
/*
//prepare META-INF/context.xml
DocumentBuilder docbldr = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docbldr.newDocument();
Element context = doc.createElement("Context");
context.setAttribute("path", "/7Restaurant");
Element resource = doc.createElement("Resource");
resource.setAttribute("name", "datasource");
resource.setAttribute("type", "javax.sql.DataSource");
resource.setAttribute("auth", "Container");
resource.setAttribute("maxActive", "10");
resource.setAttribute("maxIdle", "3");
resource.setAttribute("maxWait", "10000");
resource.setAttribute("username", Configurer.get(GeneralConfigurerSetup.DB_USERNAME));
resource.setAttribute("password", Configurer.get(GeneralConfigurerSetup.DB_PASSWORD));
resource.setAttribute("driverClassName", Configurer.get(GeneralConfigurerSetup.DB_CLASS));
resource.setAttribute("url", Configurer.get(GeneralConfigurerSetup.DB_JDBCURL));
context.appendChild(resource);
doc.appendChild(context);
System.out.println(System.getProperty("java.class.path"));
File fOut = new File("META-INF/context.xml");
System.out.println(fOut.getAbsolutePath());
if (fOut.exists()) fOut.delete();
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
Source input = new DOMSource(doc);
Result output = new StreamResult(fOut);
tf.transform(input, output);
*/
//META-INF/context
InitialContext enc = new InitialContext();
Context compContext = (Context) enc.lookup("java:comp/env");
DataSource dataSource = (DataSource) compContext.lookup("datasource");
DBDAO.setDataSource(dataSource);
}
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
try {
contextInitialized2(servletContext);
} catch(Exception e) {
Logger.error("Initializing failed: " + e.getMessage());
System.exit(-1);
}
}
public void contextDestroyed(ServletContextEvent sce) {
}
}
答案 0 :(得分:0)
从构建中删除context.xml(只是为了安全),并将其放在conf / Catalina / localhost下的tomcat文件夹中。重命名它,这就是你想要的应用程序上下文名称(如myapp.xml)。
正如您所看到的,这允许每个环境具有不同的myapp.xml文件,并且当您部署战争时,它将选择它而不是使用内部文件。