My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Wiki pages

Java Web Annotation


This project was made with the intention of removing config settings from your web.xml file into annotations. The reason for this was because some projects contained many servlets which led to a huge web.xml file.


jWebAnnotation has the following dependencies

  • commons-logging-1.1.1.jar
  • javassist.jar
  • scannotation-1.0.2.jar
  • commons-beanutils-core-1.8.3.jar

To use jWebAnnotations place the following in your web.xml file..


The above code will catch all pages matching '*.do'. In order to catch the first page add the following welcome file <welcome-file></welcome-file>.



Function: This is used to catch a request. A replacement of a servlet or any other "page".

Definition: Placed above a method that is to be called when a specific path is called.


  • page = "/" (manditory) - the url mask to catch
  • method = HttpMethod.GET | HttpMethod.POST | HttpMethod.ALL (optional) - the type of request to catch. Default is GET

Example 1:

@Request(page = "/")
public String handleHelp(HttpServletRequest request, HttpServletResponse response){
  return "help.jsp";

This will catch GET requests to / Once this method returns with help.jsp the framework will forward the request to the dispatcher.

Example 2:

@Request(page = "/", method = HttpMethod.POST)
public String handleHelp(HttpServletRequest request, HttpServletResponse response, MyBean bean){
  return null;

This will catch POST requests to / The framework will go through all parameters on the request and populate the bean if their variable names are the same as the parameter name and there is an appropriate setter. This method returns null so the framework will not do anything once it is complete.

Notes: It is important to note that only ONE instance of each object is created, for example if there are three @Request annotations in a class, only one object of that class will be created and used. Therefore make sure to observe thread safety rules.


Function: This is used to catch any exceptions caused in any @Request.

Definition: Placed above a method that is to be called when an exception occurs.

Parameters: None.

Example 1:

  public String handleError(HttpServletRequest request, HttpServletResponse response, Exception e){
	request.setAttribute("exception", e.toString());
 	return "WEB-INF/pages/errorPage.jsp";

This will be called when an exception occurs in an @Request. The return will then be redirected.

Notes: VERY IMPORTANT; you can only specify this annotation once! If you have it more than once the framework will not startup. For security reasons it is really good practice to implement this as one would not want a user to see a stacktrace (which is default) on a webpage.


Function: This is used to indicate which methods should be called when the web application starts up and shuts down

Definition: Placed above a method that is to be called.


  • Stage.STARTUP | Stage.SHUTDOWN (manditory) - to catch the startup or shutdown

Example 1:

public void startup(ServletContextEvent arg0) {
  System.out.println("Hello world!");

This code will be executed when the web application is started.

Notes: None

Powered by Google Project Hosting