Skip to main content
Last updated March 19, 2011 13:17, by jaenicke_c
Feedicon  

bitworld.web - Reference documentation and tutorial


Preparation

  1. Create a new Enterpries Application with an EJB module and a WAR module and name it MyApp or choose a name of your own.
  2. Add the bitworld.utils.bin-2.x.x.jar and bitworld.web.bin-2.x.x.jar files to the projects build path.

Build the tutorial application

The tutorial application includes in their final state all features of the bitworld.web library. But at first we will start with a basic setup.

Create your application servlet

The library is based on the abstract servlet class de.bitworld.web.BitworldServlet. You have to inherit this class. Use the following default method implementations in your servlet class. Name your new servlet class MyAppServlet.

package de.bitworld.myapp;

import de.bitworld.web.BitworldServlet;
import de.bitworld.web.User;
import java.io.IOException;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyAppServlet extends BitworldServlet {
   private static final long serialVersionUID = -2458733435586850526L;

   @Override public void addInjectableObjects(Set<Object> injectableObjects, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException {}

   @Override public void registerDomainsByClass(Set<Class<? extends Object>> domains) {}

   @Override public void setUser(User user, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException {}

   @Override public User getUser(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException {
      return null;
   }
}

Register your application servlet

Add your servlet class to the web.xml deployment descriptor and use the url pattern *.html to map to your servlet class.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>MyApp</servlet-name>
        <servlet-class>de.bitworld.myapp.MyAppServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyApp</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

Create a JSP file

Create a new folder named jsp in the WEB-INF directory of your WAR module and create a new JSP file in there. Name it hello.jsp.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

Create a domain class

Create the following java class in your WAR module.

package de.bitworld.myapp.domains;

import de.bitworld.web.DomainMethod;
import de.bitworld.web.RequestContext;
import de.bitworld.web.WebDomain;

@WebDomain
public class Navigation {

   @DomainMethod(URI="hello.html")
   public void index(RequestContext ctx) {
      ctx.forward("/WEB-INF/jsp/hello.jsp");
   }
}

Register your domain class

Modify your servlet class and add your domain class by using the registerDomainsByClass method.

@Override
public void registerDomainsByClass(Set<Class<? extends Object>> domains) {
   domains.add(Navigation.class);
}

Test your application

Run the WAR module and open the URL http://localhost:8080/MyApp-war/hello.html in your browser.

 
 
Close
loading
Please Confirm
Close