Friday, September 25, 2009

MVC Design pattern with Struts.

Struts Framework:(1.3 version)
Struts is MVC based framework developed by Apache. Framework generally provides the basic functionality for a project. Struts provides the set of Action classes, set of tag-libraries, set of Form beans and etc.
Struts combines the most of the design patterns in to a single framework. Struts implements the Single-ton design pattern, Delegate design pattern, Factory Design pattern, Chain of responsibilities.
Struts is Strongly Model View Controller (MVC)based framework MVC fallows the three tier architecture. The model, view and controllers regarding to the Struts are explained as fallows.

View:
View is used to show something to user. Struts allows to use htmls, jsps as view components. To develop the more friendly views struts provided the set of tag libraries. You can download these libraries from the apache site and you can use those into your view components. The set of tag libraries are
struts-html : These tags are replacement of normal html tags. The struts html tags are binds with the form bean properties.
Ex: <html:text property=”userName” />
tag replaces the normal html tag like
<input type=”text” name=”userName” />
struts-bean : These tags are replacement for the jsp action tags. The bean properties are defined with this tags, you can get the bean properties using struts-bean tags.
Ex: <bean:define id=”tempVar” value=”10” />
Above tag defines a variable as 'tempVar' and assign 10 to it as string value.
<bean:write name=”Employee” property=”empId” />
Above tag get the value of empId from the Employee form bean.
Struts-logic: These tags are used to write the logical operations. To validate the if conditions or equals conditions you can use these tags.
Ex: <logic:equal name=”empId” value=”100” >Adimin</logic:equal>
Admin is displayed when the empId equals to 100 only.
To get the for loop type iterations you can use the <logic:iterate /> tag.
struts-nested: To display the group of objects you can use the nested tags.
You can implement most of the functionality of above three tags with the nested tags.
Inplace of <bean:write /> you have <nested:write /> and so on.......

Model:
Business logic and persistence logic (database dependent logic) will be developed in Model. In struts you can use normal Java beans, DTOs, Dos as models. You can develop the Model layer using other tools also. Struts allows to use Hibernate, spring in the model layer.

Controller:
Controller is most important concept in the Struts 1.3. In struts ActionServlet acts as Front controller. Each and every request must passes through the ActionServlet. You can change the ActionServlet functionality by extending ActionServlet to your own class.

RequestProcessor class also acts as the controller to process the request. In struts 1.3 Request processor is the ComposableRequestProcessor. It has processXXX() methods. These methods are controls the execution of action classes, controlling of form beans ..etc.

Actions, Dispatch actions, Action Forms , Validator forms ..etc all are come under the controller.


Difference between Struts html tags and normal html tags


What is the difference Normal HTML tags and Struts HTML tags in the way of performance?

Apache struts provided the struts html tags as the replacement of normal html tags for the Jsps. These Struts html tags are directly bind to the form bean using ActionForm where as normal html tags cannot bind with form beans. HTML tags are static but Struts tags are Dynamic (At the run-time struts tags are called)


But coming to performance half of the code in struts tags is pre-compiled one. If we use normal html tags web container has to load the jsp from the scratch. Where as struts tags loads with the pre-compiled code. So that struts tags improves the performance.




Thursday, September 24, 2009

Inner classes in Java

There are four types inner classes in Java. Those are
1. Regular Inner classes
2. Method-local inner classes
3. Anonymous inner classes
4. Static nested classes.

Javascript Dynamic row Addition

Please copy this code and create as test.html and see the functionality
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function addRow(div,value,x){

var ni = document.getElementById(div);
var numi = document.getElementById(value);
var num = (document.getElementById(value).value -1)+ 2;
numi.value = num;

var divIdName = "my"+num+x+"Div";
var newdiv = document.createElement(div);
newdiv.setAttribute("id",divIdName);
newdiv.innerHTML = "<table width=\'100%\' class=\'listTable\' >"
+"<tr class=\'listTR"+(num%2)+"\' onMouseOver=this.style.backgroundColor=\'DEDEBE\' onMouseOut=this.style.backgroundColor=\'\'>"
+"<td><input type='text' name='studNo'/></td>"
+"<td><input type='text' name='studName'/></td>"
+"<td><a href=\"javascript:;\" class=\"menu\" onclick=\"removeEvent(\'"+divIdName+"\',\'"+div+"\')\">Remove</a></td>"
+"</tr></table>";
ni.appendChild(newdiv);
}
function removeEvent(divNum,div){
var d = document.getElementById(div);
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
}
</script>
</head>
<body>
<form action="">
<table>
<tr bgcolor="#737328"><td>
<table>
<tr><td>Student No</td><td>Student Name</td><td>Actions</td></tr>
<tr><td><input type="text" name="studNo"/></td><td><input type="text" name="studName"/></td><td> </td></tr>
</table>
</td></tr>
<tr bgcolor="#737328"><td>
<input type="hidden" name="theValue" id="theValue" value="0"/>
<div id="myDiv"></div>
</td></tr>
<tr>
<td><p><a href="javascript:;" onclick="addRow('myDiv','theValue','');"><strong>Add More</strong></a></p></td>
</tr>
</table>
</form>
</body>
</html>

Adding Dynamic rows to table using javascript

Please copy this code and create as test.html and see the functionality

<html>
<head>
<script LANGUAGE="JavaScript">
function addRow(tableID){
var table = document.getElementById(tableID);

var rowCount = table.rows.length;
var row = table.insertRow(rowCount);

var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "text";
element1.name = "empNo";
cell1.appendChild(element1);

var cell2 = row.insertCell(1);
var element2 = document.createElement("input");
element2.type = "text";
cell2.appendChild(element2);

var cell3 = row.insertCell(2);
var element3 = document.createElement("textarea");
element3.setAttribute("name","mytextarea");
element3.setAttribute("cols","10");
element3.setAttribute("rows","1");
cell3.appendChild(element3);
}
function deleteRow(tableID) {

var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount > 1) {
table.deleteRow(rowCount - 1);
}

}
</script>
</head>
<body>
<form name="f1" id="f1">
<input type="button"value="Add" onclick="addRow('datatable')">
<table id="datatable" cellspacing="0" border="1" bgcolor="#738711">
<tbody>
<tr>
<td><input type="text" ></td><td><input type="text"></td><td><textarea rows="1" cols="10"></textarea></td>

<td><a href="javascript:void(0);" onclick="deleteRow('datatable')" >Remove</a></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

Wednesday, September 23, 2009

Java Interview Questions

Please post answers for fallowing Questions:

1. What is serial version id which generated in Eclipse.
2. What is the difference between implementing an interface and extending a class
3. What is servlet lifecycle?
4. can static variables are bind with objects?
5. what is the difference between Servlet and Servlet filter?
6. What is the difference between filter and listener?
7. What are features swing has over the awt?
8. what are the features of hibernate?. What is hibernate?
9. What are adapting classes?
10. What are the differences between string and StringBuffer?
11. What is mutable?
12. What are the differences between Arraylist and Vector?
13. How can you deploy an application in tomcat and JBoss?
14. How many layers do you used to develop the web application?

Wednesday, September 16, 2009

Hibernate Questions

Please post the Answers for the fallowing Questions:

1Q: How the web server or applications server knows that you are using Hibernate?
2Q: What are the parameters has to use to create a database resource in the hibernate.cfg.xml?
3Q: What is hivernate mapping file and what is the use of it?
4Q: What is ORM mapping software and how hibernate differs from other ORM mapping softwares?
5Q: What are the features of hibernate?
6Q: What is sessionFactory and when the sessionFactory is created?
7Q: What is the use of dialect in the hibernate mapping file?
8Q: What is HQL ?
9Q: What are the ways you have to get data from database in hiberanate?
10Q: What is the difference between Hibernate session and HTTP session.

Monday, September 14, 2009

Example to create thread by implementing runnable interface

class Thread1{
static public void main(String[] args){
Thread myThreadA = new Thread(new MyThread1(),"threadA");
Thread myThreadB = new Thread(new MyThread1(),"threadB");
//run the two threads
myThreadA.start();
myThreadB.start();

try{
Thread.currentThread().sleep(1000);
}catch(InterruptedException e){}
//Display info about the main thread
System.out.println(Thread.currentThread());
}
}
class MyThread1 implements Runnable{
public void run(){
for(int i= 0;i <10;>
System.out.println(Thread.currentThread());
}
}

Write into file Example

import java.io.*;

class Bistream{
public static void main (String args[]){
try{
BufferedInputStream in =new BufferedInputStream(new FileInputStream("test.txt"));
int i,j=1;
while((i = in.read ())!= -1){
System.out.print((char)i);
if (j==10)
in.skip(10);
if (j==20)
in.mark(199); //mark at 20th byte so that we can come back
if(j==30)
in.reset();
j++;
}
in.close();
} catch (Exception e){};
}
}

Wednesday, September 9, 2009

Java Collection Frame work

Java collection pages are more useful in the development of the applications. Collection is an object which stores the objects such as ArrayList, Vector...etc

The basic interface in collection frame work is Collection. Other interfaces are Set, List, SortedList.
List:
1. When sequence matters
2. When you know index position
3. Duplicates are allowed.

Set:
1. When uniqueness matters
2. You can never have more then one element referencing the same object
3. Does not allow Duplicates.

Map:
1. Key value pairs
2. Maps know the value associated with the given key.
3. You can have two keys that refer the same value but you cannot have a duplicate key(A key can be any object).

The basic advantages of Collection frame work are:
By providing data structures and Algorithms internally the reduces the Programming effort.