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

Download Mozilla Firefox 50 7 Offline Installer for Windows Mac and Linux

Download Mozilla Firefox 50 7 Offline Installer for Windows Mac and Linux Download Mozilla Firefox 50.7 Offline Installer for Windows 32 / 64 Bit / Mac / Linux - Mozilla Firefox released Firefox 50 on 2016. Firefox was as soon as the best internet browser as for recognition in terms of international consumption when compared to the leading web browser like Internet Traveler (IE) and Google Chrome, given that its launch in Nov 2004. Download the Latest Mozilla Firefox Offline Installer Software Full Setup Free Download. Offline Installer help you from getting a roll back if internet connection is lost. Skype Offline Installer is very Easy to install as you can install it on the computer without internet  connection. Mozilla Firefox 50.7  Offline Installer have all the required files in one Installer package. So just Download the  Mozilla Firefox  50.7  Offline Installer Full Setup Free Download and save the Time. Mozilla Firefox  may be the Among the best p...

ElasticSearch query with AND and OR criteria

ElasticSearch query with AND and OR criteria Frequently when we will need to construct query to elasticsearch which bear AND and OR criteria similar to the following SQL statements: Statement 1: SELECT * FROM tblOrder WHERE orderDate=2015-01-01 14:00:00 AND customerID=29439; Statement 2: SELECT * FROM tblOrder WHERE orderDate=2015-01-01 14:00:00 OR customerID=29439; Statement 3: SELECT * FROM tblOrder WHERE orderDate <= 2015-01-01 14:00:00 AND customerID=29439; Statement 4: SELECT * FROM tblOrder WHERE (orderDate=2015-01-01 14:00:00 AND customerID=29439) OR customerID = 20991; Statement 5: SELECT * FROM tblOrder WHERE orderDate=2015-01-01 14:00:00 AND (customerID=29439 OR customerID = 20991); In ElasticSearch, we use "must" and "should" in place of AND and OR and "bool" in place of WHERE. Suppose in our ElasticSearch (at 179.168.0.1:9200), we have indexed documents having the following structures at index myOrder/myOrder: { "orderID" : xxxxx, ...

Download MISSION IMPOSSIBLE GHOST PROTOCOL

Download MISSION IMPOSSIBLE GHOST PROTOCOL Download MISSION: IMPOSSIBLE - GHOST PROTOCOL -  Dated Released : 21 December 2011 Quality :  FULL CAM READNFO XviD  |Kualitas Buruk Info : imdb.com/title/tt1229238/ IMDB Rating : 8.0 (3,731 users) Star : Tom Cruise, Jeremy Renner, Paula Patton Genre : Action | Adventure | Thriller ---------------------------------------- Download Film By: icinema3satu.com download  file  now