开发者

Java Webservice URL in JSF

开发者 https://www.devze.com 2023-02-10 04:12 出处:网络
I created a JSF application which also offers some Webservices. The webservices are created via annotations.

I created a JSF application which also offers some Webservices. The webservices are created via annotations.

Now I want to create a webserviceInfo.xhtml Page , where I get all the needed webservice Information.

When I g开发者_StackOverflowo to the address http://our.server.com/application/OurWebserviceName, I get all the information needed to access the webservice (this info page is generated automatically by Glassfish ).

To include this page, I did the following in the webserviceInfo.xhtml:

 <iframe scrolling="automatic"  width="971" height="1000" src="#{myBean.generateUrlToWebservice()}/OurWebserviceName"/>

Where:

public String generateUrlToWebservice(){
    FacesContext fc = FacesContext.getCurrentInstance();
    String servername = fc.getExternalContext().getRequestServerName();
    String port = String.valueOf(fc.getExternalContext().getRequestServerPort());
    String appname = fc.getExternalContext().getRequestContextPath();
    return "http://"+servername+":"+port+appname;
}

Is there a more elegant solution to this?

BR, Rene


Use a page-relative URL.

<iframe src="OurWebserviceName"></iframe>

Or make use of <base> tag with little help of JSTL functions taglib.

<html xmlns:fn="http://java.sun.com/jsp/jstl/functions">
...
<base href="#{fn:replace(request.requestURL, request.requestURI, '')}#{request.contextPath}"></base>

This way any URL which doesn't start with scheme or / is always relative to this URL.

Or if you really need to do it in JSF the following gives less headache with scheme and port.

HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
return request.getRequestURL().toString().replace(request.getRequestURI, "") + request.getContextPath();


Better will be if you gets all parameters dynamically like protocol (http,https..) and pages (after app name)

public String generateUrlToWebservice(){
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext exContext = fc.getExternalContext();
    String servername = exContext.getRequestServerName();
    String port = String.valueOf(exContext.getRequestServerPort());
    String appname = exContext.getRequestContextPath();
    String protocol = exContext.getRequestScheme();
    String pagePath = exContext.getInitParameter("pagePath"); //read it from web.xml
    return protocol +"://"+servername+":"+port+appname+pagePath;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消