RSS Feed Subscribe to RSS Feed

 

Spring MVC Hello World

There is a good Getting Started with Spring MVC blog post over on the Spring team blog.

I have created several Spring MVC projects for both work and play, and am attaching my own simple version of the HelloWorld example here, based on the Spring blog example.
Find my maven ready source here.
Like my previous JSP/Servlet example, I find these templates useful for getting prototypes up and running.

Tags: , , , , , ,

HelloWorld: JSP -> Servlet

A while ago, I posted a simple HelloWorld Html->Servlet->JSP setup. Even though the html part is unnecessary (a JSP can obviously handle that alone), I deliberately included it as a template for Html/Servlet/JSP interaction. I have used it a bunch of times to get simple Proof of Concept projects up and running, or as a basis to create a simple browser interface to some server side process.

Most of the more recent projects I have used it on however have used straight JSP->Servlet, with no .html file involved. So I am now posting that setup, including some simple JSTL EL and conditional logic.

Read more

Tags: , ,

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
HelloWorldServlet.java


package com.helloworld;

import java.io.IOException;

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
HelloWorld.jsp:


Hello ${msg}

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




	
		HelloWorld.html
	
	
	
		HelloWorldServlet
		com.helloworld.HelloWorldServlet
	
	
	
		HelloWorldServlet
		/HelloWorldServlet
	
	



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: , , ,