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

Java Web Annotation


Summary

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.


Setup

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..

<servlet>
  <display-name>ControllerServlet</display-name>
  <servlet-name>ControllerServlet</servlet-name>
  <servlet-class>com.google.code.jwebannotation.WebAnnotationServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>ControllerServlet</servlet-name>
  <url-pattern>*.do</url-pattern>
  <url-pattern>/Index.do</url-pattern>
</servlet-mapping>
<listener>
  <listener-class>com.google.code.jwebannotation.WebAnnotationLifecycle</listener-class>
</listener>

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


Usage

@Request

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.

Parameters:

  • page = "/Help.do" (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 = "/Help.do")
public String handleHelp(HttpServletRequest request, HttpServletResponse response){
  return "help.jsp";
}

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

Example 2:

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

This will catch POST requests to /Login.do. 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.

@ErrorPage

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:

  @ErrorPage
  public String handleError(HttpServletRequest request, HttpServletResponse response, Exception e){
  	e.printStackTrace();
	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.

@Lifecycle

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.

Parameters:

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

Example 1:

@Lifecycle(Stage.STARTUP)
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