Creating a simple “HelloWorld” Spring servlet

This tutorial explains how to create a class that consumes a Spring bean and outputs a simple message via a servlet.

Prerequisites

Before you begin make sure you have downloaded and installed the following:

WebSphere Application Server V8.5.5
WebSphere Application Server Developer Tools for Eclipse Juno V8.5.5
Spring/SpringMVC v3.2.1 Integration

Create a runtime environment

  1. Select File > New > Other
  2. In the wizard dialog, type this filter text: server
  3. Select the Server wizard and click Next
  4. Choose the server type: WebSphere Application Server V8.5 Liberty Profile
  5. Set the server name: defaultServer
  6. Click Next
  7. Under the installation folder, enter the location where you installed the Liberty Profile runtime environment with the Spring Sample
  8. Click Finish

Create a Dynamic Web Project

  1. File > New > Dynamic Web Project
  2. Name it: WASDevSpringTutorial
  3. Set the target runtime as the Liberty Profile runtime environment you created
  4. Uncheck EAR Membership
  5. Click Finish

Create a Class to consume a Spring Bean

  1. Right-click WASDevSpringTutorial
  2. New > Class
  3. Java Package: com.ibm.wasdev.tutorial.spring
  4. Class Name: HelloWord
  5. Click Finish

Add the following code to the newly created HelloWorld class:

	   private String beanOutput;

	   public void setSpringBeans(String input){
		      beanOutput  = input;
		   }

	   public String getBeanValue(){
	      return "Output from Spring Bean : " + beanOutput;
	   }

Create a Spring Bean to be consumed

  1. Right-click Src
  2. New > Other > File
  3. Name: SpringBean.xml
  4. Click Finish

Add the following XML to the newly created SpringBean.xml file.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="helloWorld" class="com.ibm.wasdev.tutorial.spring.HelloWorld">
       <property name="SpringBeans" value="Hello World!"/>
   </bean>

</beans>

Take note of the following:

  • id="HelloWord" is the reference to the spring bean we wish to consume.
  • class="com.ibm.wasdev.tutorial.spring.HelloWorld" is pointing to the class that requires input from the Spring Bean.
  • name="SpringBeans" is used for setting the variable in the HelloWorld class; the variable is prefixed with “set” and followed by the property name e.g. “setSpringBeans”.
  • value="Hello World!" is the value we wish to pass to our servlet.

Reference the Spring libraries from the Sample

  1. Right click WASDevSpringTutorial
  2. Build Path > Configure Build Path
  3. Select the Libraries tab.
  4. Click Add External Libraries
  5. Navigate to the Liberty installation that contains the Spring Sample
  6. Navigate to the shared resources directory wlp/usr/shared/resource/
  7. Include the libraries found inside the SpringSampleLibs directory.
  8. Click OK…

Create a Servlet to display the value of the Spring Bean

  1. Right click the com.ibm.wasdev.tutorial.spring package
  2. New > Servlet
  3. Name: HelloWorldServlet
  4. Click Finish…

Inside the newly created servlet, under “doGet”, add the following:

	ApplicationContext context = new ClassPathXmlApplicationContext("SpringBean.xml");

	HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");

	response.getWriter().println(helloWorld.getBeanValue());

Update the configuration

  1. Open the servers server.xml file
  2. Remove the reference to the existing Sample App PhoneBookBySpringMVC

Add the following to the server.xml file

<webApplication id="WASDevSpringTutorial" location="WASDevSpringTutorial.war" name="WASDevSpringTutorial">
	<classloader commonLibraryRef="SpringSampleLibs" />
</webApplication>

Finally, run the application and ensure that the value of the bean is printed via the servlet.

  • Neeraj

    Thanks … this helped… The only document in the web that helps integrating Spring and WebSphere Liberty … Thanks

    • Samuel O’Donnell

      Thank you Neeraj, I am glad you found it useful.