zoukankan      html  css  js  c++  java
  • 用REST访问ALM的Servlet

    写了一个最简单的servlet用REST访问ALM。

    需要用到ALM server端的两个包:

     org.hp.qc.web.restapi.docexamples.docexamples

    org.hp.qc.web.restapi.docexamples.docexamples.infrastructure

    Servlet的代码(待修改)如下:

    public class AlmRestServlet extends HttpServlet {
    
        @Override
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            
            String serverUrl = request.getParameter("serverUrl");
            String port = request.getParameter("port");
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String domain = request.getParameter("domain");
            String project = request.getParameter("project");
            String resource = request.getParameter("resource");
    
            String conUrl = "http://" + serverUrl + ":" + port + "/qcbin";
    
            RestConnector con = RestConnector.getInstance().init(
                    new HashMap<String, String>(), conUrl, domain, project);
    
            // use the login example code to login for this test. go over this code
            // to learn how to authenticate/login/logout
            AuthenticateLoginLogoutExample login = new AuthenticateLoginLogoutExample();
            
            try {
                login.login(username, password);
    
                // read a simple resource, not even an entity..
                String urlOfResourceWeWantToRead = con.buildUrl(resource);
    
                Map<String, String> requestHeaders = new HashMap<String, String>();
                requestHeaders.put("Accept", "application/xml");
    
                Response serverResponse = con.httpGet(urlOfResourceWeWantToRead, null, requestHeaders);
    
                PrintWriter writer = response.getWriter();
                writer.write(serverResponse.toString());
            
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }          
        }
    }

    页面内容如下:通过form的action almrest来调用servlet。

    <html>  
    <head>  
    <title>Server Information</title>  
    </head>  
    <body>  
        <form method="get" action="almrest">  
            <table>  
                <tr>  
                    <td>host:</td>  
                    <td><input type="text" name="serverUrl"></td>  
                    <td>port:</td>  
                    <td><input type="text" name="port"></td>
                </tr>
                <tr>  
                     
                </tr>
                <tr>  
                    <td>username:</td>  
                    <td><input type="text" name="username"></td>  
                    <td>password:</td>  
                    <td><input type="password" name="password"></td>  
                </tr>
                <tr>  
                    
                </tr>            
                <tr>  
                    <td>domain:</td>  
                    <td><input type="text" name="domain"></td> 
                    <td>project:</td>  
                    <td><input type="text" name="project"></td>                  
                </tr>  
                <tr>  
                    
                </tr> 
                <tr>  
                    <td>resource:</td>  
                    <td><input type="text" name="resource"></td>  
                </tr>            
                <tr>  
                    <td><input type="submit" value="GetResource"></td>  
                </tr>  
            </table>  
        </form>  
    </body>  
    </html>

    web.xml的配置如下:将/almrest配置成对servlet AlmRestServlet的调用。

    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      version="3.0"
      metadata-complete="true">
    
      <display-name>Welcome to Tomcat</display-name>
      <description>
         Welcome to Tomcat
      </description>
      
      <servlet>
            <servlet-name>AlmRestServlet</servlet-name>
            <servlet-class>stan.AlmRestServlet</servlet-class>
      </servlet>
        
      <servlet-mapping>
            <servlet-name>AlmRestServlet</servlet-name>
            <url-pattern>/almrest</url-pattern>
      </servlet-mapping>
    
    
    </web-app>

    将java文件build后的class文件按照包的层级关系考到WEB-INF的classes文件夹下。

    运行的结果如下:

    点击 GetResource之后的效果:

  • 相关阅读:
    平凡的世界
    MySql启动,提示:Plugin 'FEDERATED' is disabled....Cannot allocate memory for the buffer pool
    MySql开启慢查询报错:Could not open /var/log/slow_query.log for logging (error 13).
    ie浏览器,背景色兼容解决方法
    基于Bootstrap、Jquery的自适应导航栏
    chmod、chown、chgrp的意思
    Can't connect to MySQL server on localhost (0)
    mysql-bin引起mysql不能启动
    文件类型检测
    从数据库中查询规则
  • 原文地址:https://www.cnblogs.com/stanzhu/p/3196736.html
Copyright © 2011-2022 走看看