Java

Java is a set of computer software and specifications developed by Sun Microsystems, which was later acquired by the Oracle Corporation, that provides a system for developing application software and deploying it in a cross-platform computing environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones to enterprise servers and supercomputers.

Spring Logo

Spring Framework

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is infrastructural support at the application level: Spring focuses on the "plumbing" of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.

Hibernate Logo

Hibernate Framework

Hibernate ORM is an object-relational mapping framework for the Java language. It provides a framework for mapping an object-oriented domain model to a relational database.

Friday, December 28, 2012

Encode and Decode Example in Java

This post demonstrates the how to encode and decode the String data.

package com.ranga;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
* This class is used to encode and decode the string data.
* @author Ranga Reddy
* @version 1.0
*/

public class EncodeAndDecodeExample {
private static final String data = "Hello, I am Ranga Reddy. ";

public static void main(String[] args) {

String encodedData = getEncodedData(data);
System.out.println("Encoded Data: " + encodedData);

String decodedData = getDecondedData(encodedData);
System.out.println("Decoded Data: " + decodedData);
}

private static String getDecondedData(String encodedData) {
try {
return URLDecoder.decode(encodedData, "UTF-8");
} catch (UnsupportedEncodingException exception) {
throw new RuntimeException("Does not support UTF-8");
}
}

private static String getEncodedData(String data) {
try {
return URLEncoder.encode(data, "UTF-8");

} catch (UnsupportedEncodingException exception) {
throw new RuntimeException("Does not support UTF-8");
}
}
}

Output:
Encoded Data: Hello%2C+I+am+Ranga+Reddy.+
Decoded Data: Hello, I am Ranga Reddy.

Saturday, October 27, 2012

Spring MVC Hello World Example

In this post, we are going to see how to implement Spring MVC HelloWorld example.
Step 1:
The initial step is to create Dynamic Web Project in eclipse. To create a new Dynamic project in Eclipse IDE select File -> Dynamic Web Project.
Step 2:
After that New Dynamic Project Window will appear on the screen and you will enter the web application name in the Project name text box. Enter SpringHelloWorld in the Project Name and then click Next button.

Step 3:
Add Spring Web MVC jar files.
spring-webmvc.jar
spring.jar
commons-logging.jar
Step 4:
Now we will created a index.jsp in project's WebContent  folder. In the jsp file we will create a new hyperlink "Click Here" that will be linked to hello.do page.  The code of index.jsp is:
index.jsp

<html>
<head>
<title>Spring Demo</title>
</head>
<body>
<a href="hello.do">Click Here</a>
</body>
</html>
Step 5:
Now we will configure the DispatcherServlet in web.xml file. The DispatcherServlet will be configured to process all the request ending with *.do
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SpringHelloWorld</display-name>
<description>Hello world Application</description>
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Step 6:
Now we will create a new xml file called controller-servlet.xml in the WEB-INF folder of the web application. This is the main configuration file for the Spring MVC.
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
Then we will define the url mapping with the Controller bean as shown below:

<property name="mappings">
<props>
<prop key="/hello.do">helloController</prop>
</props>
</property>
Finally we will define the Controller bean as shown below:
<bean id="helloController" class="com.ranga.HelloWorld"> </bean>
controller-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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.do">helloController</prop>
</props>
</property>
</bean>
<bean id="helloController" class="com.ranga.HelloWorld" />
</beans>
Step 7:
After that we will creates the controller class in the project src folder. This will be used as the controller for the hello.jsp request. The code of the  HelloWorld.java calss is:

HelloWorld.java

package com.ranga;

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 HelloWorld implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
System.out.println("HelloWorld");
String str = "Hello World!";
return new ModelAndView("hello.jsp", "message", str);
}
}
Step 8:
After that we will create a hello.jsp in the WEB-INF folder. The WEB-INF/hello.jsp code is:
hello.jsp

<%@page isELIgnored="false" %>
${message}
Step 9:
To run the example select Run -> Run As -> Run On Server from the menu and then run the application on tomcat server. Eclipse will start Tomcat server and deploy the application on the Tomcat. Ellipse will also open the application in it internal browser as shown below:
Click on the "Click Here" link to test the application. It should display the "Hello World!" message as shown below.

Friday, October 19, 2012

Eclipse IDE Tutorial

Please find the following site(s) it is very useful for beginners and experienced people.

  • http://www.vogella.com/tutorials/Eclipse/article.html
  • https://www.eclipse.org/
  • http://www.tutorialspoint.com/eclipse/

Friday, September 14, 2012

Difficult Questions And Intelligent Answers for Interview

Difficult Questions and Intelligent Answers for Interview: 

1. How can you drop a raw egg onto a concrete floor without cracking it?
Ans. Concrete floors are very hard to crack! (UPSC Topper)

2. If it took eight men ten hours to build a wall, how long would it take four men to build it?
Ans. No time at all it is already built. (UPSC 23 Rank Opted for IFS)

3. If you had three apples and four oranges in one hand and four apples and three oranges in the other hand, what would you have?
Ans. Very large hands..(Good one) (UPSC 11 Rank Opted for IPS)

4. How can you lift an elephant with one hand?
Ans. It is not a problem, since you will never find! an elephant with one hand. (UPSC Rank 14 Opted for IES)

5. How can a man go eight days without sleep?
Ans. No Probs, He sleeps at night. (UPSC IAS Rank 98)

6. If you throw a red stone into the blue sea what it will become?
Ans. It will Wet or Sink as simple as that. (UPSC IAS Rank 2)

7. What looks like half apple?
Ans. The other half. (UPSC - IAS Topper)

8. What can you never eat for breakfast?
Ans. Dinner.

9. What happened when wheel was invented?
Ans. It caused a revolution.

10. Bay of Bengal is in which state?
Ans. Liquid (UPSC 33Rank)

11. How many buckets of water do Pacific Ocean contains?
Ans. It depends on the size of the bucket. (CA Institute Campus Interview Placement)

12. Interviewer said 'I shall either ask you ten easy questions or one really difficult question. Think well before you make up your mind!' The boy thought for a while and said, 'my choice is one really difficult question. ''Well, good luck to you, you have made your own choice! Now tell me this. 'What comes first, Day or Night?'
Ans. The boy was jolted into reality as his admission depends on his answer, but he thought for a while and said, 'It's the DAY sir!'
'How' the interviewer asked,
'Sorry sir, you promised me that you will not ask me a SECOND difficult question!'
He was selected for IIM!

Spliting a String without using any built in functions in Java

In this post, we are going to see how to split a string without using any built in functions.
package com.ranga;

import java.util.ArrayList;
import java.util.List;

/**
* Write a Java program to split a string with out using any bulit methods like
* split(), StringTokenizer class methods.
*
* @author Ranga Reddy
* @version 1.0
*/

public class SplitString {
public static void main(String[] args) {
String str = "Welcome to JavabyRangaReddy blog";
String delimiter = " "; // here delimiter is space
List<String> list = new ArrayList<String>(); // list is used to store the words
int i, start = 0, end = 0;
for (i = str.indexOf(delimiter); i != -1; i = str.indexOf(delimiter, i + 1)) {
end = i;
list.add(str.substring(start, end)); // by using substring we can get the one word
start = i;
}
list.add(str.substring(end)); // this is used to add the last word
System.out.println(list); // print the list

// Converting list to array of strings
String words[] = (String[]) list.toArray(new String[list.size()]);
for (String word : words) {
System.out.println(word.trim());
}
}
}
Output:
[Welcome,  to,  JavabyRangaReddy,  blog]
Welcome
to
JavabyRangaReddy
blog