Friday, 17 February 2012

ACTIONFORM IS NOT THE MODEL

        Many   developers  get  confused   when  they 
learn  about  the ActionForm  class. Although  it  can
hold  state  for  an  application , the state  that 
it  holds should  be  limited and constructed to  the
user input  that is  received  from  the client, and
the ActionForm  should  hold  it  only until  it  can 
be  validated  and  transferred  to the business tier.
    Business  objects  can  be  persisted  and  should 
contain  the  browser  logic  for  an application. They 
also should  be reusable. This  set  of  criteria does
not  match  up  well when  compared  against  ActionForms
   For  one thing,  the  ActionForm  class  servlet  is 
tied to the struts  framework  and explicitly  to a web
container, as it imports javax.servlet  package.
  It  would be  very  difficult  to  post  ActionForm
classes  to a different  type framework, such as a swing
application.
  ActionForm are designed  exclusively  to capture the
HTML data  from a client, allows presentation, validation
to occur, and provide a transport data from the  tier 
forward to the views. Apart from these users, you should
keep  the ActionForms  separate  from  your  business 
components.




---> These  lines are  from 
"Programming  Jakarta  Struts"


   

Thursday, 16 February 2012

SIMPLE JAVA MAIL DEMO USING APACHE JAMES SERVER

Today (16.2.2012),


  I have done a simple demo for sending mail.
 I used James mail server. 


   I  have used Struts Validation Framework for email validation.
In the other aspects, it is just the routine Struts coding.


  This program is a sample application to 
develop the real mail application. After
 sending the mail you can check it in 


'..\james\apps\james\var\mail\outgoing'
folder.


  Suppose, you want to develop a real mail 
application, you have to do some  changes in
this code.


  I have given the code in my website.sites.google.com/site/sudharaj4j2ee

Wednesday, 15 February 2012

report on work done on 12th, 13th and 14th February.

   I had my First review on last monday and
tuesday(13.2.2012 and 14.2.2012). So, I
prepared powerpoint presenatations and documents
for the  review, on Sunday ( 12-2-2012).
.
 13-2-2012    I had the First Review of my project
     
 14-2-2012    Had to attend college to sign some
              document about the review.
             
              Today, there was power shutdown for
               monthly maintenance. So no progress.
=======================================================  

         

Saturday, 11 February 2012

HIBERNATE IN STRUTS

   Today(11.12.2012), I did an experiment  for integrating 
struts1.1 and hibernate. It is very similar to a normal 
struts program. The thing is we should place the 
hibernate.properties file and *.hbm.xml  file in
correct directory. as shown below.

Friday, 10 February 2012

Getting Started with Hibernate

    My class name is 'mycontact'.It is a public class and I didn't use package to place this class. It is a value bean


  In that class I declared three fields(
Id,name and phone). Each of them is  String type.


 This class has a no-argument consructor and a parameterized  constructor with three parameters to initialize  Id, name and phone. And setter / getter methods for each field.
----------------------------------------------


  There is a hibenate.properties file In which we have to mention the dialect, driver  and the url of the mycontactstable.


hibernate.dialect  =   
        net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class   =  
        com.mysql.jdbc.Driver
hibernate.connection.url     =      
        jdbc:mysql://localhost/mycontacts
hibernate.connection.username  =   root
hibernate.connection.password
----------------------------------------------


  and the third important file is mapping file  (mycontact.hbm.xml). I have shown the essential elements.


<hibernate-mapping>     
    <class name="mycontact"                table="mycontactstable">
     <id    name="id"               column="ID"  unsaved-value="0">
          
               <generator class="assigned"/>


          </id>


          <property name="name"                      type="string"  />
          <property name="phone"                     type="string" />
</class>

</hibernate-mapping>
=======================================
( I have not shown the DTDeclaration).


The most important part is setting the classpath.
It should have the following jar files.


    1. cglib-full-2.0.2.jar;


    2. commons-collections-2.1.1.jar;


    3. commons-logging-1.0.4.jar;

    4. dom4j-1.4.jar;


    5. ehcache-0.9.jar;


    6. jdbc2_0-stdext.jar;


    7. jta.jar;


    8. log4j-1.2.8.jar;


    9. xerces-2.4.0.jar;


    10. xml-apis.jar;


    11. hibernate2.jar;


    12. commons-pool-1.2.jar;


    13. commons-dpcp-1.2.1.jar;


    14. mysqldriver.jar;   


All these files are available in hibernate\lib.
So we set the classpath accordingly.
=======
     I then created a bean for using hibernate.
As in my previous blog, I think , best thing is to   mention the operation also in the bean's method.
    The method name is 'edit'.
It has 4 arguments. ( namely name, phone, id and operation).
------------------------------------------
   There is a common portion for all the operations. like:


The function is:
  public String  edit
     ( String  a, String b, String c, String d)
----
   a   is  name
   b   is  phone
   c   is  Id
   d   is  operation.
---------------------------------------- 


SessionFactory  factory  =  null;
System.out.println("Creating session factory");




String   r="";
   try
   {
 Configuration     cfg =     new Configuration();
 cfg.addClass(mycontact.class);


 factory = cfg.buildSessionFactory();
 System.out.println("sessionfactory ready");


 Transaction tx = null;


 Session session = factory.openSession();
                                 
 tx   =  session.beginTransaction();


***************************************************************************************************
And  for a add record,




mycontact  mycontact1 = new mycontact(a,b,c);
   session.save(mycontact1);
------------------------------------------


for removing,


mycontact  mycontact1    =
     (mycontact)session.get(mycontact.class,c);
session.delete(mycontact1);


------------------------------------------
to find a record,




mycontact  mycontact1 =
         (mycontact)session.get(mycontact.class,c);
String n=mycontact1.getName();
String m=mycontact1.getPhone();
System.out.println(n+"\t"+m);
 r=n+"......."+m;


------------------------------------------
to show all the records,


java.util.List list1  =  session.find("from mycontact");
Iterator i=list1.iterator();


                 while(i.hasNext())
                 {
      mycontact mycontact1 = (mycontact)i.next();
      r=r+mycontact1.getId()+"\t";
      r=r+mycontact1.getName()+"\t";
      r=r+mycontact1.getPhone()+"\t";
                 }


------------------------------------------
and for modifying,                


 mycontact  mycontact1 =
     (mycontact)session.get(mycontact.class,c);
               
    mycontact1.setName(a);
    mycontact1.setPhone(b);
************************************************
After carrying out the operation,
 the session and transaction are to be properly handled as shown.


               tx.commit();


               session.flush();
               session.close();
==============================================
  After creating the bean and compiling, I created a simple console program as shown below.


 besthiberbean   bean  = new besthiberbean();
 String           r    = bean.edit(a,b,c,d);




System.out.println(r);
===
   I got correct results.
( >java besthiberbean    gopal 34251 200   add
etc).
***
   Now, our component is ready! After trying with a package, I can use it anywhere, be it frame or servlet or jsp or struts!


That is for integration of hibernate with Struts.
*************************************************  


               

creating MySql database from XAMPP .

    Yesterday( 10-2-2012)  I learned  how to create a database using MySql which comes with XAMPP  software. 
     XAMPP software contains Apache WebServer, MySql5 database and PHP as well as Perl  scripting engines.
   I give below the steps required for creating 
database named 'mycontacts' in MySql.


go to  c:\xammp\mysql\bin


>mysql -u  root
Now you are taken to the mysql prompt


mysql>create  database  mycontacts;


>use mycontacts;


>create  table mycontactstable(id  varchar(4) primary key,name  varchar(20),phone  varchar(10));




 Initializing ID field can be of either auto incremented or assigned. In this database I am going to assign the ID. so, It can be of any of the data types. I have declared it as String (varchar)type.  If you choose auto Increment, the ID field should be declared of Integer type.        


>select *   from mycontactstable;


Initially You won't get any entry.


>insert  into  mycontactstable  values('101', 'tom','98762');




>insert  into  mycontactstable  values('102', 'sam','98476');


After making a few entries, test


>select *  from mycontactstable;


you will get your records in mycontactstable.


I am going to use this table for my experiment 
on Hibernate. In Hibernate mapping document, we
mention about generator type. It refers to the way  in which primary key is generated. Many 
databases have provision for auto-increment.
In that case, it will be a number.
If so,the following syntax is used to get a record in
Hibernate code.


   int   n = Integer.parseInt(c);
    etc.


If we are using String type for Id field, it is not necessary. I just wanted to make things as simple as possible.



Sunday, 5 February 2012

A SIMPLE DEMO FOR LOOKUP DISPATCH ACTION...HINTS

         In struts, there are five special  Actions.They are




                   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
...