This content has been marked as final.
Show 2 replies
-
1. Re: Accessing HttpServletRequest
junkie Mar 13, 2008 3:47 PM (in response to alringer)I had written this here, gives you what you need (getRequest()) and some more useful stuff:
public class JSFUtil { private static ResourceBundle getBundle() { FacesContext ctx = FacesContext.getCurrentInstance(); UIViewRoot uiRoot = ctx.getViewRoot(); Locale locale = uiRoot.getLocale(); ClassLoader ldr = Thread.currentThread().getContextClassLoader(); return ResourceBundle.getBundle(ctx.getApplication().getMessageBundle(), locale, ldr); } public static String getStringFromBundle(String key) { ResourceBundle bundle = getBundle(); return bundle.getString(key); } public static void setViewRootLocale(Locale locale){ FacesContext ctx = FacesContext.getCurrentInstance(); UIViewRoot uiRoot = ctx.getViewRoot(); uiRoot.setLocale(locale); } public static HttpServletRequest getRequest() { return (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest(); } public static String getRequestServerName(){ return getRequest().getServerName(); } public static String getRequestURI(){ return getRequest().getRequestURI(); } public static String getRequestQueryString(){ return getRequest().getQueryString(); } public static Locale getRequestLocale(){ return getRequest().getLocale(); } public static String getRequestUserAgent(){ return getRequest().getHeader("User-Agent"); } /** switches from HTTP to HTTPS or back * if used to go from HTTPS to HTTP older browsers (like IE 6) could complain * so use carefully */ public static void switchHTTPSecurity() throws IOException{ HttpServletRequest req = getRequest(); StringBuffer sb = new StringBuffer(); if (req.isSecure()) sb.append("http://"); else sb.append("https://"); sb.append(req.getServerName()).append(req.getRequestURI()); if (req.getQueryString() != null) sb.append("?").append(req.getQueryString()); JSFUtil.sendRedirect(sb.toString()); } public static void sendRedirect(String URL) throws IOException { HttpServletResponse response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse(); FacesContext.getCurrentInstance().responseComplete(); response.sendRedirect(URL); } public static void setPageNotFound() throws IOException { HttpServletResponse response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse(); response.setStatus(response.SC_NOT_FOUND); } }
-
2. Re: Accessing HttpServletRequest
pmuir Mar 13, 2008 10:59 PM (in response to alringer)Please don't post the same topic twice.