Gadgets

Saturday, 5 March 2016

Store the details into database using spring framework

Store the details in database using spring framework

 

Steps to create spring MVC web application:

1.      Load the required jar file.
2.      Create the request page to give input.
3.      Create the Controller class.
4.      Create the web.xml file.
5.      Create the spring configuration xml file.
6.      Create output page
7.      Run the project.



Load the required jar files:
Note: load the jar files in lib folder
·        commons-logging-1.1.1
·        org.springframework.asm-3.0.1
·        org.springframework.beans-3.0.1
·        org.springframework.context-3.0.1
·        org.springframework.core-3.0.1
·        org.springframework.expression-3.0.1
·        org.springframework.web-3.0.1
·        org.springframework.web.servlet-3.0.1
·        com.mysql.jdbc_5.1.5.jar (for mysql database)
Create the request page to give input: This file contains input fields.
index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Register here</h1>
<form action="./empsave.html" method="post">
<pre>
ID :   <input type="text" name="id"/>
Name : <input type="text" name="name"/>
Email :       <input type="text" name="email"/>
Address :     <textarea rows="3" cols="10" name="address"></textarea>
                     <input type="submit" value="save"/>
</pre>
</form>
</body>
</html>
Create the Controller class: in this file we get the details from input page and send the result to success page.
EmpController.java
package com.javathub;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class EmpController extends MultiActionController {
       //save
       public ModelAndView empsave(HttpServletRequest req,
                     HttpServletResponse res) throws Exception {    
              int id=Integer.parseInt(req.getParameter("id"));
              String name=req.getParameter("name");
              String email=req.getParameter("email");
              String address=req.getParameter("address");           
              Class.forName("com.mysql.jdbc.Driver");
              Connection con=DriverManager.getConnection("jdbc:mysql://localhost/employee","employee","12345");
              PreparedStatement ps=con.prepareStatement("insert into empdetails values(?,?,?,?)");
              ps.setInt(1,id);
              ps.setString(2,name);
              ps.setString(3,email);
              ps.setString(4,address);         
              int i=ps.executeUpdate();
              ModelAndView mav=null;
              if(i!=0){                 
                     return new ModelAndView("successpage");
              }
              else
                     return new ModelAndView("errorpage");          
              }     
       }

Create the web.xml file: in this file we are specifying the DispatcherServlet, it is act as front controller. This file forwards the input requests to DispatcherServlet.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>springMVC_login</display-name>
  <welcome-file-list>  
    <welcome-file>index.jsp</welcome-file>   
  </welcome-file-list>
  <servlet>
  <servlet-name>reg</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet> 
  <servlet-mapping>
  <servlet-name>reg</servlet-name>
  <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>
Create the spring configuration xml file: this file name should be (what we specified in web.xml)servletname-servlet.xml, here we define the handler mapping class, Controller class and configure the view resolver.
reg-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!--default handler mapping -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/empsave.html">empdetails</prop>
</props>
</property>
</bean>
<!-- define controller name -->
<bean id="empdetails" class="com.javathub.EmpController"></bean>
<!-- view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

Output page:

successpage.jsp
<h1>welcome to my page.</h1>
<p>your successfully registered...</p>


No comments:

Post a Comment