This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • cviewer-javaee6 - Programmatically register servlets and filters

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

Wiki Markup
{scrollbar}

Application Overview

The cviewer-javaee6 sample includes a JSP file that calls a servlet. A form allows the user to enter a java class name.Then a filter will catch the class name and turn null or invalid class name into fully qualified class name. The servlet uses java reflection to extract information about the class which, in turn, is returned to the user.

...

Information about the project sucha as module's unique identification, dependencies is described inside the <sys:environment/> tags. It is a good practise to give a module an unique identification, so that it can later be referenced by some other deployable application. This module is in the group org.apache.geronimo.samples. The path specified in the <context-root> tag will be the entry point of this web application. Therefore you can access this web application at http://<hostname>:<port>/cviewer-javaee6.

...

...


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1"
         xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.2"
         xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0"
         xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">

  <sys:environment>
    <sys:moduleId>
      <sys:groupId>org.apache.geronimo.samples</sys:groupId>
      <sys:artifactId>cviewer-javaee6</sys:artifactId>
      <sys:version>${version}</sys:version>
      <sys:type>car</sys:type>
    </sys:moduleId>
    <sys:dependencies/>
    <sys:hidden-classes/>
    <sys:non-overridable-classes/>
  </sys:environment>

  <context-root>/cviewer-javaee6</context-root>

</web-app>

CviewerServletListener.java is a web listener which implements ServletContextListen,it will programmatically register a servlet and filter.

...

  • addFilter("CViewerFilter","org.apache.geronimo.samples.javaee6.cviewer.CviewerFilter");
    The first parameter CviewerFilter is the filter name and the second parameter org.apache.geronimo.samples.javaee6.cviewer.CviewerFilter is the corresponding filter class name. The addFilter method allows the application to declare a filter pro-grammatically. It adds the filter with the given name and class name to the web application.

...

...


package org.apache.geronimo.samples.javaee6.cviewer;

...

@WebListener()
public class CviewerServletListener implements ServletContextListener {

	public void contextInitialized(ServletContextEvent sce) {
		ServletContext sc = sce.getServletContext();
		ServletRegistration sr = sc.addServlet("ClassViewer","org.apache.geronimo.samples.javaee6.cviewer.CViewerServlet");
		sr.addMapping("/ClassViewer");
		FilterRegistration fr = sc.addFilter("CViewerFilter","org.apache.geronimo.samples.javaee6.cviewer.CviewerFilter");
		fr.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true,"ClassViewer");

	}
...
}

Get Source Code

Please reference Samples General Information for information on obtaining and building the source for this and other samples.

...

mvn clean install

...