Web服务(axis2)如何从web.xml

时间:2016-01-19 17:25:11

标签: java xml web-services servlets axis2

我创建一个简单的类然后我在Eclipse Luna中使用Axis2创建一个Web服务,我需要为获取与数据库连接的组件设置一些基本参数(主机,数据库名称,端口,用户,密码) ,我尝试从web.xml获取参数,但我无法得到它。

public class SearchPay {
    /***
     * Search the payment
     * @param paramKey
     * @return return true or false
     */
    public String PaySearch(String paramKey){

        ResultSet rsPayment=null;
        String xmlResp="";

        try{
            rsPayment = getData(paramKey);

            if(getRowCount(rsPayment )>0){
                while(rsPayment.next()){
                    if(rsPayment.getString("STATUS")!=null)
                        xmlResp = rsPayment.getString("STATUS").trim();

                    System.out.println(xmlResp);
                }
            }
        }catch(Exception e2){
            e2.printStackTrace();
        }finally{
            try{
                if(!rsPayment.isClosed()){
                    rsPayment.close();
                }
            }catch(Exception e1){
                e1.printStack();
            }
        }

        return xmlResp;
    }
}

如果我使用Servlet类,我只能这样做。但我不能使用我以前的方法,因为Servlet使用doPost和doGet方法。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = null;

        try{
            context = this.getServletContext();
            setBDHost(context.getInitParameter("bdhost"));
            setBDUser(context.getInitParameter("bduser"));
            setBDPassword(context.getInitParameter("bdpassword"));
            setBDPort(context.getInitParameter("bdport"));

        }catch(Exception ex){
            ex.printStackTrace();
        }

    }

这是web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>FirstWebServiceExample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>/axis2-web/index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <display-name>Apache-Axis Servlet</display-name>
    <servlet-name>AxisServlet</servlet-name>
    <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/servlet/AxisServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>*.jws</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  <servlet>
    <display-name>Apache-Axis Admin Servlet Web Admin</display-name>
    <servlet-name>AxisAdminServlet</servlet-name>
    <servlet-class>org.apache.axis2.transport.http.AxisAdminServlet</servlet-class>
    <load-on-startup>100</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>AxisAdminServlet</servlet-name>
    <url-pattern>/axis2-admin/*</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>DBHost</param-name>
    <param-value>myHost</param-value>
  </context-param>
  <context-param>
    <param-name>DBUser</param-name>
    <param-value>myUser</param-value>
  </context-param>
  <context-param>
    <param-name>DBPassword</param-name>
    <param-value>myPassword</param-value>
  </context-param>
  <context-param>
    <param-name>DBPort</param-name>
    <param-value>myPort</param-value>
  </context-param>
</web-app>

您如何推荐我从某些存档xml中获取参数?

如果我使用Servlet类,我如何调用我的方法SearchPay(String)?如果这个类使用doPost和doGet方法?

0 个答案:

没有答案