How to change IBM WebSphere Application Server’s Class Loading properties (enabling Hot Deployment)


If you need to configure WebSphere App. Servers’ class loading behavior and need to have a hot deployment functionality on your WAS application server. You should follow the following steps;

Click on Applications section on the left menu

Select Enterprise Applications

Click on the enterprise Application (You ear file name-application name)

Click on Class Loading and update detection under the menu Detail Properties

(Enterprise Applications > ApplicationName> Class loader)

Under the menu General Properties

Click on check box near to text –>  Reload classes when application files are updated.

set a value for Polling interval for updated files  textbox such as 10 seconds.

Select radio Classes loaded with parent class loader first under the menu Class loader menu

Select radio Class loader for each WAR file in application under the menu WAR class

Click apply button.

After applying these operations, deploying required classes on WebSphere Application Server (these could be a jar file) will be reflected instantly and you don’t need to restart WAS.

How to use <h:selectOneMenu /> tag JSF 2.0 Example


Friends the following udemy course created by me;

      Click here to attend Spring Framework 4.x and certification course with a discount

Spring Framework and Core Spring Certification Udemy course with discount
Spring Framework and Core Spring Certification Udemy course with a discount coupon

First create a managed bean by using the annotation syntax;

package generator;

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author tunatore
 */
@ManagedBean(name=”generator”)
@RequestScoped
public class OptionSelectGenerator {

private List<SelectItem> options = null;
    /** Creates a new instance of OptionSelectGenerator */
private String selected;
public OptionSelectGenerator() {
this.selected = “SelectItemValue1”;
options = new ArrayList<SelectItem>();
SelectItem item1 = new SelectItem(“SelectItemValue1”, “SelectItemLabel1”);
SelectItem item2 = new SelectItem(“SelectItemValue2”, “SelectItemLabel2”);
SelectItem item3 = new SelectItem(“SelectItemValue3”, “SelectItemLabel3”);
SelectItem item4 = new SelectItem(“SelectItemValue4”, “SelectItemLabel4”);
options.add(item1);
options.add(item2);
options.add(item3);
options.add(item4);
}
  public List<SelectItem>  getOptions() {
return options;
}

/**
     * @param options the options to set
     */
public void setOptions(List<SelectItem> options) {
this.options = options;
}

/**
     * @return the selected
     */
public String getSelected() {
return selected;
}

    /**
     * @param selected the selected to set
     */
public void setSelected(String selected) {
this.selected = selected;
}
}

Use it inside xhtml file

<?xml version=’1.0′ encoding=’UTF-8′ ?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml&#8221;
xmlns:h=”http://java.sun.com/jsf/html&#8221;
xmlns:f=”http://java.sun.com/jsf/core”&gt;
<h:head>
<title>h:selectOneMenu example</title>
</h:head>
<h:body style=”background-color: aquamarine”>
<h:form>
<h1><h:selectOneMenu/>  example</h1>
        <h:selectOneMenu value=”#{generator.selected}” onchange=”submit();”>
            <f:selectItems value=”#{generator.options}” />
       </h:selectOneMenu>
<b>Selected –></b> <b><h:outputText value=”#{generator.selected}”/></b>
</h:form>
</h:body>
</html>

Sample output;


Download Source

How to develop and use custom tags inside a J2EE application example


J2EE specification allows programmers developing their own tags. By implementing custom tags procedures, programmers can develop custom tags such as <customTags:HeaderLogo>, <customTags:FooterLogo> or <abc:XYZ> inside a J2EE application.

Normally, programmers could have similar import functionality by using SUN’s tags however custom tag development gives more flexibility and provides readable code for  programmers .  However complex tag development is also available in J2EE environment by using tag handlers provided by J2EE specification.  I will post a more detailed example on this topic (tag handlers simple and classic tag handler development) . In this post, I will give an example about coding standard tags.

First of all, you will need to create a tags folder inside WEB-INF folder to prevent unwanted access for your tag files.

And create a LogoTag.tag file inside tags folder with the following content

<%–
    Document   : LogoTag
    Created on : 26.Jun.2011, 19:49:48
    Author     : tunatore
–%>

<%@tag description=”LogoTags” pageEncoding=”UTF-8″%>

<%– this attribute will take its value from the request object –%>
<%@attribute name=”message”%>

<%– content for the LogoTag–%>
<h2>${message}</h2> </br>
<img src=”images/java_powered_logo.jpg”/> </br>

And create another tag file name DescriptionTag.tag and put it into the tags folder too.

<%–
    Document   : DescriptionTag
    Created on : 26.Jun.2011, 20:01:08
    Author     : tunatore
–%>

<%@attribute name=”fontSize” required=”true”%>

<%@tag description=”DescriptionTag” body-content=”tagdependent” pageEncoding=”UTF-8″%>

<font size=”${fontSize}“><jsp:doBody/></font>

You should put jsp:doBody tag like the above one for getting a long content from the custom tags’ body.

Lets, create an index.jsp file for using the custom tags developed

<%–
    Document   : index
    Created on : 26.Jun.2011, 19:47:51
    Author     : tunatore
–%>

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<%@taglib prefix=”customTags” tagdir=”/WEB-INF/tags”%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSP Page</title>
</head>
<body>
<h1>CustomTagDevelopment</h1>
<% request.setAttribute(“message”, “Custom tag will display this mesage”); %>
<customTags:LogoTag/>
<customTags:DescriptionTag fontSize=”12″>
Description TEXT will go here…  </br>  
Line </br>    
By </br>
Line
</customTags:DescriptionTag>
</body>
</html>

Lets deploy the web application and run it;

Download Source

How to print Hello World! using Python


First download Python on your computer and install it using Windows Installer.

I am using Python 2.7.2 Windows Installer (Windows binary — does not include source) for this code.

After installing Python go to Start>All Programs>Python 2.7>IDLE(Python GUI)

You will see a console such as the following one;

On this Shell if you write a code;

print  “Hello World!”

You will see Hello World text on the console output

Another approach for this code is to create a file with an extension such as helloworldPrinter.py

And calling it from the windows console

The content for helloworldPrinter.py

# this is a comment –>  Hello world python program
# Introduction to Python Development
# Hello World Progran
# author – Tuna TORE

print “Hello World!” # regular print statement

# this is a function
def hello():
    print “Hello World! Function”

if __name__ == “__main__”: # main method
    hello()

The output for the program

Download helloworldPrinter.py file

How to use Ant-Contrib Tasks ant:if example


If you want to use ANT-contrib tasks inside your ANT project. First you must install ANT itself on your machine.

Second you should download the required ant-contrib (ant-contrib-1.0b3.jar) library from here and put it inside ANT_HOME\lib folder

You should also add the following lines(green lines) in your build.xml file

<project name=”projectname” default=”buildProject” basedir=”.” xmlns:ant=”antlib:net.sf.antcontrib>

        <taskdef resource=”net/sf/antcontrib/antlib.xml” uri=”http://ant-contrib.sourceforge.net”&gt;
            <classpath>
                <pathelement location=”${ant-contib.jar.location}”/>
            </classpath>
        </taskdef>       

Here is a complete project for testing a ant-if task;

build.properties file for ant properties


#javac.debug=on
#author: Tuna Tore#this is a comment
failmessage=Please press key 1, 2 or 3...  Other key is pressed!!!
ant-contib.jar.location=YouShouldWriteHereANT_HOMELocation\\lib\\ant-contrib-1.0b3.jar
javac.deprecation=off
javac.nowarn=off

Here is build.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!-- Author: Tuna Tore -->
    <project name="testingantcontrib" default="buildProject" basedir="." xmlns:ant="antlib:net.sf.antcontrib">
        <taskdef resource="net/sf/antcontrib/antlib.xml" uri="http://ant-contrib.sourceforge.net">
            <classpath>
                <pathelement location="${ant-contib.jar.location}"/>
            </classpath>
        </taskdef>
        <description>
         This script will use if conditions inside ANT tasks,
         Console input will take some inputs from users and the program's flow
         will be changed according to these inputs.
        </description>
        <!-- getting build.properties file and set a prefix for it -->
        <property file="build.properties" prefix="projectproperties"/>
        <target name="decideProgramFlow" description="Deciding" >
        <input message="Please enter key 1,2 or 3..." addproperty="input"/>
        <ant:if>
            <equals arg1="${input}" arg2="1" trim="true"/>
                <then>
                    <antcall target="onePressed" />
                </then>
        <elseif>
            <equals arg1="${input}" arg2="2" trim="true"/>
                <then>
                    <antcall target="twoPressed" />
                </then>
        </elseif>
        <elseif>
            <equals arg1="${input}" arg2="3" trim="true"/>
                <then>
                    <antcall target="threePressed" />
                </then>
        </elseif>
        <else>
            <antcall target="fail" />
        </else>
        </ant:if>
        </target>
        <target name="onePressed">
            <echo message="ONE is pressed!..."/>
        </target>
        <target name="twoPressed">
            <echo message="TWO is pressed!..."/>
        </target>
        <target name="threePressed">
            <echo message="THREE is pressed!..."/>
        </target>
        <target name="fail">
            <fail message="${projectproperties.failmessage} pressed--> ${input}"/>
        </target>
        <target name="buildProject" depends="decideProgramFlow">
            <echo message="decideProgramFlow target is called by ANT..."/>
        </target>
</project>

If you call this project using ant command(from terminal or console), theoutput will be such as the following one (will be changed based on input);

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\tunat>cd Desktop

C:\Documents and Settings\tunat\Desktop>cd antDevelopment

C:\Documents and Settings\tunat\Desktop\antDevelopment>ant
Buildfile: C:\Documents and Settings\tunat\Desktop\antDevelopment\build.xml

decideProgramFlow:
    [input] Please enter key 1,2 or 3…
1

onePressed:
     [echo] ONE is pressed!…

buildProject:
     [echo] decideProgramFlow target is called by ANT…

BUILD SUCCESSFUL
Total time: 9 seconds

C:\Documents and Settings\tunat\Desktop\antDevelopment>ant
Buildfile: C:\Documents and Settings\tunat\Desktop\antDevelopment\build.xml

decideProgramFlow:
    [input] Please enter key 1,2 or 3…
4

fail:

BUILD FAILED
C:\Documents and Settings\tunat\Desktop\antDevelopment\build.xml:41: The following error occurred while executing this line:
C:\Documents and Settings\tunat\Desktop\antDevelopment\build.xml:59: Please press key 1, 2 or 3…  Other key is pressed!!! pressed–> 4

Total time: 4 seconds

C:\Documents and Settings\tunat\Desktop\antDevelopment>
Download source –> build.xml and build.properties file

How to use JSTL tags c:forEach, c:if, c:choose and c:catch example


      Click here to attend Spring Framework 4.x and certification course with a discount

If you want to use JSTL core tags inside your web application use the following codes;

First include JSTL standard.jar and jstl.jar inside your web project and create a servlet class for setting attributes used by JSTL tags;

CustomServlet .java

package com.tunatore.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author tunatore
 */
public class CustomServlet extends HttpServlet {

   /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
        response.setContentType(“text/html;charset=UTF-8”);
        String[] carList = {“SUBARU”,”HONDA”,”TOYOTA”,”SUZUKI”,”VOLCO”,”MERCEDES”};
        request.setAttribute(“carList”, carList);
        request.setAttribute(“username”, “TUNA”);
//forwarding request to resulst.jsp page
        request.getRequestDispatcher(“/result.jsp”).forward(request, response);
}

    // <editor-fold defaultstate=”collapsed” desc=”HttpServlet methods. Click on the + sign on the left to edit the code.”>
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
@Override
public String getServletInfo() {
return “Short description”;
}// </editor-fold>
}

And create a index.jsp file for forwarding first request to servlet by the following;

<%–
    Document   : index
    Created on : 19.Jun.2011, 19:48:51
    Author     : tunatore
–%>

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSTL Examples</title>
</head>
<body>
<h1>JSTL index</h1>
   <!–this tag will forward request to CustomServlet–>
        <jsp:forward page=”/CustomServlet” />
</body>
</html>

And last create a result.jsp file for testing JSTL core tags;

<%–
    Document   : result
    Created on : 19.Haz.2011, 19:50:24
    Author     : tunatore
–%>
<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core&#8221; %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSTL Examples</title>
</head>
<body bgcolor=”#A9D0F5″>
<h1>JSTL</h1>
<table border=”1″>

<!– c:forEach tag will get carList from the request object and iterate over it–>
            <!– nesting c:forEach tags could be done too–>
<c:forEach var=”car” items=“${carList}”>
<tr>
<td>${car}</td>
</tr>
</c:forEach>
<tr>
<td>
        <!– testing c:if test tag usage –>
        <c:if test=“${username ==’TUNA’}”>
                    Username is –> TUNA c:if test –> WORKED         
         </c:if>
         <!– the following syntax is also true you can use –>
          <!– == (or eq), != (or ne), < (or lt), > (or gt), <= (or le), >= (or ge)–>
          <c:if test=“${username eq ‘OTHERS’}”>
                     Username is –> OTHERS                
          </c:if>    
</td>
</tr>
<tr>
<td>
<!– testing c:choose tag usage –>
       <c:choose>
             <c:when test=“${username eq ‘BILL’}”>
                     Username is –> BILL  
              </c:when>                        
              <c:when test=“${username eq ‘JAMES’}”>
                      Username is –> JAMES  
              </c:when>
              <c:otherwise>
                       Username is –> c:otherwise WORKED! == TRUE  
             </c:otherwise>
     </c:choose>      
</td>
</tr>
<tr>
<td>
<!– testing c:catch tag usage –>
     <c:catch var=“customException”>
           <% int abc = Integer.parseInt(“ABC”);%>                        
      </c:catch>    
       <c:if test=“${customException !=null}”>
             <c:out value=“${customException}”/>                    
       </c:if>
</td>
</tr>
</table>
</body>
</html>

Result of the program;


Download Source

How to define a HttpSessionBindingListener for a J2EE Web Application


HttpSessionBindingListener interface is used for tracking of session events on the object. If an object implements HttpSessionBindingListener interface, it will be aware of its association with a  HttpSession.

First create a class that implements HttpSessionBindingListener interface

package com.tunatore.listeners;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

/**
 *
 * @author tunatore
 */
public class ObjectWillBeInSession implements HttpSessionBindingListener{

private String property1 = null;
private String property2 =null;

    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        //code to run when ObjectWillBeInSession object associated with a http session
        System.out.println(“valueBound is working!”);
        System.out.println(“object’s property1 value –> ” + ((ObjectWillBeInSession)event.getValue()).getProperty1());
        System.out.println(“object’s property2 value –> ” + ((ObjectWillBeInSession)event.getValue()).getProperty2());
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        //code to run when ObjectWillBeInSession object removed from a http session        
        //logging into a database server could be done here
        System.out.println(“valueUnbound is working!”);
        System.out.println(“object’s property1 value –> ” + ((ObjectWillBeInSession)event.getValue()).getProperty1());
        System.out.println(“object’s property2 value –> ” + ((ObjectWillBeInSession)event.getValue()).getProperty2());
    }

/**
     * @return the property1
     */
public String getProperty1() {
return property1;
}

   /**
     * @param property1 the property1 to set
     */
public void setProperty1(String property1) {
this.property1 = property1;
}

   /**
     * @return the property2
     */
public String getProperty2() {
return property2;
}

/**
     * @param property2 the property2 to set
     */
public void setProperty2(String property2) {
this.property2 = property2;
}

}

And define it in the web.xml file

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”3.0″ xmlns=”http://java.sun.com/xml/ns/javaee&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”&gt;
    <listener>
        <description>HttpSessionBindingListener</description>
        <listener-class>com.tunatore.listeners.ObjectWillBeInSession</listener-class>
    </listener>
   <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

And last create a jsp file for testing listener class

<%–
    Document   : index
    Created on : 18.Jun.2011, 21:19:35
    Author     : tunatore
–%>

<%@page import=”com.tunatore.listeners.ObjectWillBeInSession”%>
<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Testing HttpSessionBindingListeners</title>
</head>
<body>
<h1>Testing HttpSessionBindingListeners</h1>
        <%
            ObjectWillBeInSession owi = new ObjectWillBeInSession();
            owi.setProperty1(“I am a value for Property1”);
            owi.setProperty2(“I am a value for Property2”);
            //this will call HttpSessionBindingListener’s
            //valueBound method for the object
            session.setAttribute(“owi”, owi);

            //this will call HttpSessionBindingListener’s
            //valueUnbound method for the object
            session.removeAttribute(“owi”);        
        %>
</body>
</html>

The output of the program(taken from System.out)

valueBound is working!
object’s property1 value –> I am a value for Property1
object’s property2 value –> I am a value for Property2
valueUnbound is working!
object’s property1 value –> I am a value for Property1
object’s property2 value –> I am a value for Property2

Download Source