得到服务器的文件的绝对路径:
<%! //获取服务器根目录 String serPath = this.getClass().getResource("/").getPath(); int indTom = serPath.indexOf("Tomcat"); String be = serPath.substring(0,indTom); //处理后得到context.xml文件的绝对路径 String conf = be + serPath.substring(serPath.indexOf("Tomcat")).substring(0,serPath.substring(serPath.indexOf("Tomcat")).indexOf("/")) +"/conf/context.xml"; %> <% //将路径中符号转变成空格,这样得到的conf就是文件的绝对路径 conf = conf.replaceAll("%20", " "); %>
完整列子:
<%@ page language="java" import="java.util.*,org.w3c.dom.*,java.io.*" pageEncoding="GBK"%> <%@ page import="javax.servlet.http.HttpServletRequest,javax.xml.transform.stream.*, org.w3c.dom.*,javax.xml.transform.*,javax.xml.parsers.*,javax.xml.transform.dom.*" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%! //属性项集合 Map att = new HashMap<String, String>(); String userName = ""; String pass = ""; String databaseType = ""; String url = ""; String databaseName = ""; //将修改的内容添加到xml文件中 // // @param document // @param filename // @return public static boolean doc2XmlFile(Document document, String filename) { boolean flag = true; try { /** 将document中的内容写入文件中 */ TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); /** 编码 */ // transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File(filename)); transformer.transform(source, result); } catch (Exception ex) { flag = false; ex.printStackTrace(); } return flag; } //读取xml文件 // // @param filename // @return public static Document load(String filename) { Document document = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(new File(filename)); document.normalize(); } catch (Exception ex) { ex.printStackTrace(); } return document; } /** * 演示修改文件的具体某个节点的值 */ public static void xmlUpdateDemo() { Document document = load("c://Message.xml"); Node root = document.getDocumentElement(); /** 如果root有子元素 */ if (root.hasChildNodes()) { /** ftpnodes */ NodeList ftpnodes = root.getChildNodes(); /** 循环取得ftp所有节点 */ for (int i = 0; i < ftpnodes.getLength(); i++) { NodeList ftplist = ftpnodes.item(i).getChildNodes(); for (int k = 0; k < ftplist.getLength(); k++) { Node subnode = ftplist.item(k); //修改节点的值 if (subnode.getNodeType() == Node.ELEMENT_NODE && subnode.getNodeName() == "status") { subnode.getFirstChild().setNodeValue("1"); } } } } doc2XmlFile(document, "c://Message.xml"); } // 演示修改文件的具体某个节点的属性 public static void xmlUpdateDemoAttri() { Document document = load("c://xx.xml"); Node root = document.getDocumentElement(); /** 如果root有子元素 */ if (root.hasChildNodes()) { /** ftpnodes 根节点的子节点 */ NodeList ftpnodes = root.getChildNodes(); /** 循环取得第一层子节点所有节点 */ for (int i = 0; i < ftpnodes.getLength(); i++) { Node subnode = ftpnodes.item(i); //添加或修改某节点的属性配置 if ("Resource".equals(subnode.getNodeName())) { // 生成一个属性对象 Attr attr = document.createAttribute("ss"); attr.setValue("ssss"); subnode.getAttributes().setNamedItem(attr); } } } // 将修改的内容添加到xml文件中 doc2XmlFile(document, "c://xx.xml"); } //添加新的节点 // 根节点下没有节点的话直接添加 // 根节点下没有重名的直接添加 // 有重名的节点则更新节点属性 // @param nodeName 添加、更新的节点名 // @param attr 属性集合 public static void xmlAddDemoAttri(String filePath,String nodeName,Map<String, String> attr) { Document document = load(filePath); Node root = document.getDocumentElement(); //创建节点元素,并命名 Element element =document.createElement(nodeName); //向节点中添加属性 for (Object key : attr.keySet().toArray()) { element.setAttribute(key.toString(), attr.get(key)); } //找到根节点 NodeList nodeList = document.getElementsByTagName("Context"); //先判断根节点下有没有子节点,没有的话直接添加 Node rootNode = nodeList.item(0); if(!root.hasChildNodes()){ nodeList.item(0).appendChild(element); }else{ //如果有重复的节点,flag=true; boolean flag = false; NodeList rootChs = rootNode.getChildNodes(); //循环根节点下的所有子节点 for (int i = 0; i < rootChs.getLength(); i++) { Node node = rootChs.item(i); //如果没有重名,并且是最后一个节点的就添加 if(!nodeName.equals(node.getNodeName()) && !flag && (i+1) == rootChs.getLength()){ nodeList.item(0).appendChild(element); }else if(nodeName.equals(node.getNodeName())){ //有重名的就看name属性,name一样就修改属性 if(node.hasAttributes()){ //如果有属性项,判断name属性值,如果name的值相同,则修改其他属性 if(null != node.getAttributes().getNamedItem("name") && attr.get("name").equals(node.getAttributes().getNamedItem("name").getNodeValue())){ // 生成一个属性对象 Attr chAttr = null; //向节点中添加属性 for (Object key : attr.keySet().toArray()) { //不更新name属性 if(!"name".equals(key.toString())){ chAttr = document.createAttribute(key.toString()); chAttr.setValue(attr.get(key)); } } node.getAttributes().setNamedItem(chAttr); }else if(null != node.getAttributes().getNamedItem("name") && !attr.get("name").equals(node.getAttributes().getNamedItem("name").getNodeValue()) && !flag && (i+1) == rootChs.getLength()){ //如果name的值不相同,且都没有相同的节点,添加新的节点 System.out.println(attr.get("name")); System.out.println(node.getAttributes().getNamedItem("name").getNodeValue()); nodeList.item(0).appendChild(element); } } } } } // 将修改的内容添加到xml文件中 doc2XmlFile(document, filePath); } %> <%! //获取服务器根目录 String serPath = this.getClass().getResource("/").getPath(); int indTom = serPath.indexOf("Tomcat"); String be = serPath.substring(0,indTom); //处理后得到context.xml文件的绝对路径 String conf = be + serPath.substring(serPath.indexOf("Tomcat")).substring(0,serPath.substring(serPath.indexOf("Tomcat")).indexOf("/")) +"/conf/context.xml"; %> <% //将路径中符号转变成空格 conf = conf.replaceAll("%20", " "); //添加属性项 userName = request.getParameter("userName"); url = request.getParameter("url"); pass = request.getParameter("pass"); databaseType = request.getParameter("databaseType"); databaseName = request.getParameter("databaseName"); //如果是SqlServer的修改 att.put("username", userName); att.put("password", pass); att.put("name", "jdbc/sqlserver-database"); att.put("url", url+":1433;databaseName="+databaseName+";SelectMethod=cursor"); att.put("type", "javax.sql.DataSource"); att.put("driverClassName", "net.sourceforge.jtds.jdbc.Driver"); att.put("maxIdle", "5"); att.put("maxWait", "5000"); att.put("maxActive", "100"); try{ xmlAddDemoAttri(conf ,"Resource",att); }catch(Exception e){ }finally{ %> <mce:script type="text/javascript"><!-- alert("配置完成,请重启Tomcat服务!"); window.close(); // --></mce:script> <% } %>
您还没有登录,请您登录后再发表评论
注意:xml文件是区分大小写的,不可以将<Context>写成<context>。 步骤三: 重新启动Tomcat服务器 将jsp页面放到C:\wang或D:\MyBook\zhang下保存 步骤四: 在浏览器输入: ...
网络上很多的APN文件,看了许多,没有带电信参数的,花费一晚上的时间,通宵写成了这个,确切的说是改成了这个集成了中国电信相关参数的apns-conf.xml文件,借阅请说明出处,谢谢
一、Idea关联的maven本地仓库配置文件settings.xml (1)必须使用默认文件名 D:\developsoft\javaweb\commonPlugins\maven\apache-maven-3.8.1_first\conf\settings.xml 二、Myeclipse关联的maven本地仓库配置文件...
节选自《Linux安全技术内幕》的“11.3 安全配置DNS服务器”,介绍named.conf文件的主要配置,各字段的含义等
小米手机中获取的apn列表 apns-conf.xml,适用于需要自动配置apn的场合,从9.3.21固件中提取
在Tomcat默认安装后,tomcat的主目录是webapps/root目录。 在conf文件夹下面找到server.xml 打开 在<hosts> ……中间添加一行 代码如下:<Context path=”” docBase=”/usr/tomcat/apache-tomcat-8.5.11/webapps/...
redis.conf Redis配置文件 下载 redis.conf 配置详解 Redis配置文件redis.conf 详解1.基本配置内存单位的表示# 1k => 1000 bytes# ...Redis 的详细介绍Redis 的下载地址
Apache服务器的本地配置是一件令人头疼的事,无法访问一般是由于配置文件的不正确导致的,这是一份可以在Mac系统中正常运行的配置文件。
4、修改 Tomcat 的 conf/context.xml 文件,将<Context>为<Context swallowOutput="true"> (增加 swallowOutput="true" 的属性配置,只有这样才能完全的把tomcat的stdout给接管过来。这一步很关键); 5、删除...
TongWeb主配置文件tongweb.xml概要说明,TongWeb常见问题处理,用于运维的问题处理,便于解决应用部署时遇到的各种问题.
vendor/rockchip/common/phone/etc apn 电信、联通,移动
snmpd.conf配置文件 送给同学 用在nagios上面
以下是对Linux系统中的xorg.conf文件进行了详细的分析介绍,需要的朋友可以过来参考下
2.修改 conf 目录下的 context.xml 文件 <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions....
第三步:修改tomcat/conf目录下的server.xml文件. 在</host>前加入以下代码: <Context path="" docBase="e:\jsp" debug="0" relbookdable="true" workDir="e:\jsp\work"></Context> 保存server....
redis.conf配置文件下载
Linux tomcat下catalina.out日志文件分割 前言: tomcat默认使用Java.util.logging记录日志,默认只记录tomcat的日志,不记录应用的日志。tomcat支持采用log4j进行日志记录,配置方法如下: 1.更新最新的tomcat_juli...
编辑${TOMCAT_HOME}/conf/context.xml,在context中加入 <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions....
Tomcat简单的说就是一个运行JAVA的网络服务器,底层是Socket的一个程序,它也是JSP和Serlvet的一个容器。如果你学过html,css,你会知道你写的页面只能自己访问,别人不能远程访问你写的页面,Tomcat就是提供能够让...
相关推荐
注意:xml文件是区分大小写的,不可以将<Context>写成<context>。 步骤三: 重新启动Tomcat服务器 将jsp页面放到C:\wang或D:\MyBook\zhang下保存 步骤四: 在浏览器输入: ...
网络上很多的APN文件,看了许多,没有带电信参数的,花费一晚上的时间,通宵写成了这个,确切的说是改成了这个集成了中国电信相关参数的apns-conf.xml文件,借阅请说明出处,谢谢
一、Idea关联的maven本地仓库配置文件settings.xml (1)必须使用默认文件名 D:\developsoft\javaweb\commonPlugins\maven\apache-maven-3.8.1_first\conf\settings.xml 二、Myeclipse关联的maven本地仓库配置文件...
节选自《Linux安全技术内幕》的“11.3 安全配置DNS服务器”,介绍named.conf文件的主要配置,各字段的含义等
小米手机中获取的apn列表 apns-conf.xml,适用于需要自动配置apn的场合,从9.3.21固件中提取
在Tomcat默认安装后,tomcat的主目录是webapps/root目录。 在conf文件夹下面找到server.xml 打开 在<hosts> ……中间添加一行 代码如下:<Context path=”” docBase=”/usr/tomcat/apache-tomcat-8.5.11/webapps/...
redis.conf Redis配置文件 下载 redis.conf 配置详解 Redis配置文件redis.conf 详解1.基本配置内存单位的表示# 1k => 1000 bytes# ...Redis 的详细介绍Redis 的下载地址
Apache服务器的本地配置是一件令人头疼的事,无法访问一般是由于配置文件的不正确导致的,这是一份可以在Mac系统中正常运行的配置文件。
4、修改 Tomcat 的 conf/context.xml 文件,将<Context>为<Context swallowOutput="true"> (增加 swallowOutput="true" 的属性配置,只有这样才能完全的把tomcat的stdout给接管过来。这一步很关键); 5、删除...
TongWeb主配置文件tongweb.xml概要说明,TongWeb常见问题处理,用于运维的问题处理,便于解决应用部署时遇到的各种问题.
vendor/rockchip/common/phone/etc apn 电信、联通,移动
snmpd.conf配置文件 送给同学 用在nagios上面
以下是对Linux系统中的xorg.conf文件进行了详细的分析介绍,需要的朋友可以过来参考下
2.修改 conf 目录下的 context.xml 文件 <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions....
第三步:修改tomcat/conf目录下的server.xml文件. 在</host>前加入以下代码: <Context path="" docBase="e:\jsp" debug="0" relbookdable="true" workDir="e:\jsp\work"></Context> 保存server....
redis.conf配置文件下载
Linux tomcat下catalina.out日志文件分割 前言: tomcat默认使用Java.util.logging记录日志,默认只记录tomcat的日志,不记录应用的日志。tomcat支持采用log4j进行日志记录,配置方法如下: 1.更新最新的tomcat_juli...
编辑${TOMCAT_HOME}/conf/context.xml,在context中加入 <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions....
Tomcat简单的说就是一个运行JAVA的网络服务器,底层是Socket的一个程序,它也是JSP和Serlvet的一个容器。如果你学过html,css,你会知道你写的页面只能自己访问,别人不能远程访问你写的页面,Tomcat就是提供能够让...