RSS Feed Subscribe to RSS Feed


HelloWorld: Html -> Servlet -> JSP

A couple of times recently I have had to setup up some very simple code to display on the client side (browser) some values created on the server side based on basic user input.

This has been part of some prototype/proof-of-concept work. I am posting my basic setup as an example in the form of the ubiquitous HelloWorld app. It uses a Html page to receive user input, which is submitted to a Servlet, processed, and the response displayed in a JSP using EL (Expression Language).

Obviously this could be done in various ways, including just JSPs, but this example acts as a template of Html/Servlet/JSP interaction.

1) Create a Html page called HelloWorld.html

Greeting Setup - Hello Who?

Enter greeting recipient:

2) Create a servlet to receive the user input

package com.helloworld;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorldServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		//get inputs		
		String msg = req.getParameter("msg");
		//any additional processing can be done here
		//set outputs
		req.setAttribute("msg", msg);
		//forward to jsp
		req.getRequestDispatcher("/HelloWorld.jsp").forward(req, res); 


3) Create a JSP to display the response

Hello ${msg}

4) Create the web application’s deployment descriptor file:


That’s it.

The easiest way I have found for this kind of development is to create in Eclipse as a Dynamic Web Project (available via the Web Tools Platform that comes with the in the “Eclipse IDE for Java EE Developers” package). When you have carried out the above steps, just right click the project -> Run As -> Run on Server (assuming you have a server like Tomcat or GlassFish setup in Eclipse…).

I have attached the source in a war file.

Tags: , , ,

Leave a Reply