Pages

Thursday, 26 April 2012

Access or get HttpRequest or HttpSession out side container (in POJO)


We can use RequestContext class to share the Http Request object across all POJO classes. This is how it can be done.

Step 1: Setting the Http Request into RequestContext.
This is done by calling the static method newInstance(HttpServletRequest)on RequestContext  from your servlet.

Step 2: Fetch the Http Request from anywhere
Simply give a method call “getCurrentInstance().getRequest()” on RequestContext  in any POJO class. 
This will return Http Request object for current thread.

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

public class RequestContext implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private static ThreadLocal<RequestContext> instance = new ThreadLocal<RequestContext>() {

        protected RequestContext initialValue() {
            return null;
        }

    };

    private HttpServletRequest request;

    private RequestContext(HttpServletRequest request) {
        this.request = request;
    }
    /**
     * This method will returns RequestContext object
     * @return
     */
    public static RequestContext getCurrentInstance() {
        return instance.get();
    }

    public static RequestContext newInstance(HttpServletRequest request) {
        RequestContext context = new RequestContext(request);
        instance.set(context);
        return context;
    }
    /**
     * This method will removes RequestContext object from ThreadLocal for current thread
     *
     */
    public void release() {
        instance.remove();
    }

    public HttpServletRequest getRequest() {
        return request;
    }

}

JPA Entity creation using reverse engineering


Entity creation using reverse engineering
Steps:
Step 1: Create new JPA project.

 Step 2: Give the project name.
 Step 3: Click on Next 


 Step 4: Click on next.
Step 5: Select Platform type as “Generic 2.0”.
If you have created Userlibraries with JPA required then select type “User Library. And select user library.
 If you have not created user library for JPA libraries then select “Disable library configuration”.I am going with “Disable library configuration”. Click on Finish after selecting “Disable library configuration” type.
  Step 6: Project will be opened in JPA perspective.
Step 7: Right click on the project and select Entity from Tables.
After selecting entities from tables below option will come.

   Step 8: Click on add connections.
 Step 9:  Select Oracle connection profile and enter OracleConnection as the name in text field.
 Step 10: Click on Next.
 Step 11: Enter db details.
Step 12: After entering db details then click on Test Connection.

You will get Ping Succeeded success message. If not check db details and enter correctly. 
Step 13: Click on Finish and close. 
Step 14:Right click on the project and Select Entities from Tables.

 Step 15:Select Connection as Oracle from dropdown box. After selecting it all the tables will be    displayed.
Step 16: Select Tables for which you want to create entities.
Step 17: After selecting tables click on Next.
 
Step 18:Click on Next. 
Step 19 :
Enter key generator for primarykeys.
Select entity access for field or for property
Slect fetch type.
Select Collection Properties type what you reuired.
 Step 20: Enter the package name.
 Step 21: Click on Next.
 Step 22: Default class names will be created if you want to change Select the Table and change the name.
Step 23:
Click on Finish.
Entities will be created under specified package.
Thanks,
Sudharsan M