Example usage for javax.servlet.jsp PageContext getServletContext

List of usage examples for javax.servlet.jsp PageContext getServletContext

Introduction

In this page you can find the example usage for javax.servlet.jsp PageContext getServletContext.

Prototype


abstract public ServletContext getServletContext();

Source Link

Document

The ServletContext instance.

Usage

From source file:com.ecyrd.jspwiki.preferences.Preferences.java

/**
 *  Reloads the preferences from the PageContext into the WikiContext.
 *  /*from  w ww .ja v  a2 s .co  m*/
 *  @param pageContext The page context.
 */
// FIXME: The way that date preferences are chosen is currently a bit wacky: it all
//        gets saved to the cookie based on the browser state with which the user
//        happened to first arrive to the site with.  This, unfortunately, means that
//        even if the user changes e.g. language preferences (like in a web cafe),
//        the old preferences still remain in a site cookie.
public static void reloadPreferences(PageContext pageContext) {
    Preferences prefs = new Preferences();
    Properties props = PropertyReader.loadWebAppProps(pageContext.getServletContext());
    WikiContext ctx = WikiContext.findContext(pageContext);

    prefs.put("SkinName",
            TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.skinname", "PlainVanilla"));
    prefs.put("DateFormat",
            TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.dateformat",
                    ctx.getEngine().getInternationalizationManager().get(
                            InternationalizationManager.CORE_BUNDLE, getLocale(ctx), "common.datetimeformat")));

    prefs.put("TimeZone", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.timezone",
            java.util.TimeZone.getDefault().getID()));

    prefs.put("Orientation",
            TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.orientation", "fav-left"));

    prefs.put("Language", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.language",
            getLocale(ctx).toString()));

    prefs.put("SectionEditing",
            TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.sectionediting", ""));

    // FIXME: "editor" property does not get registered, may be related with http://bugs.jspwiki.org/show_bug.cgi?id=117
    // disabling it until knowing why it's happening
    // FIXME: editormanager reads jspwiki.editor -- which of both properties should continue
    prefs.put("editor", TextUtil.getStringProperty(props, "jspwiki.defaultprefs.template.editor", "plain"));

    parseJSONPreferences((HttpServletRequest) pageContext.getRequest(), prefs);

    pageContext.getSession().setAttribute(SESSIONPREFS, prefs);
}

From source file:io.undertow.test.jsp.expression.ExpressionJspTestCase.java

public static void testIIllegalState(PageContext pageContext, JspWriter out) throws Exception {
    assert pageContext != null;
    ELContext elContext = pageContext.getELContext();
    assert elContext != null;
    JspApplicationContext jaContext = JspFactory.getDefaultFactory()
            .getJspApplicationContext(pageContext.getServletContext());
    assert jaContext != null;

    try {/*from   ww w.j av a 2s .c o m*/
        jaContext.addELResolver(new CompositeELResolver());
        out.println("addELResolver call succeeded. Test FAILED.");
    } catch (IllegalStateException ise) {
        out.println("Test PASSED");
    }
}

From source file:de.iteratec.iteraplan.presentation.tags.TagUtils.java

public static void insertImage(PageContext ctx, String image, int width, int height) throws IOException {
    JspWriter w = ctx.getOut();//from www. j av  a 2 s.c  o m
    String alt = "";

    w.print("<img src=\"");
    w.print(ctx.getServletContext().getContextPath());
    w.print(image);
    w.println("\" style=\"float: right;\" width=\"" + width + "\" height=\"" + height + "\" alt=\"" + alt
            + "\" />");
}

From source file:net.ontopia.topicmaps.webed.impl.utils.TagUtils.java

public static void processWithVelocity(PageContext pageContext, String template_file, Writer writer,
        VelocityContext vc) {/*from  w ww.ja v a2  s.c o  m*/
    try {
        VelocityEngine vengine = getVelocityEngine(pageContext.getServletContext());
        vengine.mergeTemplate(template_file, org.apache.velocity.runtime.RuntimeSingleton
                .getString(Velocity.INPUT_ENCODING, Velocity.ENCODING_DEFAULT), vc, writer);
    } catch (Exception e) {
        throw new OntopiaRuntimeException(e);
    }
}

From source file:test.pl.chilldev.web.spring.context.SpringBeansJspPageMetaModelResolverTest.java

@Test(expected = PageMetaModelContextException.class)
public void getPageMetaModelWithoutBean() throws PageMetaModelContextException {
    GenericWebApplicationContext appContext = new GenericWebApplicationContext();
    appContext.refresh();/*from  www .j a va2 s.c  om*/
    PageContext jspContext = new MockPageContext();
    jspContext.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
            appContext);

    PageMetaModelResolver resolver = new SpringBeansJspPageMetaModelResolver();
    resolver.getPageMetaModel(jspContext);
}

From source file:test.pl.chilldev.web.spring.context.SpringBeansJspPageMetaModelResolverTest.java

@Test
public void getPageMetaModel() throws PageMetaModelContextException {
    PageContext jspContext = new MockPageContext();
    jspContext.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
            this.appContext);

    PageMetaModelResolver resolver = new SpringBeansJspPageMetaModelResolver();
    PageMetaModel page = new PageMetaModel();

    when(this.appContext.getBean(PageMetaModel.class)).thenReturn(page);

    assertSame(/*  ww w.j a va  2 s.c om*/
            "SpringBeansJspPageMetaModelResolver.getPageMetaModel() should return page model from Spring container.",
            page, resolver.getPageMetaModel(jspContext));
}

From source file:mercury.tags.SetInitialLocale.java

@Override
public void doTag() throws JspException {
    try {//from ww w . j  a v a  2  s . com
        PageContext pageContext = (PageContext) getJspContext();

        if (pageContext.getServletContext().getAttribute("FIRST_EXECUTION") != null) {
            pageContext.getServletContext().removeAttribute("FIRST_EXECUTION");
            pageContext.getSession().removeAttribute("LAST_VISITED_PAGE");
            pageContext.getSession().removeAttribute("MESSAGE_TEXT");
            pageContext.getSession().removeAttribute("MESSAGE_LEVEL");
            pageContext.getSession().setAttribute("MESSAGE_TEXT", "DIALOG_VOID");
            pageContext.getSession().setAttribute("MESSAGE_LEVEL", "NORMAL");
        }

        String lastVisitedPage = (String) pageContext.getSession().getAttribute("LAST_VISITED_PAGE");
        if (lastVisitedPage == null || lastVisitedPage.trim().equals("")) {
            lastVisitedPage = errorJsp;
            pageContext.getSession().setAttribute("LAST_VISITED_PAGE", lastVisitedPage);
        }

        if ((String) pageContext.getSession().getAttribute("I18N") == null) {
            String locale = pageContext.getRequest().getLocale().toString();
            Properties languages = (Properties) pageContext.getServletContext().getAttribute("LANGUAGES");

            if (StringUtils.isNotBlank(locale) && languages.containsKey(locale)) {
                pageContext.getSession().setAttribute("I18N", locale);
            } else {
                pageContext.getSession().setAttribute("I18N", defaultLanguage);
            }
        }
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
        throw new JspException("[mercury.SetInitialLocale.doTag()] Exception: " + ex.getMessage());
    }
}

From source file:gov.nih.nci.cabig.ctms.acegi.csm.web.CSMAccessControlTag.java

/**
 * Allows test cases to override where application context obtained from.
 * //  ww w .  j av a2  s. com
 * @param pageContext
 *            so the <code>ServletContext</code> can be accessed as
 *            required by Spring's <code>WebApplicationContextUtils</code>
 * 
 * @return the Spring application context (never <code>null</code>)
 */
protected ApplicationContext getContext(PageContext pageContext) {
    ServletContext servletContext = pageContext.getServletContext();

    return WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
}

From source file:de.u808.simpleinquest.web.tags.VersionInfoTag.java

@Override
public void doTag() throws JspException, IOException {
    PageContext pageContext = (PageContext) getJspContext();
    JspWriter out = pageContext.getOut();
    try {/*from  ww w  .  j  ava 2  s. c o  m*/
        String appServerHome = pageContext.getServletContext().getRealPath("/");

        File manifestFile = new File(appServerHome, "META-INF/MANIFEST.MF");

        Manifest mf = new Manifest();
        mf.read(new FileInputStream(manifestFile));

        Attributes atts = mf.getMainAttributes();

        out.println("<span id=\"version\"> (Revision " + atts.getValue("Implementation-Version") + " Build "
                + atts.getValue("Implementation-Build") + " Built-By " + atts.getValue("Built-By")
                + ")</span>");
    } catch (Exception e) {
        log.error("Tag error", e);
    }
}

From source file:net.mojodna.sprout.support.SpringBodyTagSupport.java

/**
 * Fetch ContextLoaderPlugIn's WebApplicationContext from the ServletContext,
 * falling back to the root WebApplicationContext (the usual case).
 * @return the WebApplicationContext//from   w ww  . j a v a  2s . c om
 * @throws IllegalStateException if no WebApplicationContext could be found
 * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
 * @see WebApplicationContextUtils#getWebApplicationContext
 */
protected WebApplicationContext initWebApplicationContext(final PageContext pageContext)
        throws IllegalStateException {
    final ServletContext sc = pageContext.getServletContext();
    WebApplicationContext wac = (WebApplicationContext) sc
            .getAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX);
    if (null == wac) {
        wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
    }
    return wac;
}