2012-12-19 111 views
0

我使用HttpClient。我使用JSF開發了Web應用程序。部署到tomcat6.0.29。 我正在嘗試停止該應用程序。但它不工作。使用java代碼停止tomcat中的web應用程序

../conf/tomcat-users.xml文件內容

<?xml version='1.0' encoding='utf-8'?> 
     <tomcat-users> 
      <role rolename="manager"/> 
      <user username="tomcat" password="s3cret" roles="manager" /> 
    </tomcat-users> 

我的Java代碼是

import org.apache.commons.httpclient.*; 
import org.apache.commons.httpclient.methods.*; 
import org.apache.commons.httpclient.params.HttpMethodParams; 
import java.io.*; 
public class HttpClientTutorial 
{ 
private static String url = "http://localhost:8080/manager/html/stop?path=/jsfproject"; 
public static void main(String[] args) 
{   
    HttpClient client = new HttpClient(); 

    GetMethod method = new GetMethod(url); 

    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
      new DefaultHttpMethodRetryHandler(3, false)); 
    try{ 

     int statusCode = client.executeMethod(method); 
     if (statusCode != HttpStatus.SC_OK) 
     { 
      System.err.println("Method failed: " + method.getStatusLine()); 
     } 

     byte[] responseBody = method.getResponseBody(); 

     System.out.println(new String(responseBody)); 
    } 
    catch (HttpException e) 
    { 
     System.err.println("Fatal protocol violation: " + e.getMessage()); 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     System.err.println("Fatal transport error: " + e.getMessage()); 
     e.printStackTrace(); 
    } 
    finally 
    {    
     method.releaseConnection(); 
    } 
} 
} 

但我得到了錯誤這樣

log4j:WARN No appenders could be found for logger (org.apache.commons.httpclient.HttpClient). 
log4j:WARN Please initialize the log4j system properly. 
Method failed: HTTP/1.1 401 Unauthorized 

此外,我得到

401 Unauthorized 
You are not authorized to view this page. If you have not changed any configuration `files, please examine the file conf/tomcat-users.xml in your installation. 

That file will contain the credentials to let you use this webapp. 
ou will need to add manager role to the config file listed above. For example: 

<role rolename="manager"/> 
<user username="tomcat" password="s3cret" roles="manager"/> 

For more information - please see the Manager App HOW-TO. 

幫幫我。 在此先感謝。

回答

2

爲了停止應用程序,Tomcat需要您在tomcat-users.xml中創建的用戶憑據,但是您的代碼不會嘗試提供它們。

下面是一些clode這可能給你一個線索:

HttpClient client = new HttpClient(); 
HttpState state = client.getState(); 

// Set credentials on the client 
Credentials credentials = new UsernamePasswordCredentials("tomcat","s3cret"); 
state.setCredentials(null, null, credentials); 

HttpMethod method = new GetMethod(url); 
+0

感謝你的努力。 state.setCredentials(null,null,credentials);此方法已棄用。有沒有其他方法setCredentials(,,) – jackrobert

相關問題