Northwind Hello World Servlet

From RifidiWiki

Jump to: navigation, search

This is Step 7 in the Northwind Application Tutorial
Previous Step: Step 6: Create the Web Application
Next Step: Step 8: Write a Tag Location Service and JMS Listener

What you will learn

  • How to use Spring's MVC Controller
  • How to write a servlet
  • How to write a JSP

Write a JSP

Create a new file in the jsp directory called taglocation.jsp. Edit it as follows:

<%@ include file="/WEB-INF/jsp/include.jsp" %>
Hello World at <fmt:formatDate value="${model.date}" pattern="MM.dd.yyyy" />

As you can see, this does the same thing as the index page, except it will get the date passed in from the Contoller in the "model" instead of getting it from a JSP tag.

Write a Controller

The next thing to do is to create the controller. The controller's purpose is to pass in a model to the view. In this case, our model is simply a date object. However, this will change when we hook up our web app to our RFID application. For now, create a new package called <tt?>com.northwind.rfid.shipping.war</tt>. Create a new Java class called TagLocationController. Edit it as follows:

package com.northwind.rfid.shipping.war;

import java.util.Date;
import java.util.HashMap;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * @author Kyle Neumeier - kyle@pramari.com
 *
 */
public class TagLocationController implements Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		HashMap<String, Object> model = new HashMap<String, Object>();
		model.put("date", new Date(System.currentTimeMillis()));
		
		return new ModelAndView("/WEB-INF/jsp/taglocation.jsp", "model", model);
	}

}

What happens now is that anytime a web page that is controlled by this controller is loaded, the handleRequest method will be invoked. This method simply passes in a model object to the proper jsp.

Modify the web.XML

Now we need to modify the web.xml so that the controller will be loaded at the proper time. The web.xml should now look like this:

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<jsp-config>
		<taglib>
			<taglib-uri>/spring</taglib-uri>
			<taglib-location>/WEB-INF/spring.tld</taglib-location>
		</taglib>
	</jsp-config>
	
	<servlet>
		<servlet-name>NorthwindDemo</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>NorthwindDemo</servlet-name>
		<url-pattern>/taglocation.htm</url-pattern>
	</servlet-mapping>
	
</web-app>

The two things we did here:

  1. Told the web application about a servlet called NorthwindDemo.
  2. Told the web application to invoke the NorthwindDemo servlet whenever a page called 'taglocation.htm' is requested.

Create a servlet xml

Now we need to create a servlet XML that controls how the NorthwindDemo servlet behaves. Create a new file called NorthwindDemo-servlet.xml in the WEB-INF folder (the name of the file must be the name of the servlet with "-servlet.xml" appended to it). Make the servlet xml look like this:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi"
	xmlns:amq="http://activemq.apache.org/schema/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	META-INF/xsd/spring-beans-2.5.xsd
    http://www.springframework.org/schema/osgi 
    http://www.springframework.org/schema/osgi/spring-osgi.xsd">

	<bean name="/taglocation.htm" class="com.northwind.rfid.shipping.war.TagLocationController" />

</beans>

Modify index.jsp

The last thing to do is to modify our welcome file so that we are redirected to 'taglocation.htm' by default

<%@ include file="/WEB-INF/jsp/include.jsp" %>

<%-- Redirected because we can't set the welcome page to a virtual URL. --%>
<c:redirect url="/taglocation.htm"/>

Run It!

When you run the web application again (or use the 'update' trick) and point your browser to http://127.0.0.1:8080/NORTHWIND-DEMO, you should see the same Hello World message as last time. This time however, the back end is using Spring's Web MVC framework.

Personal tools