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();
}
}
页:
[1]