2012-12-19 57 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

相关问题