当前位置: 动力学知识库 > 问答 > 编程问答 >

servlets - Initialize application scoped variables

问题描述:

I have a JSP-based Webapp which is accessing a SOAP-Service for some auth-credentials verification.

I want to put this Services URL in a application-scope Variable so I can easily pass to the SOAP-calls. Whats the best way to do this? Put it in a init-param and then on pass it to application.wsdlurl on init?

网友答案:

You need to define it as <init-param> in servlet's definition in web.xml.

<servlet>
    <servlet-name>yourServlet</servlet-name>
    <servlet-class>com.example.YourServlet</servlet-class>
    <init-param>
        <param-name>wsdlurl</param-name>
        <param-value>http://...</param-value>
    </init-param>
</servlet>

You can assign it as an instance variable of the servlet as follows:

public class YourServlet extends HttpServlet {

    private String wsdlurl;

    @Override
    public void init() throws ServletException {
        wsdlurl = getServletContext().getInitParameter("wsdlurl");
        // Do if necessary some validation.
        // E.g., if null, then throw ServletException.
    }

    // ...
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: