I am trying to implement applicationContextAware in my servlet.I have the data from client side coming to my servlet.From my servlet I need to pass it to the beans which has getters and setters.I have my DAO s where I have MYSQL operations.
My applicationContext.xml has
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/bazaar_admin_portal" />
<property name="username" value="usrnm" />
<property name="password" value="pwd" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg index="0">
<ref bean="dataSource" />
</constructor-arg>
</bean>
<bean
class="org.dao.impl.TestDAOimpl"
id="TestDAO">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
My web.xml contains
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<description></description>
<display-name>TestServlet</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.controllers.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
And in my TestServlet under doPost method
private static ApplicationContext applicationContext = null;
public void setApplicationContext(ApplicationContext ctx)
throws BeansException {
applicationContext = ctx;
I have getters and setters class Test.Also interface TestDAO and TestDAOimpl class which implements the interface.
I want to know how do I pass the data from my servlet to the spring side…i.e set the data which will enable the TestDAOimpl to insert into my DB.
Thanks
Are you sure you don’t want to use Spring WebMVC? It will handle your problem automatically.
Then try this in you POST Method (It’s quite slow, init it lazily):
applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());