Sunday, 5 February 2012
A SIMPLE DEMO FOR LOOKUP DISPATCH ACTION...HINTS
1.ForwardAction
2.DispathAction
3.LookupDispathAction
4.SwitchAction
5.IncludeAction
In these Actions first three actions (ForwardAction,DispatchAction, LookupDispatchAction) are very important.
I have already done a simple demo for DispatchAction which is about If we want to use more than one function in Action class what we have to do.
LookupDispathchAction is also similar to DispatchAction.
But, Additionally we have to do two things
1.Implemetation of getKeyMethodMap() method
2.put entry in .properties file
demoAction.java
-----------------
public class demoAction extends LookupDispatchAction
{
//your methods
protected Map getKeyMethodMap()
{
Map map=new HashMap();
map.put("operation.add","add");
map.put("operation.sub","sub");
//map your methods as above
return map;
}
}
Application.properties file entry
----------------------------------
operation.add=Add
operation.sub=Sub
...
PREPARING ABSTRACT,TABLE-DESIGN, TECHNOLOGY STACK,DFD
I prepared tables design and DataFlow Diagram.
I think, the technology stack will be of interest to students like me.
I am giving that below.
=====================================================================
Brief Overview about Technology Stack
Nowadays, There is much debate between two sets
of technologies. One group consists of Spring, Struts1.1,
struts2, Hibernate, and the other group consists of JSF, JPA,
SEAM. And also for providing webservice some of them make use
of REST protocal and some of them use SOAP.
JPA, JSF are all JCP standard.
Spring, and struts are not standards.
But, theyare used widely .
That is why we are giving equal importance to
both set of technologies.
Webserver Tomcat 5.0
Enterprise Server JBoss4.2.2 / JBoss5
Development Platform Windows XP (service pack2)
Database MySql in WAMP server
Database Accessing JDBC / ORM(Hibernate)
Syndication RSS feed
Container Spring / SEAM
AJAX technology jQuery+JSON
Web Framework STRUTS1.1/ STRUTS2/ JSF
Portal JBoss Portal software
Messaging JBoss Messaging
Mobile Client J2ME
Reports Jasper Reports
XML WebService AXIS / JBoss webservice and REST
IDE ECLIPSE 3,5 ( GANYMEDE)
Build-tools ANT / MAVEN
UNIT TESTING JUnit
CMS JBOSS CMS
UML STAR UML
Friday, 3 February 2012
SEQUENTIAL IMAGE LOADING FROM SERVER USING JSON & jQUERY
This time, I have made use of JSON-Java library as a jar file. I got that from JSON.org site.
The name of the jar file that I selected is json-simple.jar. There are a lot of json-java librarries there!.
This library is recommended in Ajax on Java book. which I am using.
I placed this jar in tomcat\web-apps\root\web-inf\lib folder.
--------------------------------------------------------------------------------
The jsp code is given below.
JSONObject ob1 = new JSONObject();
JSONObject ob2 = new JSONObject();
String s1="";
s1="alice.jpg"; ob1.put("alice",s1);
s1="clinton.jpg"; ob1.put("clinton",s1);
s1="cindy.jpg"; ob1.put("cindy",s1);
s1="colin.jpg"; ob1.put("colin",s1);
s1="nathan.gif"; ob1.put("nathan",s1);
s1="mathew.gif"; ob1.put("mathew",s1);
ob2.put("myarray",ob1); String r=ob2.toString();
System.out.println(r); // for checking
out.println(r);
----------------------------------------------------------------------------------------
Now to the client side.
function job5() // jQuery Ajax
{
$.get("imagesender.jsp",function(r)
{
a=r
});
a = eval('('+a+')');
}
n=1
function job1()
{
if(n==7){ n=1}
if(n==1){ s="http://localhost:8080/"+a.myarray.alice} etc
if(n==2) s="http://localhost:8080/"+a.myarray.clinton
setTimeout("job4()",1000)
}
function job4()
{
$("#img1").attr({src:s});
n=n+1
job1();
}
//------------------------
DISPATCH ACTION DEMO FOR CRUD OPERATIONS
When we have a number of related methods,
instead of writing action-class for each operation,
we should use DispatchAction. Usually in Action class we
define execute(...) method.But, in DispatchAction you
can have your own methods which should resemble the
signature of the original execute(...) method.
public ActionForward execute(ActionMapping mapping,
ActionForm form,HttpServletRequest request,
HttpServletResponse response);
I am going to explain this DispatchAction with two applications.
The first one deals with databese CRUD operations and the second
one deals with simple arithmetic operations.
when you use this DispatchAction, you should be careful
while providing action mapping. We have to specify one more
attribute. that is 'parameter' which holds the method name which
we want to access.
My database table has three fields ( key, name, place).
My example has the following files.
demoInput.jsp
demoForm.java
Finally, we have demoOutput.jsp for
displaying the result in table form
using jstl for iteration.
I am giving demoAction.java here
=================================================================
package demopack;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;
public class demoAction extends DispatchAction
{
public ActionForward addRecord(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
demoForm demoform=(demoForm)form;
String a=demoform.getKey();
String b=demoform.getName();
String c=demoform.getPlace();
if(a.length()!=0)
{
querybean bean1=new querybean();
Vector vector=bean1.addRecord(a,b,c);
request.setAttribute("result",vector);
return(mapping.findForward("success"));
}
else
{ return ( mapping.findForward("failure"));}
}
//------------------------------------------
public ActionForward modifyRecord(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
demoForm demoform=(demoForm)form;
String a=demoform.getKey();
String b=demoform.getName();
String c=demoform.getPlace();
if(a.length()!=0)
{
querybean bean1=new querybean();
Vector vector=bean1.modifyRecord(a,b,c);
request.setAttribute("result",vector);
return(mapping.findForward("success"));
}
else
{ return ( mapping.findForward("failure"));}
}
//--------------------------------------------------
public ActionForward removeRecord(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
demoForm demoform=(demoForm)form;
String a=demoform.getKey();
String b=demoform.getName();
String c=demoform.getPlace();
if(a.length()!=0)
{
querybean bean1=new querybean();
Vector vector=bean1.removeRecord(a);
request.setAttribute("result",vector);
return(mapping.findForward("success"));
}
else
{ return ( mapping.findForward("failure")); }
}
//------------------------------------------------------------
public ActionForward showRecord(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
demoForm demoform=(demoForm)form;
String a = demoform.getKey();
String b = demoform.getName();
String c = demoform.getPlace();
if(a.length()!=0)
{
querybean bean1=new querybean();
Vector vector1 = bean1.showRecord(a); // key
request.setAttribute("result",vector1);
return(mapping.findForward("success"));
}
else
{ return ( mapping.findForward("failure"));}
}
//-------------------------------------------------------
public ActionForward showallRecords(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
demoForm demoform=(demoForm)form;
String a=demoform.getKey();
String b=demoform.getName();
String c=demoform.getPlace();
if(a.length()!=0)
{
querybean bean1=new querybean();
Vector vector1 =bean1.showallRecords();
request.setAttribute("result",vector1);
return(mapping.findForward("success")); }
else
{ return ( mapping.findForward("failure"));}
}
In demoOutput.jsp,I have iterated the objects
returned by the methods defined in
demoAction class
===================================================
package demopack;
import java.util.*;
import java.sql.*;
public class querybean
{
Connection connection;
Vector vector1;
public querybean()
{
vector1 = new Vector();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:keynameplace";
connection=DriverManager.getConnection(url);
}
catch(Exception e1)
{ vector1.addElement(""+e1);}
}
//-------------------------------
public Vector addRecord(String a,String b,String c)
{
Statement statement=connection.createStatement();
String sql="insert into table1 values('"+a+"','"+b+"','"+c+"')";
statement.execute(sql);
player player1 = new player();
player1.setKey("added");
player1.setName("");
player1.setPlace("");
vector1.add(player1);
connection.close();
}catch(Exception e1) {System.out.println(""+e1); }
return vector1;
}
//--------------
public Vector modifyRecord(String a,String b,String c)
{
Vector vector1 = new Vector();
try
{
Statement statement=connection.createStatement();
String sql="update table1 set table1.name='"+b+"',table1.place='"+c+"' where table1.key='"+a+"' ";
statement.execute(sql);
player player1 = new player();
player1.setKey("modified");
player1.setName("");
player1.setPlace("");
vector1.add(player1);
connection.close();
}catch(Exception e1) {vector1.addElement(""+e1); }
return vector1;
}
//======================
public Vector removeRecord(String a)
{
try
{
Statement statement=connection.createStatement();
String sql="delete from table1 where table1.key='"+a+"' ";
statement.execute(sql);
player player1 = new player();
player1.setKey("removed");
player1.setName("");
player1.setPlace("");
vector1.add(player1);
}catch(Exception e1) {System.out.println(""+e1); }
return vector1;
}
//===========================
public Vector showRecord(String a)
{
try
{
Statement statement=connection.createStatement();
System.out.println ("statement ready");
String sql="select * from table1 where key='"+a+"' ";
System.out.println(sql);
ResultSet rs=statement.executeQuery(sql);
while(rs.next())
{
String s1=rs.getString(1);
String s2=rs.getString(2);
String s3=rs.getString(3);
player player1 = new player();
player1.setKey(s1);
player1.setName(s2);
player1.setPlace(s3);
vector1.add(player1);
}
}catch(Exception e1) { vector1.addElement(""+e1); }
return vector1;
}
//======================
public Vector showallRecords()
{
try
{
Statement statement=connection.createStatement();
String sql="select * from table1";
ResultSet rs=statement.executeQuery(sql);
while(rs.next())
{
player player=new player();
String s1=rs.getString(1);
String s2=rs.getString(2);
String s3=rs.getString(3);
player.setKey(s1);
player.setName(s2);
player.setPlace(s3);
vector1.add(player);
}
}catch(Exception e1) {System.out.println(""+e1); }
return vector1;
}
//======================
}
==========================================
The following code is for the player class that we have used above.
package demopack;
public class player
{
String key;
String name;
String place;
public void setKey(String a) { key = a; }
public void setName(String b) { name = b; }
public void setPlace(String c) { place = c; }
//==================
public String getKey() { return key; }
public String getName() { return name; }
public String getPlace() { return place; }
}
how to iterate the collection of objects created in querybean.
=============================================
I tested the code and got correct results.
This demo shows how we use DispatchAction.
It also shows how the action just returns an array ( vector).
It is the job of the output.jsp to display the result as it wants.
The action class should NOT take the responsibilty of how
the result is to be displayed,
If you want to explain the purpose of DispatchAction to a beginner,
I think we should use a very simple example. We need not bring in
things jstl ,jdbc etc.
We said that we use DispatchAction when there are a number of functions
inter-related as in CRUD.
So, why not a simple calcbean ?
For this purpose I have thought of an calcbean having 4 methods
( add, subtract,multiply and divide).
So, I developed dispatchapp with simple
arithmetic operations (addidtion, subtraction,
multiplication and division.
That will better bring out the essence of DispatchAction.
This is given in my next blog.
Thursday, 2 February 2012
CYCLIC DISPLAY OF IMAGES FROM SERVER-SIDE
jQuery-Ajax for getting URL of selected picture from JSP
Wednesday, 1 February 2012
JDBC-AJAX-Chained List Using JSON
Today(1-2-2012), I developed a chained list demo using json.js library
and raw
1.combo1 -to show state names 2.List1 -to populate cities according
to the selected state. 3.List2 -the selected city in List1
to be added to list2
To store these state and cities I needed a database table. So I created a table with two fields 1.state,2.city
Then, I used 3 buttons,a)for listing the cities in list1
b) to add city to List2
c) to Remove city from List2
When user clicks “List the cities” button, the
Using this input we form the sql query as
“select *from table1 where state=’”+selectedState+”’”
This will return the Resultset. In this ResultSet, we need the second column only which contains cities name in accordance with given state.
Then we have to iterate this String to form JSONString. Then return this String to
(Reference:
For populating the cities in List1 we need to access database with the help of JSP. for other remaining two tasks we can use simple javascript code.
I tested this code in IE. I got correct result. I tried this same code in Firefox and Chrome. But, It didn’t work. Though the object is initialized correctly, data could not be populated in Lists. I must find out!