JOBS4TIMES jobs4timesLogo

Spring Web MVC Framework

Agenda :
  1. Introduction

First Spring Web Application :

Login.html
 <body>
 <form action="login.spring" >
 User Name : <input type="text" name="userName"> <br>
 Password: <input type="password" name="password"> <br>
 <input type="submit" value="Login">
 </form> 
</body>
LoginController.java
package com.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;


public class LoginController implements Controller {

LoginModel loginModel;
public void setLoginModel(LoginModel lm){
 loginModel=lm;
}
	
public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
String userName=request.getParameter("userName");
String password=request.getParameter("password");
System.out.println("userName : "+userName);
System.out.println("password : "+password);
String type = loginModel.getValidate(userName,password);

System.out.println("type : "+type);

if(type==null)
	return new ModelAndView("/login.html");
else if(type.equals("admin"))
	return new ModelAndView("/pages/admin.jsp");
else
	return new ModelAndView("/pages/user.jsp");

} //handleRequest
}
LoginModel.java
package com.controller;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

public class LoginModel {

private JdbcTemplate jdbcTemplate;

public LoginModel(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate=jdbcTemplate;
}

public String getValidate(String userName, String password) {
	
 String sql="select type from UserDetails where username=\'"
	         + userName+ "\' and password=\'"+password+ "\' ";
 
 try{
	return jdbcTemplate.queryForObject(sql, String.class);	
 }catch (EmptyResultDataAccessException e) {
	 return null;
} 

} //getValidate()

}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">	

<servlet>
<servlet-name>ds</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>	
<servlet-mapping>
<servlet-name>ds</servlet-name>
<url-pattern>*.spring</url-pattern>
</servlet-mapping>
  
</web-app>
ds-servlet.xml
<?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:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


<!-- configure DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
 <value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
 <value>jdbc:oracle:thin:@localhost:1521:xe</value>
</property>
<property name="username">
 <value>lms</value>
</property>
<property name="password">
 <value>scott</value>
</property>
</bean>

<!-- configure JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
 <constructor-arg>
  <ref local="dataSource" />
 </constructor-arg>
</bean>

<bean id="loginModel" class="com.controller.LoginModel">
 <constructor-arg>
  <ref local="jdbcTemplate"/>
 </constructor-arg>
</bean>

<bean id="loginController" class="com.controller.LoginController">
 <property name="loginModel">
  <ref local="loginModel"/>
 </property>
</bean>

<bean id="myUrlMapping" 
 class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
 <property name="mappings">
  <props>
    <prop key="/login.spring">loginController</prop>
  </props>
 </property>
</bean>

</beans>
adminHome.jsp
Wel come to Admin page <br/>

User Name : <%= request.getParameter("userName") %>
userHome.jsp
Wel come to User page <br/>

User Name : <%= request.getParameter("userName") %>
Data Base Code snoppet :
SQL> create table userdetails (username varchar2(15),
                  password varchar2(15),type varchar2(15));
                  
SQL> insert into userdetails values('ashok','scott','admin');

BACK