jReflectServer - HTML code snippets

Below you'll find some useful HTML related code snippets.

Serve files (setting document root)

jReflect acts as a normal file server and can serve any file, including default html pages. Just put files into the document root folder which can be configured using:

server.setDocRoot(Paths.get("/home/some/folder/docroot").toString());

If not set, the default document root folder is the current working directory (i.e. the directory from which jReflectServer was launched.

Initialize dynamic page from static html

Programmatic page generation can be difficult for huge pages. It might be useful to initialize a page from a static html in the document root folder or from inside any referenced jar (since version 1.0) and then to modify its content using the jsoup API:

1
2
3
4
5
6
7
public void generatePage(JReflectServer server, WebRequest request, WebResponse response) throws Exception {
    response.initFrom("html/static.html");
    WebDocument wd = response.getWebDocument();

    Element body = wd.getBody();
    body.append("<p>A new text block.</p>");
}

Modifications using element selections

Use the extensive API of integrated jsoup for jquery-like selections and to modify html or attributes:

1
2
3
4
5
Elements elements = body.select("h3 > a[href]");
for (Element next : elements)
{
    next.attr("href", "/StartPage");  // Modify all links after h3
}

The index page

There is one route reserved for your application's entry point:

1
2
3
4
@Route("index")
public void generatePage(JReflectServer server, WebRequest request, WebResponse response) throws Exception {
    ...
}

The "index" route (line 1) defines the page to be generated when directing the browser to http://localhost.

Session attributes

Every request will be associated with a new or existing session. Sessions are in-memory only. The session-ID will be stored in both the URI and a cookie. The session object containing all attributes can be retrieved by the WebRequest.getSession() method:

1
2
3
4
5
Session session = request.getSession();
// Get an attribute
Object value = session.getAttribute("testKey");
// Set an attribute
session.setAttribute("newKey", "newValue");

Session parameters can be configured using JReflectServer.setMaxSessions(...) and JReflectServer.setMaxSessionTime(...). The default number of concurrent sessions is 1024 (oldest one will be discarded). The default session time is 3600 seconds.

Request parameters (HTML forms)

Request parameters (string only, for files see the section below) sent by GET or POST are stored in a different map and can be retrieved by the WebRequest.getParameters() method.

Suppose you have an html form like this containing an input field:

1
2
3
4
<form action="/ResultPage" method="post" accept-charset="ISO-8859-1">
    <input type="text" name="test"/>
    <input type="submit" value="Submit">
</form>

Then you can get test request parameter with the key "test" using the following statement:

// Get a request parameter
String value = request.getParameters().get("test");

Handling file uploads (multipart/form-data)

Suppose you have an html form like this containing a file field:

1
2
3
4
<form action="/UploadPage" method="post" enctype="multipart/form-data">
    <input type="file" name="upload"/>
    <input type="submit" value="Submit">
</form>

Then you can access a file instance from the WebRequest.getFiles() method inside a generatePage implementation, providing the right key ("upload"):

FileItem fi = request.getFiles().get("upload");
fi.write(new File("/home/some/path/" + fi.getName()));

It's also possible to get an input stream using fi.getInputStream(). The Maximum allowed upload size can be set by JReflectServer.setMaxUploadSize(...). The Default size is unlimited (-1). If the size is exceeded, the response will be a Request Too Long (413) error.

Linking and redirecting

You can use relative links starting with a slash ("/"):

body.append("<a href=\"/StartPage\">Go to the start page.</a>");

 

Also, you can easily redirect the client to another location using the WebResponse.setRedirectUrl() method:

body.append("<p>Redirecting in 3 seconds...</p>");
response.setRedirectUrl(3, "/ResultPage");