Skip to main content

EE Serlvet 3 Generating HTML output in Servlet

EE Serlvet 3 Generating HTML output in Servlet


If you just need to handle a handful of requests URI in your EE web module, then it might be easier to generate your own HTML response in your Servlet code instead of using a full blown template library. As part of my examples, I tried out a very simple Java DSL that generate html output when writing your own Serlvet. The code looks like this:

package zemian.servlet3example.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/index")
public class IndexServlet extends HtmlWriterServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HtmlWriter html = createHtmlWriter(req, resp);
        String message = getMessage(req, html);
       
        html.header()
            .h(1, "Welcome to Servlet 3 Example")
            .p("Lets explore Java Servlet 3.x Features.")
            .p(message)
            .ul(
                html.link("Index", "/index"),
                html.link("Hello", "/hello"),

                html.link("Sys Props", "/sys-props")
            )
            .footer();
    }

}

I wrote a base HtmlWriterServlet class that provide a method where you can get an instance of a HtmlWriter builder. The benefit of wrapping the HTML like builder is that its more easier to read and helps generate correct well form tags. For example the "ul" and "table" accepts Java List or Map object, and it generates the correct html tags.

Here is another example how I generate a table view of Java System Properties page with few lines of code:

package zemian.servlet3example.web;

import java.io.IOException;
import java.util.TreeMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/sys-props")
public class SysPropsServlet extends HtmlWriterServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HtmlWriter html = createHtmlWriter(req, resp);
        TreeMap sysProps = new TreeMap(System.getProperties());
        html.header()
            .h(1, "Java System Properties")
            .table(sysProps)
            .footer();
    }
}



The simple HtmlWriter class provide few html builder methods and it can help generate HTML links with relative context paths. You can easily further improve it to help generate more HTML code such as form tags etc.

Also, note that ServletResponse object let you have full control on writing custom responses, so you are not restricted to only returing HTML. You can write binary output such as PDF or even MP3 files. You simply need to control the Response Writer and the correct corresponding content mime type and size that will return.


You can get these code at servlet3-example

download file now

Popular posts from this blog

Empire Four Kingdoms Hack Unlimited Rubies Unlimited Gold Unlimited Wood Unlimited Stone Unlimited Food for Android iOS

Empire Four Kingdoms Hack Unlimited Rubies Unlimited Gold Unlimited Wood Unlimited Stone Unlimited Food for Android iOS Acest blog Link de aici Web Acest blog Link de aici Web Empire Four Kingdoms Hack , cheats, tool, trainer 100% working on Android and iOS that will give you Free Unlimited Rubies, Gold, Wood, Stone and Food. You can utilize any way you like the Rubies, Gold, Wood, Stone and Food generated by Empire Four Kingdoms Hack. Empire Four Kingdoms Hack work with Android and iPhone, iPad, iPod Touch, iOS device, Trainer is very simple to use on PC or Android APK and you can easily add Rubies, Gold, Wood, Stone and Food in your account with just a one clicks of Patch Game button. You will not find a better Hack or Cheats for Empire: Four Kingdoms on Android or iOS. This hack tool has been created by the-D2 Team(available only on the-D2.com) by...

Eclipse Android on Linux Mint 13 Maya

Eclipse Android on Linux Mint 13 Maya So far, my previous blogs on Linux Mint 13 has described how to install Java and the Android SDK. If you have followed the blogs, you will now be ready for the next step towards Android software development and that is to install a Java programming environment called Eclipse. Of course, first we have to install this (if it has not been installed yet). The install process is very simple - you can use either Synaptic or the command line to carry out this job. To use Synaptic, start Synaptic, enter your password, then type eclipse into the search box. The entry for Eclipse will appear - mark it for installation, and the rest of the additional items, and then click on the Apply button. Wait for download and install process to finish, close Synaptic and you are done! To use the command line, start a terminal, and type: sudo apt-get install eclipse Type Enter when prompted, wait for the packages to be downloaded and installed, close the terminal and you ...

Earth Defence Force 2 Portable JPN PSP

Earth Defence Force 2 Portable JPN PSP Blogger is a widely used free blogging platform owned by Google.Many people use this free platform to create high quality blogs while some others use it to create blogs just for the sake of fun.Let it be for any reason blogs are a great way to share content. But what is a blog actually? A blog is a website where users write stuffs on an ongoing basis.So, what is the difference between a normal website and a blog? There is not much difference between the two but a blog is a part of  a website that keeps updating with the latest information where as a website might not contain very recent information.Some may use website to share their brand or products,some use to create social interactivity,some for portfolios,etc. .There are many companies that offer you  to create a website for free.Some of them include webs,wix,weebly,etc.but not blogger because blogger is a blogging platform.But as always we have found a way around.This post will teac...