Saturday, 26 December 2015

Spring redirect using mvc:redirect-view-controller tag


This is an extension of my previous post on 
So if you have directly landed here I would highly recommend to read it first for setup.  In this post we will essentially see how can we redirect one URL to other using mvc:redirect-view-controller tag configuration.

Spring redirect using mvc:redirect-view-controller tag

Basic setup remains the same as that of previous post

NOTE : For this Spring 4.1+ is required! So change your ivy dependency accordingly.

        <dependency org="org.springframework" name="spring-webmvc"
            <exclude org="javax.servlet" name="javax.servlet-api" />
            <exclude org="javax.servlet.jsp" name="jsp-api" />
            <exclude org="javax.el" name="javax.el-api" />

Now only change that you need to do in your spring configuration is as follows - 

    <mvc:redirect-view-controller redirect-url="/TestWebProject/newtest" path="/oldtest" status-code="308" keep-query-params="true"/>
    <mvc:view-controller path="/newtest" view-name="test"/>

All it says is if you get a URL "/oldtest" redirect it to new URL "/newtest" and the 2nd line states if you get url "/newtest" then render view with logical name test which will again render the same JSP test.jsp as per the InternalResourceViewResolver.

Notice the redirect with 308 response status.

Alternate way to redirect is using

<mvc:view-controller path="/oldtest" view-name="redirect:newtest"/>
 <mvc:view-controller path="/newtest" view-name="test"/>

Again notices the 302 response code.

NOTE : It is not desirable to use version in xsd declaration. Spring will use highest version in the project's dependencies if version not given.

How to install Maven on Windows


  • We are going to install  Maven 3.3.9. So make sure you have JDK 7 installed. 
  • Set JAVA_HOME env variable to above JDK. For me it is C:\Program Files\Java\jdk1.7.0_79
  • Also I am using Windows 7.

How to install Maven on Windows

  • Download Maven zip file from their official site. We are going to install Maven 3.3.9. So go ahead download the corresponding zip file -
  • Unzip it to some folder.  I have extracted it in folder - C:\Users\athakur\Softwares\apache-maven-3.3.9
  • Now add M2_HOME and MAVEN_HOME environment variable and point them to your extracted folder.
  • Append your %PATH% variable to add %M2_HOME%\bin
  • Make sure your JAVA_HOME is set to JDK 7 as per note below.
  • Finally you can execute mvn -version to see if maven is successful installed.

NOTE : Maven 3.3 requires JDK 1.7 or above to execute. Maven 3.2 requires JDK 1.6 or above, while Maven 3.0/3.1 requires JDK 1.5 or above

Related Links

Serving static resources with Spring MVC 3.0 in Java


Many time we use custom CSS and java script file to add functionality and style to our web pages. We usually refer them in our JSP pages. In this post we will see how we can refer such static resources with Spring 3.0+. We will use Tomcat as our container/server.


If you are new to Spring then try out a sample Spring program -
Above post is somewhat old but should give you good idea as to get started. Anyhow I will provide the setup xmls here.

Your web.xml file should look as follows -

<web-app id="WebApp_ID" version="2.4"

  <display-name>Spring Web MVC Demo Application</display-name>




 Important file here is mvc-dispatcher-servlet.xml which forms a part of web application context. Have given a dummy file root-context.xml for root application context but you may not add it in your web.xml file. If you are not familiar with context loading part I suggest read

As I said create file mvc-dispatcher-servlet.xml with following content -

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <bean id="viewResolver"
        <property name="prefix">
        <property name="suffix">
    <!-- Scan for components under this package -->
    <context:component-scan base-package="com.osfg.test" />


As you can see the web application context will scan package com.osfg.test to find Spring resources like handler/interceptor. GO ahead create this package and create a controller in it. Lets call it Add following content to it.

package com.osfg.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

 * @author athakur
public class TestController {
    @RequestMapping(value="/test", method=RequestMethod.GET)
    public String welcome() {
        return "test";


That's it. We have our controller all setup. This controller returns a logical view name called test. Now if you notice our spring configuration we have already defined InternalResourceViewResolver which provides a JSTL view. So go ahead and create a JSP file at /pages/test.jsp under webapp or webcontent.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>OSFG Test Page</title>
<link href="CSS/test.css" rel="stylesheet">

<h1>Hello World from OSFG!</h1>


As you can see our JSP references a CSS file called test.css. Create a folder called CSS and then create a file called test.cs  in it. Add following contents to it.
H1 {
color: white; background: teal; FONT-FAMILY: arial; FONT-SIZE: 18pt; FONT-STYLE: normal; FONT-VARIANT: normal

Ok we are all set to test now.

Note the Directory structure below -


Testing 1.0

Go ahead start up your tomcat server and load following URL
  • http://localhost:8080/TestWebProject/test 

 Oops! what happened? It is not able to find the CSS file.Well lets come to the point. Spring does not directly allow you to access static resources and the very goal of this post is to show you how. So lets see that now.

Add following to your Spring configuration -
<mvc:default-servlet-handler />

This basically allows you to render static resources from root folder (under webapp or webcontent).
It essentially configures a handler for serving static resources by forwarding to the Servlet container's default Servlet (DefaultServletHttpRequestHandler is used).
 Lets test with our new configuration -

Testing 2.0

After restarting the server hit the URL again -

All good? Yup :) There is also alternate way to do this. You can define resource mapping instead of specifying default-servlet-handler.

<mvc:resources mapping="/resources/**" location="/CSS/" />

Then you also need to make change in JSP as follows

<link href="resources/test.css" rel="stylesheet">

So when you reference resources it will go and pick up from CSS. You can use classpath references in location as well. Here ResourceHttpRequestHandler is used to serve request based on location provided.

Related Links

Saturday, 19 December 2015

Variables Inheritance in Java


In one of the previous posts we saw method overriding. 
In case of variables it is slightly different. It is shadowing that overriding.

Variables Inheritance in Java

Following code will help you understand better

class A {
    int number = 10;

class B extends A {
    int number = 4;

    public void test() {
        System.out.println(((A)this).number); //same behavior as super.number

public class HelloWorld {
    public static void main(String args[]) {
        B b = new B();


And this will output : 

Note : Access to static/instance fields and static methods depend on class of the polymorphic reference variable and not the actual object to which reference point to. Also variables are never overridden. They are shadowed.

So if you have

public class A {
    public static String test = "testA";
    public static void test() {


public class B extends A {
    public static String test = "testB";
    public static void test() {

and you run

public class Test {
    public static void main(String args[]) {
        A a  = new B();

it will print testA

Note : In method overridden return type can be same or subclass of the super class method. Same goes for Exception thrown. Exception thrown by the overridden method can be same or subclass of Exception thrown by method in the super class. However the access modifies for the overridden method should be more strict.

Related Links

Thursday, 10 December 2015

Using SQL developer for connecting to DB2 database


To execute queries on IBM DB2 we can use IBM data studio . But if you are already user of SQL developer (using oracle DB) then you can use the same for connecting and executing to DB2 queries. In this post we will see how can we do that.

Using SQL developer for connecting to DB2 database

1. Download the JDBC driver for DB2 (click here). It will be something like db2cc4-4.18.60.jar.

2. Next go to your SQL developer. Go to Tools -> Preferences -> Databases -> Third party JDBC drivers and click on "Add Entry". Next select the driver jar you downloaded in step 1.

3. Next go to New (Ctrl + N) and select database connection. Provide details about your connection. Don't forget to select database type as DB2 rather than oracle (see screenshot) and provide your db details.

4. You can test your connection and save or connect to it. After this you should be good to execute queries on your DB2 database from SQL developer.

NOTE :  Your database connection are stored by SQL developer in location - C:\Users\athakur\AppData\Roaming\SQL Developer\system3.0.04.34\o.jdeveloper.db.connection.\connections.xml
Instead of athakur on the path it will be your username.

Saturday, 5 December 2015

Declare variables in PL/SQL


In this post we will see how to declare and initialize variables in PL/SQL block.


General syntax to declare variable in PL/SQL is
var_nm datatype [NOT NULL := var_value ];
  • var_nn is the name of the variable.
  • datatype is a valid PL/SQL datatype.
  • NOT NULL is an optional specification on the variable which this variable cannot be assigned null value.
  • var_value or DEFAULT value is also an optional specification, where you can initialize a variable with some specific value.
  • Each variable declaration is a separate statement and must be terminated by a semicolon.
We can assign value to variables in one of the following two ways -
  1. direct assignment (Eg. var_nm:= var_value;)
  2. Using select from (Eg. SELECT col_nm INTO var_nm FROM tbl_nm [WHERE clause];)

 Usage Examples

keeping above syntax in mind you can define variables as follows -

 id number; 
 SELECT 1000 into id from dual;
 dbms_output.put_line('id : '|| id ); 
 id number := 1000; 
 dbms_output.put_line('id : '|| id ); 

t> UA-39527780-1 back to top