Difference between revisions of "Northwind Hello World Servlet"
From RifidiWiki
Line 1: | Line 1: | ||
− | This is Step 7 in the [[Northwind Tutorial|Northwind Application Tutorial]] | + | ---- |
− | Previous Step: [[Northwind Creating the Web Application | Step 6: Create the Web Application]] | + | <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]]<br> | ||
+ | Previous Step: [[Northwind Creating the Web Application | Step 6: Create the Web Application]]<br> | ||
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> | |
− | + | <%@ include file="/WEB-INF/jsp/include.jsp" %> | |
− | Hello World at | + | 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 | + | 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=== | ===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 | + | 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; | 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 | + | HashMap<String, Object> model = new HashMap<String, Object>(); |
− | model.put( | + | model.put("date", new Date(System.currentTimeMillis())); |
− | return new ModelAndView( | + | 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. | 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> | |
− | + | <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" | |
− | xmlns:xsi= | + | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
− | xsi:schemaLocation= | + | xsi:schemaLocation="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"> |
− | + | <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: | 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 | + | 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" | |
− | xmlns:xsi= | + | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi" |
− | xmlns:amq= | + | xmlns:amq="http://activemq.apache.org/schema/core" |
− | xsi:schemaLocation= | + | xsi:schemaLocation="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"> |
− | + | <bean name="/taglocation.htm" class="com.northwind.rfid.shipping.war.TagLocationController" /> | |
− | + | </beans> | |
− | + | </pre> | |
===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> | |
− | + | <%@ 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!=== | ===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" 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> </pre> The two things we did here:
- Told the web application about a servlet called NorthwindDemo.
- 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" 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> </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.