Tomcat Port

import java.io.File;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;







public class PortCheck extends DefaultHandler {

String port="NOTHING";

// String PortControl2(){
// return "";
// }


// 重载DefaultHandler类的方法
// 以拦截SAX事件通知。
//
// 关于所有有效事件,见org.xml.sax.ContentHandler
//
public void startDocument() throws SAXException {
// System.out.println( "SAX Event: START DOCUMENT" );
}

public void endDocument( ) throws SAXException {
// System.out.println( "SAX Event: END DOCUMENT" );
}

public void startElement( String namespaceURI,
String localName,
String qName,
Attributes attr ) throws SAXException {
System.out.println( "SAX Event: START ELEMENT[ " +
localName + " ]" );

// 如果有属性,我们也一并打印出来...
for ( int i = 0; i < attr.getLength(); i++ ){
System.out.println( " ATTRIBUTE: " +
attr.getLocalName(i) +
" VALUE: " +
attr.getValue(i) );
}

if(localName.equals("Connector")&&attr.getLength()>=4){
System.out.println("-------------------"+attr.getValue(0));
port=attr.getValue(0);
}

}



public void endElement( String namespaceURI,
String localName,
String qName ) throws SAXException {
// System.out.println( "SAX Event: END ELEMENT[ " +
// localName + " ]" );
}

public void characters( char[] ch, int start, int length )
throws SAXException {

// System.out.print( "SAX Event: CHARACTERS[ " );

try {
OutputStreamWriter outw = new OutputStreamWriter(System.out);
outw.write( ch, start,length );
outw.flush();
} catch (Exception e) {
e.printStackTrace();
}

// System.out.println( " )" );

}



public static String PortCheck(String url){
PortCheck port= new PortCheck();
try {
XMLReader xr = XMLReaderFactory.createXMLReader();
xr.setContentHandler(port);
// "E:\\apache-tomcat-6.0.29\\apache-tomcat-6.0.29\\conf\\server.xml"
xr.parse( new InputSource(new FileReader(url)) );
}catch ( Exception e ) {
e.printStackTrace();
}
return port.port.toString();

}



public boolean TomcatServer(String port){

try{
URL url = new URL("http://localhost:"+port+"/TestingIdP/");
URLConnection con = url.openConnection();
java.io.BufferedReader in = new
java.io.BufferedReader(new java.io.InputStreamReader(con.getInputStream()));
}catch(Exception e){
return false;
}
return true;

}




public static void main(String[] args){

try {

String port=PortCheck("E:\\Pangoo\\TomcatServer\\apache-tomcat-6.0.29\\conf\\server.xml");
System.out.println("--------test------ "+port);
}catch ( Exception e ) {
e.printStackTrace();
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值