marți, 16 iunie 2015

[OmniFaces utilities 2.0] Get the HTTP request domain URL


[OmniFaces utilities] The getRequestDomainURL() method returns the HTTP request domain URL. This is the URL with the scheme and domain, without any trailing slash.

Method:
Usage:

·         inside a servlet filter or even a plain vanilla servlet (basically, when FacesContext is not available):

import org.omnifaces.util.Servlets;
...
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
 protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // e.g. http://localhost:8080
  String domain = Servlets.getRequestDomainURL(request);
 }
 ...
}

·         in JSF, when FacesContext is available use, Faces#getRequestDomainURL()or more clumsy (not recommended):

import org.omnifaces.util.Servlets;
...
// or simply use, Faces#getRequest()
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext externalContext = context.getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();

// e.g. http://localhost:8080
String domain = Servlets.getRequestDomainURL(request);

Niciun comentariu:

Trimiteți un comentariu