Some Samples of Servlet from Tomcat

Request Info

This example demonstrates that how to use properties file for i18n and i10n. Values of Strings in different languages are defined in different properties files, which are pure text files. Here is an example of this issue.

A config file *.properties is referred by servlet object via ResourceBundle.getBundle(String). config.properites is created for this demo and its content is:

hello.title=a request info demo
hello.helloworld=Hello, world!
hello.methodname=method name
hello.pathinfo=path info

Obviously, they are key-value pairs, we can refer them in this way:

ResourceBundle rb = ResourceBundle.getBundle("config");
String title = rb.getString("hello.title");

The advantage of this mechanism is that the value of strings can be changed directly in configuration files by one who does not know Java, this will make the program maintain easy and safe.

Source code appened as an attachement below.

Request Headers

To use javax.servlet.http.HttpServletRequest.getHeaders(String) can get all the values of the specified request header as an Enumeration of String objects. Just list them in my current enviroment:

host =
user-agent = Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv: Gecko/2008070208 Firefox/3.0.1
accept = text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
accept-language = zh-cn,zh;q=0.5
accept-encoding = gzip,deflate
accept-charset = gb2312,utf-8;q=0.7,*;q=0.7
keep-alive = 300
connection = keep-alive
cache-control = max-age=0

The code of doGet method is:

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("<head><title>request header</title></head>");
        Enumeration e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            String name = (String) e.nextElement();
            String value = request.getHeader(name);
            out.println(name + " = " + value);

Request Parameters

A HTML form can send infomation (parameter value) to remote http server and the server proccess them (compute) and reponse to browser, this servlet accepts two paramters and display to terminal.

In servlet, request.getParameter(String) gets parameter, and a PrintWriter object generated by response.getWriter() outputs parameter value to client browser.

Cookies and Session

Main Item: [Cookie and Session]

In Tomcat Cookies and Session are two seprate parts. I studied the code, in the simplest way to use them regardless what they mean are:


        Cookie[] cookies = request.getCookies();
        String dataName = request.getParameter("dataname");
        String dataValue = request.getParameter("datavalue");
        if (dataName != null && dataValue != null) {
            Cookie cookie = new Cookie(dataName, dataValue);


        HttpSession session = request.getSession(true);
        String dataName = request.getParameter("dataname");
        String dataValue = request.getParameter("datavalue");
        if(dataName != null && dataValue != null) {
            session.setAttribute(dataName, dataValue);

The three (request parameters, cookies, and sessions) examples will combines into one and be attached later.