Session Http Server Tutorial
This tutorial demonstrates maintaining state within the HTTP session.
The example application for this tutorial will show a list of postings as follows:

The template for displaying the posts and adding posts is as follows:
<html>
<body>
<table border="1">
<!-- {posts} -->
<tr>
<td>${text}</td>
</tr>
<!-- {endPosts} -->
</table>
<form action="#{post}" method="POST">
<input name="text" type="text" />
<input type="submit" value="Post" />
</form>
</body>
</html>
Session objects within WoOF are dependency injected. The following POJO shows the annotation necessary to dependency inject it and also store it within the HTTP session. Note HTTP session objects must also be serializable.
@HttpSessionStateful
public class Posts implements Serializable {
private final List<Post> posts = new ArrayList<Post>();
public void addPost(Post post) {
this.posts.add(post);
}
public Post[] getPosts() {
return this.posts.toArray(new Post[this.posts.size()]);
}
}
WoOF will do the following:
The logic for the template is as follows. The Posts parameter is dependency injected following the above steps.
public class TemplateLogic {
public Post[] getPosts(Posts posts) {
return posts.getPosts();
}
public void post(Post post, Posts posts) {
posts.addPost(post);
}
}
The dependency injection provides compile safe code without requiring to:
WoOF will provide a unique name based on the object's type to bind the object within the HTTP session. This can however be overridden by providing a name to the annotation.
For completeness the Post object containing the posted information is as follows:
@HttpParameters
public class Post implements Serializable {
private String text;
public void setText(String text) {
this.text = text;
}
public String getText() {
return this.text;
}
}
WoOF again allows easy unit testing by it's dependency injection of POJOs:
public void testTemplateLogic() {
Posts session = new Posts();
TemplateLogic logic = new TemplateLogic();
// Add post to session via template logic
Post post = new Post();
post.setText("Test post");
logic.post(post, session);
assertEquals("Ensure post added", post, session.getPosts()[0]);
// Ensure post provided from template logic
assertEquals("Ensure post available", post, logic.getPosts(session)[0]);
}
The next tutorial will look at exception handling.