Announcement Announcement Module
Collapse
No announcement yet.
HttpServletBean How are bean accessed Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HttpServletBean How are bean accessed

    I have small class that extends HttpServletBean. It is loaded directly Tomcat via web.xml. How do I get access to the beans that are context? Here are some of the things I've tried.

    1.) Rely on loading of properties from the context xml.

    2.) Load a property from the context via web.xml argument (shown below)

    3.) Override initServletBean(). The api doc says you can, Eclipse says it's final. This appears inconsistent.

    Anyone have a solution for this?

    Thanks for considering it.
    Tom Sheehan

    Class Code:
    Code:
    package net.fubar.mvc;
    
    import java.util.Enumeration;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.FrameworkServlet;
    
    public class Fuu extends FrameworkServlet {
    	private static final long serialVersionUID = 1L;
    	
    	private String systemMessage = null;
    	
    	@Override
    	protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
    		logger.info("System Message: " + systemMessage);
    		if (request == null) {
    			logger.info("nothing to process,  request  object null");
    		} else {
    			Enumeration reqParams = request.getParameterNames();
    			while (reqParams.hasMoreElements()) {
    				System.out.println(
    						request.getParameter(reqParams.nextElement().toString())
    				);
    			}
    		}
    		
    	}
    
    	public String getSystemMessage() {
    		return systemMessage;
    	}
    
    	public void setSystemMessage(String systemMessage) {
    		this.systemMessage = systemMessage;
    	}
    }

    web.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" 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">
    	<display-name>
    	FooBar</display-name>
    	
    	<servlet>
    		<description>
    		FrameworkServlet example</description>
    		<display-name>
    		Fuu</display-name>
    		<servlet-name>Fuu</servlet-name>
    		<servlet-class>
    		net.fubar.mvc.Fuu</servlet-class>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>Fuu</servlet-name>
    		<url-pattern>/Fuu</url-pattern>
    	</servlet-mapping>
    	<context-param>
    		<param-name>systemMessage</param-name>
    		<param-value>serverMessage</param-value>
    	</context-param>
    	
    	<welcome-file-list>
    		<welcome-file>index.html</welcome-file>
    	</welcome-file-list>
    </web-app>
    Fuu-servlet.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:jee="http://www.springframework.org/schema/jee"
    	xmlns:lang="http://www.springframework.org/schema/lang"
    	xmlns:util="http://www.springframework.org/schema/util"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
    		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
    	
    
    	<bean id="serverMessage" class="java.lang.String">
    		<constructor-arg value="Hello Servlet World"/>
    	</bean>
    	
    	<bean name="/Fuu" id="fooServlet" class="net.fubar.mvc.Fuu">
    		<property name="systemMessage">
    			<ref bean="serverMessage"/>
    		</property>
    	</bean>
    	
    </beans>
    Last edited by Tom Sheehan; Sep 25th, 2007, 02:25 PM. Reason: fixed spelling error
Working...
X