Difference between revisions of "Northwind Hello World Servlet"

From RifidiWiki

Jump to: navigation, search
Line 1: Line 1:
This is Step 7 in the [[Northwind Tutorial|Northwind Application Tutorial]]<br>
+
----
Previous Step: [[Northwind Creating the Web Application | Step 6: Create the Web Application]]<br>
+
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 +
----
 +
=[http://exowufo.co.cc Under Construction! Please Visit Reserve Page. Page Will Be Available Shortly]=
 +
----
 +
=[http://exowufo.co.cc CLICK HERE]=
 +
----
 +
</div>
 +
This is Step 7 in the [[Northwind Tutorial|Northwind Application Tutorial]]&lt;br&gt;
 +
Previous Step: [[Northwind Creating the Web Application | Step 6: Create the Web Application]]&lt;br&gt;
 
Next Step: [[Northwind TagLocationService | Step 8: Write a Tag Location Service and JMS Listener]]
 
Next Step: [[Northwind TagLocationService | Step 8: Write a Tag Location Service and JMS Listener]]
 
===What you will learn===
 
===What you will learn===
Line 8: Line 16:
 
===Write a JSP===
 
===Write a JSP===
 
Create a new file in the jsp directory called taglocation.jsp.  Edit it as follows:
 
Create a new file in the jsp directory called taglocation.jsp.  Edit it as follows:
<pre>
+
&lt;pre&gt;
<%@ include file="/WEB-INF/jsp/include.jsp" %>
+
&lt;%@ include file=&quot;/WEB-INF/jsp/include.jsp&quot; %&gt;
Hello World at <fmt:formatDate value="${model.date}" pattern="MM.dd.yyyy" />
+
Hello World at &lt;fmt:formatDate value=&quot;${model.date}&quot; pattern=&quot;MM.dd.yyyy&quot; /&gt;
</pre>
+
&lt;/pre&gt;
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.
+
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 &quot;model&quot; instead of getting it from a JSP tag.
 
===Write a Controller===
 
===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:
+
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 &lt;tt?&gt;com.northwind.rfid.shipping.war&lt;/tt&gt;.  Create a new Java class called TagLocationController.  Edit it as follows:
<pre>
+
&lt;pre&gt;
 
package com.northwind.rfid.shipping.war;
 
package com.northwind.rfid.shipping.war;
  
Line 36: Line 44:
 
public ModelAndView handleRequest(HttpServletRequest arg0,
 
public ModelAndView handleRequest(HttpServletRequest arg0,
 
HttpServletResponse arg1) throws Exception {
 
HttpServletResponse arg1) throws Exception {
HashMap<String, Object> model = new HashMap<String, Object>();
+
HashMap&lt;String, Object&gt; model = new HashMap&lt;String, Object&gt;();
model.put("date", new Date(System.currentTimeMillis()));
+
model.put(&quot;date&quot;, new Date(System.currentTimeMillis()));
 
 
return new ModelAndView("/WEB-INF/jsp/taglocation.jsp", "model", model);
+
return new ModelAndView(&quot;/WEB-INF/jsp/taglocation.jsp&quot;, &quot;model&quot;, model);
 
}
 
}
  
 
}
 
}
</pre>
+
&lt;/pre&gt;
 
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.
 
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===
 
===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:
 
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:
<pre>
+
&lt;pre&gt;
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
+
&lt;web-app version=&quot;2.4&quot; xmlns=&quot;http://java.sun.com/xml/ns/j2ee&quot;
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  
+
xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/j2ee  
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
+
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;&gt;
<welcome-file-list>
+
&lt;welcome-file-list&gt;
<welcome-file>index.jsp</welcome-file>
+
&lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
</welcome-file-list>
+
&lt;/welcome-file-list&gt;
  
<jsp-config>
+
&lt;jsp-config&gt;
<taglib>
+
&lt;taglib&gt;
<taglib-uri>/spring</taglib-uri>
+
&lt;taglib-uri&gt;/spring&lt;/taglib-uri&gt;
<taglib-location>/WEB-INF/spring.tld</taglib-location>
+
&lt;taglib-location&gt;/WEB-INF/spring.tld&lt;/taglib-location&gt;
</taglib>
+
&lt;/taglib&gt;
</jsp-config>
+
&lt;/jsp-config&gt;
 
 
<servlet>
+
&lt;servlet&gt;
<servlet-name>NorthwindDemo</servlet-name>
+
&lt;servlet-name&gt;NorthwindDemo&lt;/servlet-name&gt;
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
+
&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
<load-on-startup>1</load-on-startup>
+
&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
</servlet>
+
&lt;/servlet&gt;
  
<servlet-mapping>
+
&lt;servlet-mapping&gt;
<servlet-name>NorthwindDemo</servlet-name>
+
&lt;servlet-name&gt;NorthwindDemo&lt;/servlet-name&gt;
<url-pattern>/taglocation.htm</url-pattern>
+
&lt;url-pattern&gt;/taglocation.htm&lt;/url-pattern&gt;
</servlet-mapping>
+
&lt;/servlet-mapping&gt;
 
 
</web-app>
+
&lt;/web-app&gt;
</pre>
+
&lt;/pre&gt;
 
The two things we did here:
 
The two things we did here:
 
# Told the web application about a servlet called NorthwindDemo.
 
# Told the web application about a servlet called NorthwindDemo.
Line 81: Line 89:
  
 
===Create a servlet xml===
 
===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:
+
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 &quot;-servlet.xml&quot; appended to it).  Make the servlet xml look like this:
<pre>
+
&lt;pre&gt;
<beans xmlns="http://www.springframework.org/schema/beans"
+
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi"
+
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:osgi=&quot;http://www.springframework.org/schema/osgi&quot;
xmlns:amq="http://activemq.apache.org/schema/core"
+
xmlns:amq=&quot;http://activemq.apache.org/schema/core&quot;
xsi:schemaLocation="http://www.springframework.org/schema/beans  
+
xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans  
 
META-INF/xsd/spring-beans-2.5.xsd
 
META-INF/xsd/spring-beans-2.5.xsd
 
     http://www.springframework.org/schema/osgi  
 
     http://www.springframework.org/schema/osgi  
     http://www.springframework.org/schema/osgi/spring-osgi.xsd">
+
     http://www.springframework.org/schema/osgi/spring-osgi.xsd&quot;&gt;
  
<bean name="/taglocation.htm" class="com.northwind.rfid.shipping.war.TagLocationController" />
+
&lt;bean name=&quot;/taglocation.htm&quot; class=&quot;com.northwind.rfid.shipping.war.TagLocationController&quot; /&gt;
  
</beans>
+
&lt;/beans&gt;
</pre>
+
&lt;/pre&gt;
  
 
===Modify index.jsp===
 
===Modify index.jsp===
 
The last thing to do is to modify our welcome file so that we are redirected to 'taglocation.htm' by default
 
The last thing to do is to modify our welcome file so that we are redirected to 'taglocation.htm' by default
<pre>
+
&lt;pre&gt;
<%@ include file="/WEB-INF/jsp/include.jsp" %>
+
&lt;%@ include file=&quot;/WEB-INF/jsp/include.jsp&quot; %&gt;
  
<%-- Redirected because we can't set the welcome page to a virtual URL. --%>
+
&lt;%-- Redirected because we can't set the welcome page to a virtual URL. --%&gt;
<c:redirect url="/taglocation.htm"/>
+
&lt;c:redirect url=&quot;/taglocation.htm&quot;/&gt;
</pre>
+
&lt;/pre&gt;
 
===Run It!===
 
===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.
 
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.

Revision as of 08:32, 24 November 2010


This is Step 7 in the Northwind Application Tutorial<br> Previous Step: Step 6: Create the Web Application<br> 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: <pre> <%@ include file="/WEB-INF/jsp/include.jsp" %> Hello World at <fmt:formatDate value="${model.date}" pattern="MM.dd.yyyy" /> </pre> 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: <pre> 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); }

} </pre> 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: <pre> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee&quot; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;>

<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> </pre> 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: <pre> <beans xmlns="http://www.springframework.org/schema/beans&quot; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:osgi="http://www.springframework.org/schema/osgi&quot; xmlns:amq="http://activemq.apache.org/schema/core&quot; 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&quot;>

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

</beans> </pre>

Modify index.jsp

The last thing to do is to modify our welcome file so that we are redirected to 'taglocation.htm' by default <pre> <%@ 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"/> </pre>

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