How to create a Servlet and use it from a JSP page (ADF 10g)

Create the Servlet

  1. Right-click on the package you need it for and select New…
  2. From the new gallery select Web-Tier > Servlets on the left and on the right select HTTP Servlet
  3. Click Next and give the Servlet:
  1. Class name (ImageServlet)
  2. the Package it will reside in
  3. select doGet() method to implement
  • click Next and set the Mapping details:
    1. Name: the name to use in the application
    2. Url pattern: the url path to use in jsp pages (/somePath)
  • Finish
  • In the class, complete the method doGet() with what the servlet should do. If it is a DB query i.e.:

    We need a connection to the DB to make a tranaction…

    [java]

    public static Connection getConnection() throws Exception {

    String amDef = “bc.datamodel.common”;
    String config = “ModelNameLocal”;
    Connection con = null;
    ApplicationModule am = null;

    try {
    am = Configuration.createRootApplicationModule(amDef, config);
    con = ((DBTransactionImpl)am.getTransaction()).getPersistManagerConnection();
    } finally {
    Configuration.releaseRootApplicationModule(am, true);
    }

    return con;

    }

    [/java] (more…)

    How to add a new field to an existing View Object in ADF 10g

    Case: You need a new field on a table in the DB and need to use it on a jspx page in ADF 10g.

    After you have added the column in the relevant table in the DB, turn to the ADF application and find the relevant view you need to add the new field.

    1. Double-click on the view object in your project (View Object Editor pops-up)
    2. Select Attributes on the left pane
    3. Click New to add the new field
    4. New View Object Attributes
      • Attribute:
      • Name: a java name that’ll be used by java classes
      • Type: set the type of this field to be used in java
      • Maped to column or SQL: selected
      • Queryable: selected
      • Updatable: select “Always”
      • Query column:
      • Alias: type the name to be used by ADF Model
      • Type: set the type as set in the DB
      • Expression: type the column name as set in the DB or a relevant SQL expression for the column
    5. From the selected attributes on the View Object Editor you should now see the new attribute you just created and be set as “calculated”
    6. Rebuild the Model project and you should then have the new field available in the relevant DataControl object.

    How to add a OutputText/InputHidden field on a jspx page (ADF 10g)

    Level: Advanced
    Prerequisits: Model, DataControl and Backing-bean class

    1. Find the page you need to add the field to i.e. CreateNewEmp.jspx

    2. In the body > form of the jsp(x) page you should typically have a “panelForm” where fields are placed on, add a “panelLabelAndMessage” item:

    r-click on af:panelForm > Insert inside af:panelForm > PanelLabelAndMessage

    3. Then add an “OutputText” item (to display the field):

    r-click on af:panelLabelAndMessage > Insert inside af:panelLabelAndMessage > browse… > OutputText

    4. Switch to source-view and set a value to the item, to be able to display it:

    [javascript]<af:outputText value=”#{uploadSession.empid}”[/javascript]

    If you need to save it to a database table you need to add a “Binding” to it:

    [javascript]binding=”#{backing_app_CreateNewEmp.empid}”/>[/javascript]
    Note: if the binding doesn’t exist inside the backing bean, it will be created (a set and get method).

    The full chunk of code (.jspx):

    [javascript]<af:panelLabelAndMessage label=”Emp Id”>
    <af:outputText value=”#{uploadSession.empid}”
    binding=”#{backing_app_CreateNewEmp.empid}”/>
    </af:panelLabelAndMessage>[/javascript]

    java backing-bean (declaritively created from jsp):

    [java]public void setEmpid(CoreOutputText empid) {
    this.empid = empid;
    }

    public CoreOutputText getEmpid() {
    return empid;
    }[/java]

    To make the field hidden from user view, do the same procedure as with OutputText but use InputHidden instead. Note that you shouldn’t just change the code inside the jsp as it will not change the variables created in the backing-bean class. This depends on what you select at first when adding the new field type on the jsp page.