简体   繁体   中英

How to identify application that calls web service

I have a Java servlet application with 6 web services and one of the web requests I must determine if it is the Live or Test instance calling the web service. I believe I can determine this from URL, ie

 https://localhost:8443/projectname/etc 

or

https://localhost:8443/projectnameTEST/etc 

I am trying to obtain this using the HttpServletRequest and I know I have to call a procedure like the one below from inside the web service named "getDocument" But how do I initialise the HttpServletRequest inside getDocument? That is how do I create the parameter "request" in the getDocument web service and initialise it to the request information?

public void doGet(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
     ...
     StringBuffer url = request.getRequestURL();
     ...
} 

You can retrieve the client information so, for example:

@WebService()
public class SomeWebService {
    ...
    @Resource private WebServiceContext wsc;
    ...
    @WebMethod 
    public void methodOfWebService() {      
        MessageContext mc = wsc.getMessageContext();

        // retrieve the client information
        HttpServletRequest httpServletRequest = 
            (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST);    
        System.out.println(httpServletRequest.getServletContext().getContextPath());
        ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM