Tuesday, February 16, 2010

Jdeveloper 11g in Windows 7

I was trying to install Jdeveloper 11g in my windows 7 machine. I got it installed correctly, but the problem I had was when I started it then it was throwing some exception and it quited. The exception was something like the following:

oracle.adf.rc.config.ConfigurationException: an ADFContext has not been registered for name [oracle.jdeveloper.rescat2.ResourcePalette]. Root Cause=[] [Root exception is java.lang.NullPointerException]

In my case this issue was due to the user profile name in my system. It had spaces. The jdeveloper by default creates its directories in the C:\DocumentSetting\Users\
My profile name was like Vv & Aa. It had spaces before and after the ampersand. So what I did to make it work was to set the JDEV_USER_DIR to another folder.

In the command prompt set the JDEV_USER_DIR to some other folder of your choice.

set JDEV_USER_DIR = F:\myJdevProfile

Then go to the directory where jdeveloper is installed and start it from the command prompt

F:>jdeveloper> jdeveloper.exe

You have to start jdeveloper every time like this after setting the JDEV_USER_DIR
Other wise you can set it permenantly by setting the environment variable.

For windows 7, right click Computer, click properties, click Advanced system settings, click environment variables in the advanced tab. In the system variables, create new variable and give name as JDEV_USER_DIR and value as the directory of your choice. (here it is F:\myJdevProfile)

Here after you can start jdeveloper from the start menu itself by clicking the jdeveloper icon.

Some useful links:

http://forums.oracle.com/forums/thread.jspa?threadID=983953

http://jdeveloperfaq.blogspot.com/2009/12/faq-1-how-to-configue-stand-alone.html

Friday, January 22, 2010

Formating date in java

 Date date = new Date();  
 Format formatter = new SimpleDateFormat("MM/dd/yy");  
 String s= formatter.format(date);  
 System.out.println(s);  

Friday, October 16, 2009

ServletContext and ServletConfig

ServletConfig

  • The servlet gets the init params from the ServletConfig. Each servlet has a ServletConfig object.

  • When the container initializes a servlet, it makes a unique ServletConfig object for it.

  • The servlet init params are read only once and is available only for that particular servlet.

  • The init params are given in the DD within the < servlet> tag.
 <servlet>  
 <init-param>  
 <param-name> name </param-name>  
 <param-value> val </param-value >  
 </init-param>  
 </servlet>  
  • The container first reads the DD and gets the init-params.

  • Then the container creates a new ServletConfig instance for the servlet

  • Container creates a new name/value pair of Strings for each init parameter

  • Container gives the ServletConfig reference to name/value pair

  • Container creates a instance of the servlet class

  • Container calls the init(ServletConfig) method by passing the ServletConfig reference

  • The servlet can access the init params using getServletConfig().getInitParameter("name")

    ServletContext

    The ServletContext is for the webapp. The container makes a ServletContext when a web application is deployed and makes it available to all the servlets and JSPs of that application. Context init params are available to the entire webapp. Any servlet and JSP in the app has access to the context init params. The context params are given in the DD outside the < servlet > tag.

    < web-app .... >
    < context-param >
    < param-name > name < /param-name >
    < param-value > val < /param-value >
    < /context-param >
    < servlet > < /servlet>
    < /we-app >

    The container reads the DD and creates name/value String for each context-param.
    Container creates a instance of ServletContext.
    Container gives the ServletContext reference to each name/value pair of the context params.
    Every JSPs and servlets deployed in that webapp can access this context params through the ServletContext using the method getServletContext().getInitParameter("name")

    Servlet Life Cycle

    A Servlet is controlled by the Container. The servlet has only one state - Initialized.

    1. The web container first loads the Servlet (.class file).

    2. The web container creates an instance of the Servlet by calling the constructor

    3. The web container then calls the init() method of the servlet which initializes the servlet. It is called only once

    4. The web container then calls the service() method of the servlet which in turn calls the respective doGet() or doPost() depending on the type of request. For each request, a separate thread is used.

    5. Finally the container calls the destroy() method which cleans up and make it ready for garbage collection. It is also called only once.



    Servlet classes and life cycle methods:

    javax.servlet.GenericServlet implements javax.servlet.Servlet (interface)
    javax.servelt.http.HttpServlet extends javax.servlet.GenericServlet
    MyServlet extends javax.servelt.http.HttpServlet


    The init() method of the Generic Servlet is called if it is not overriden in the MyServlet. The init() can be used to initalize the database before processing the request.
    The service() method of the HttpServlet is called. We don't need to override it.
    The service() method in HttpServlet calls the overrriden doGet() or doPost() of MyServlet.


    Note:


    • There is only one servlet instance per JVM

    • Each request runs in a separate thread.

    • Servlet is loaded and initialized only once when the container starts up.

    • init() always completes before the first call to service()

    • The constructor of the servlet class just creats an ordinary object. It becomes a servlet when it is initalized after init().

    J2EE Server

    A J2EE server incorporates web container and EJB container. The web container has the web components
    (Servlet + JSP ).
    The EJB container has the business components.

    A web server has only Web container. Apache is the web server and Tomcat is the web container
    There is no stand alone EJB containers now a days.

    J2EE servers are WebService , JBoss which has both web and EJB containers.

    How to configure Tomcat 6 in Eclipse

    Steps for configuring Tomcat 6 in Eclipse


    • Download Eclipse and extract it into a folder. (I have downloaded Eclipse-Galileo [Eclipse IDE for Java EE developers] from http://www.eclipse.org/downloads/ and extracted in into D:\Eclipse )
    • Open eclispe by double clicking eclipse.exe
    • Create a workspace
    • In windows->Preferences->server -> run time environment
    • Click add, select the Tomcat v6, click Next
    • Set the Tomcat installation directory (for me it is D:\apache-tomcat-6.0.20)
    • Set the JRE by clicking Installed JREs -> Add -> Standard VM -> set the JRE home to JDK folder (path in step 3)
    • Click finish
    • Select the newly added JRE and click Ok
    • Select the newly added JRE from the drop down of JRE and click Finish
    • At the top right corner of eclipse there is a button 'Open perspective'. Select Java EE perspective.
    • If you can't see 'Server' tab at the bottom, select Windows->show view -> server
    • In the server tab at the bottom, Right click -> New Server -> Select the Tomcat v6 and click Finish


    • If you want to run the tomcat manually from command prompt then set the environment variable JAVA_HOME to jdk path ie the path in step 3

    web container

    Web Container


    Container is something that extends the functionality of a web server.Web server cannot alone handle the dynamic pages . The container does it.Tomcat is an eg: of a container. While apache is the web server.Servlets are deployed in the Container.

    Container provides -
    communication support - we dont have to worry about creating sockets, listners, streams
    lifecycle management - it controls the life and death of servlets
    multithreading support - automatically creats a new thread when a request comes
    provides security - it has a deployment descriptor DD which is an XML which helps to manage the security with out changing java source code.
    jsp support - translates the jsp to java

    When a request for a dynamic page comes, the web server hands over it to the container and then the container does the following:

    container creates two objects - HttpServletRequest and HttpServletResponse
    finds the correct servlet based on URL, creates/allocates a thread, passes the request and response object to the servlet thread
    calls the service() method
    based on the request type the service() method calls doGet() or doPost() method
    the doXxx() method generates the dynamic pages and put it into the respose object
    thread is completed
    container converts the response to Http response and sends it to the client
    deletes the request and response objects

    Followers