Search and Replace Content inside a File

Search and Replace Content inside a File
Today, We are going to see how to search a file for specific content and replace the content and write to a new File.

Let us create a text document with some content in it


Now we are going to write a code to change “File 1” into “File 2”, The content will be

“Sample Content in File 2”

Once the code is executed, File2.txt will get created and content will be,



Read More

Autowiring in Spring Example – Autowire byType

Autowiring in Spring Example – Autowire byType

Autowire byName
If the name or id of the bean declared in applicationContext.xml (or xml which you create) is same as the name of the bean property then autowire byName can be applied

Autowire byType
Means it will search for one bean of given property in the container(Only one bean). If there are more than one bean, then it will not autowire.

Autowire Constructor
It will search for compatible constructor with given parameters

Autowire byName

Directory Structure


Note: autowire byType applies for exactly one type, here we have declared only one type of bean and it has applied to that. In output you can see, the value for employee2.getEname() also shows the same value of employee1.getEname(). This is cause the byType applies depending on the type








Read More

Custom Interceptor in Struts 2

Custom Interceptor in Struts 2

What are Interceptors?
Interceptors are used for performing operations like Validation, file upload, exception handling etc.

Advantages of Interceptors:
Separation of Concerns, If we need to remove any of the concerns like validation or exception handling etc.. We can remove them from struts.xml without disturbing the application.

Struts 2 provide a set of default interceptors (default interceptors), we can also create and implement our own interceptors. Creating and implementing own interceptors are known as Custom Interceptors.

Lets see an example for custom interceptors, We are going to check the time for the process.


Lets create 2 JSP files – input.jsp and showdetails.jsp




Console output to check the timing function,

[2015-09-18T20:52:01.795+0530] [glassfish 4.0] [INFO] [] [] [tid: _ThreadID=101 _ThreadName=Thread-3] [timeMillis: 1442589721795] [levelValue: 800] [[
Start time is: 1442589721795 ms]]






Read More

JSF Hibernate Integration (CRUD Example)

JSF Hibernate Integration (CRUD Example)
Code to integrate JSF with Hibernate and perform Create, Read, Update and Delete operations. This code is tested in Netbeans with MySQL Database.

Creating Project:
1) File –>New Project–>Java –>Web Application and click Next
2)Project name as HiberJSF and Click Next
3) Click Next
4) Choose the JSF and Hibernate framework and select the appropriate Database and click Finish


Table Creation




show.xhtml (to display all records)


ID Name Department
1 Alpha Computer Science
21 Seline Models
90 Sean Physics


ID Name Department
21 Seline Models
90 Sean Physics

Show Record by ID


ID Name Department
21 Christy Planner


ID Name Department
21 Christy Planner
90 Sean Physics

Do not forget to Add @ManagedBean in and import – import javax.faces.bean.ManagedBean; (Not javax.annotation.ManagedBean)










Read More

Dynamic Navigation in JSF (Rule-Based Navigation)

Dynamic Navigation in JSF (Rule-Based Navigation)

What is Navigation:
A website is a collection of web pages. Moving between web pages of a website is known as Navigation. User can switch from one webpage to another in any of these following ways,

  • Clicking a button
  • Clicking Hyperlink
  • Typing URL

Types of Navigation:

  • Static Navigation
  • Dynamic Navigation

Dynamic Navigation:
When the decision to navigate from one webpage to another webpage that depends on the user input is known as Dynamic Navigation.

Dynamic Navigation is implemented using Rule-Based Navigation.

Navigation Rule:
This is the important part which plays a major role in Dynamic Navigation.

< navigation-rule > This is the root element that specifies how to navigate from one page to another
< navigation-case > Combination of conditions that must match for case to be executed
< from-view-id > Page where navigation originates
< from-action > Navigation criteria that must be satisfied for the case to execute
< from-outcome > Value to be compared with the output of < from-action >
< to-view-id > Depending on the outcome new page will be rendered

Note: Navigation-rule can contain any number of navigation cases.

Most websites have login page, when the user enters the login credentials and click login, the login credentials are validated.  If the login details are matched it will take you to home page or it will take you to error page. The user is moved to webpage based on his inputs. This is known as Dynamic Navigation.Lets consider a simple login page, When user enters the username as “alpha” and password as “alpha” the user will be navigated to Success.xhtml. If he enters wrong credentials he will be navigated to error.xhtml

JSF Managed Bean-





When user enters the details in login.xhtml page, if the username is “alpha” and password is “alpha”, the user will be navigated to success.xhtml or error.xhtml.

This action call the bean method “checkdetails()” which checks for the username and password validation.

From the example, lets start from Navigation rule. Since it is the root element Navigation-rule comes first
< from-view-id > : The navigation originates from login.xhtml page (i.e. When user enters the credentials he may be directed to success.xhtml or error.xhtml). so login.xhtml is mentioned in < from-view-id >
< from-action >: This is calling of method from bean.
< from-outcome >: In the bean method, we have defined possibility of 2 outcome’s, either “success” or “error”. So in from-outcome we have mentioned, if outcome is success then it goes to Success.xhtml.

In the second case,
< from-outcome >: If outcome from method checkdetails is error, it will navigate to error page.

Note: Navigation-rule can contain any number of navigation-case. In our example we have defined 2 navigation-case inside navigation-rule

outcome –>Success
Username is entered Wrong,






Read More