Thursday, 6 August 2015

Spring @RequestMapping Annotation

=> Based on this Annotation only Request will come to any method in the Controller.

=> We can mention that , the method is supporting either POST request or GET request

=> We can mention the value of the url 
(Ex : http://ip:port/projName/..../value)
For each method we have to differentiate the value

=> We can append variables with URL as Path Variables

=> We can append variables with URL as Request Parameters

Annotation 

@RequestMapping

import org.springframework.web.bind.annotation.RequestMapping;

How to specify the method is only supports either POST / GET ?

@RequestMapping(value="/callController1", method=RequestMethod.GET)

@RequestMapping(value="/callController1", method=RequestMethod.POST)

@RequestMapping(value="/callController1", method={RequestMethod.GET,RequestMethod.POST})
[It will support both GET and POST Requests]


@PathVariable Example

@RequestMapping(value="/callController2/{name}",method=RequestMethod.GET)

public ModelAndView method2(@PathVariable("name") String person) {
           // Write the Code whatever you want
}

Ex URL : http://localhost:8888/SpringMVC/spring/callController2/Ram

For More Path variables 

@RequestMapping(value="/callController2/{name}/one/{salary}",method=RequestMethod.GET)

Ex URL : http://localhost:8888/SpringMVC/spring/callController2/Ram/one/50000

@RequestParam with Params attribute

@RequestMapping(value="/callController3",params="id",method=RequestMethod.GET)
public ModelAndView method3(@RequestParam("id") String uid) {
// Code

}

Ex URL : http://localhost:8888/SpringMVC/spring/callController3?id=25

For Multiple Request Parameters

@RequestMapping(value="/callController5",params={"id","name"},method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView method5(@RequestParam("id") String uid,@RequestParam("name") String name) {
// Code

}

Ex URL : http://localhost:8888/SpringMVC/spring/callController5?id=25&name=Ram


Example :

package com.ram;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SpringMvcController {

//http://localhost:8888/SpringMVC/spring/springController
//By default it will support GET method
@RequestMapping(value="/springController")
public String method() {

return "returnPage";

}

//http://localhost:8888/SpringMVC/spring/callController1
@RequestMapping(value="/callController1",method=RequestMethod.GET)
public String method1(ModelMap model) {

model.addAttribute("key1", "Hi Ram");

return "returnPage1";

}

//http://localhost:8888/SpringMVC/spring/callController2/Ram
@RequestMapping(value="/callController2/{name}",method=RequestMethod.GET)
public ModelAndView method2(@PathVariable("name") String person) {

ModelAndView model = new ModelAndView();
model.setViewName("returnPage2");
model.addObject("msg1", person);
model.addObject("msg2", "This is Message2");

return model;

}

//http://localhost:8888/SpringMVC/spring/callController3?id=25
//It will suppoert Both POST and GET
@RequestMapping(value="/callController3",params="id",method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView method3(@RequestParam("id") String uid) {

ModelAndView model = new ModelAndView();
model.setViewName("returnPage3");
model.addObject("msg1", uid);

return model;

}



//http://localhost:8888/SpringMVC/spring/callController4
@RequestMapping(value="/callController4",method=RequestMethod.GET)
public ModelAndView method4() {

ModelAndView model = new ModelAndView("returnPage4");
model.addObject("msg5", "Hi Ram");

return model;

}

//http://localhost:8888/SpringMVC/spring/callController5?id=25&name=Ram
//It will suppoert Both POST and GET
@RequestMapping(value="/callController5",params={"id","name"},method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView method5(@RequestParam("id") String uid,@RequestParam("name") String name) {

ModelAndView model = new ModelAndView();
model.setViewName("returnPage5");
model.addObject("id", uid);
model.addObject("name", name);

return model;

}

}

spring-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:component-scan base-package="com.ram" />

<mvc:annotation-driven />

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

</beans>


Web.xml

<web-app> <display-name>Spring MVC</display-name> <welcome-file-list> <welcome-file>home.jsp</welcome-file> </welcome-file-list>
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/spring/*</url-pattern> </servlet-mapping> </web-app>

returnPage.jsp

<html> <body> <h1>Spring MVC</h1> </body> </html>

returnPage1.jsp

<html> <body> <h1>Spring MVC</h1> <h2>${key1}</h2> </body> </html>

returnPage2.jsp

<html> <body> <h1>Spring MVC</h1> <h2>${msg1}</h2> <h2>${msg2}</h2> </body> </html>

returnPage3.jsp

<html> <body> <h1>Spring MVC</h1> <h2>${msg1}</h2> </body> </html>

returnPage4.jsp

<html> <body> <h1>Spring MVC</h1> <h2>${msg5}</h2> </body> </html>

returnPage5.jsp

<html> <body> <h1>Spring MVC</h1> <h2>Id : ${id}</h2> <h2>Name : ${name}</h2> </body> </html>







Wednesday, 5 August 2015

JQuery Device Detector [Mobile Device]

JQuery Device Detector [Mobile Device / IPad / IPhone / Other]

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
            $(document).ready(function() {
                     fun();
            });
</script>

<script>
function fun() {

var device = navigator.userAgent;
var checker = {
      microsoft: device.match(/IEMobile/),
      ipad: device.match(/(iPad)/),
     iphone: device.match(/(iPhone)/),
     blackberry: device.match(/BlackBerry/),
     android: device.match(/Android/)
   };
   if (checker.microsoft){
    alert("Microsoft");
   }
   else if(checker.ipad) {
   alert("ipaD");
   }
   else if (checker.iphone){
       alert("iphone...");
   }
   else if (checker.blackberry){
       alert("BlackBerry");
   }
   else if(checker.android) {
    alert("Android");
 }
   else {
       alert("Unknown Device/Desktop .... ");
   }
};
</script>
</head>
<body>


</body>
</html>

Tuesday, 4 August 2015

JQuery Back button [Go to previous page]

For Go to previous page use following method

parent.history.back();

[OR]

document.referrer;

A.html

<html>
<body>
From Page A
<a href="C.html">Next</a>
</body>
</html>

B.html

<html>
<body>
From Page B
<a href="C.html">Next</a>
</body>
</html>

C.html

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>

$(document).ready(function(){
   $("#back").click(function() {
parent.history.back();
});
});


 /* $(document).ready(function(){
   $("#back").click(function() {
   var previousPage =  document.referrer;
window.location.href = previousPage;
});

}); */

</script>
</head>
<body>
From Page C ...
<button id="back">Back</button>
</body>
</html>

Sunday, 7 June 2015

Difference between keyup,keydown and keypress events

* keydown , keypress both the events are same , this events will rise/fire whenever we click the key button

* keyup event will rise whenever we click and release key button 


* keyup and keydown both are case insensitive 

It means a , A , other languages also that button will return always same value

* Control buttons like Home,Delete,PageUp,PageDown,Backspace,Space,Tab,CapsLock,Shift,Ctr,Alt,Arrow button will work only for keydown and keyup event , these buttons will not work with keypress event


* keypress event will return the keyboard values based on the language you selected and it depends on case sensitive.


Key Down Event

<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function() {
$("#keydownId").keydown(function(e) {
functionOne(e);
});
});
</script>
<script>
function functionOne(key) {
alert(key.which);
}
</script>
</head>
<body>
<div>
KeyDown Event <input type="text" id="keydownId"/>
</div>
</body>
</html>

Key Up Event

<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function() {
$("#keyupId").keyup(function(e) {
functionOne(e);
});
$("#getVal").click(function(e) {
var val = $("#keyupId").val();
alert(val);
});
});
</script>
<script>
function functionOne(key) {
alert(key.which);
}
</script>
</head>
<body>
<div>
KeyUp Event <input type="text" id="keyupId"/>
Click to Get Value <button id="getVal">Get Val</button>
</div>
</body>
</html>

Key Press Event

<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function() {
$("#keypressId").keypress(function(e) {
functionOne(e);
});
});
</script>
<script>
function functionOne(key) {
alert(key.which);
}
</script>
</head>
<body>
<div>
KeyPress Event <input type="text" id="keypressId"/>
</div>
</body>
</html>

All Ajax Methods Example

* Instead of  Ram.txt file use your own text file , Write any Data

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
//These states for Loading files/images only
    $(document).ajaxError(function(){
           //If fail is not loading , this method will work
            alert("Error occured");
        });
    $(document).ajaxStart(function(){
            //At the time of ajax starting
            alert("Started");
        });
    $(document).ajaxStop(function(){
           at the time of ajax ending
            alert("Stopped");
        });
    $(document).ajaxSend(function(){
           //After ajax request start , request will go to server
            alert("Sended");
        });
    $(document).ajaxComplete(function(){
            //If The Ajax Request is success/failure , always it will work
            alert("Ajax Completed");
        });
    $(document).ajaxSuccess(function(){
           //For success only
            alert("Ajax Success");
        });
        $("button").click(function(){
            $(".main").load("Ram.txt");
        });
        $("#pid1").click(function(){
            $("#pid1").load("Ram.txt");
        });
        $(".pid2").click(function(){
            $(".pid2").load("Ram.txt");
        });
});
</script>
</head>
<body>
<div class="main">
<button>Get External Data</button>
<p id="pid1">Paragraph 1</p>
<p class="pid2">Paragraph 2</p>
</div>
</body>
</html>

Sunday, 8 February 2015

CREATE and INSERT Queries

=> Create Table
 create table employee(no integer,name varchar2(15));

=>Crete Table with Primary key
 create table employee(no int primary key,name varchar2(15));

=>Crete Table with different Data types

 create table student(a int,b integer,c long,d float,e numeric(7,2),f number(7,2),g dec(7,2),h decimal(7,2),i varchar2(15),j date);

* desc student

 Name                                      Null?    Type
 ----------------------------------------- --------

 A                                                  NUMBER(38)
 B                                                  NUMBER(38)
 C                                                  LONG
 D                                                  FLOAT(126)
 E                                                  NUMBER(7,2)
 F                                                  NUMBER(7,2)
 G                                                  NUMBER(7,2)
 H                                                  NUMBER(7,2)
 I                                                  VARCHAR2(15)
 J                                                  DATE


* insert row into student table

insert into student values(100000000000,20000000,300000000,111.5,15000.00,15000.00,15000.00,15000.00,'ram',TO_DATE('1988/10/28','yyyy/mm/dd'));




Login Application

Sample View

Login Page

 If Login Fail

If Login Success


 Development

1) index.html

 

<form action="/LoginApplication/login">
Name:<input type="text" name="user" value="Ram"/>
Password:<input type="password" name="pwd" value="Ram"/>
<input type="submit" value="Login">
</form>
 

 

2)success.html

 

Login Success<br>
<a href="./index.html">Login</a>

 

3)fail.html

Login Failure<br>
<a href="./index.html">Login</a>

 

4)LoginServlet.java

 

package com.ram;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {
   
    RequestDispatcher rd = null;
    boolean status;
      
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       
        String user = request.getParameter("user");
        String pwd = request.getParameter("pwd");
        UserDTO userDTO = new UserDTO();
        userDTO.setName(user);
        userDTO.setPwd(pwd);
       
        UserDAO userDAO = new UserDAO();
        status = userDAO.checkLogin(userDTO);
        if(status) {
            rd = request.getRequestDispatcher("success.html");
            rd.forward(request, response);
        } else {
            rd = request.getRequestDispatcher("fail.html");
            rd.forward(request, response);
        }
       
       
    }

}
 

 

5)UserDAO.java

 

package com.ram;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UserDAO {
   
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    boolean status;
    String name = null;
    String pwd = null;
   
    public boolean checkLogin(UserDTO userDTO) {
       
        name = userDTO.getName();
        pwd = userDTO.getPwd();
       
        try {
           
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","ram","ram");
            stmt=con.createStatement();
            rs=stmt.executeQuery("select * from users where NAME='"+name+"' and PWD='"+pwd+"'");
           
            if(rs.next()) {
                status = true;
            }
           
           
        } catch (SQLException e) {
            e.printStackTrace();
        }
       
        return status;
       
        }
    }

 

 

6)UserDTO.java

 

package com.ram;

public class UserDTO {
   
    String name;
    String pwd;
   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
   
   

}
 

7)web.xml


<web-app>
  <display-name>LoginApplication</display-name>


  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>


  <servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>com.ram.LoginServlet</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>


</web-app>

  8)Use ojdbc14.jar

9)Deployment