Saturday, July 24, 2010

Java Threading Learnings

When creating threads, there are two reasons why implementing the Runnable interface may be preferable to extending the Thread class:

* Extending the Thread class means that the subclass cannot extend any other class, whereas a class implementing the Runnable interface
has this option.
* A class might only be interested in being runnable, and therefore, inheriting the full overhead of the Thread class would be excessive.

JSP Learnings

Hi,

JSP :

JSP is a way of developing the UI where we can embed Java Code in HTML and make use of beans

To write Java Code, We need to embed the code inside <% and %> which we call them as scriptlets

<%@ -- page directive
used to import files, include pages
e.g.,<%@page language="java" import="java.sql.*,mypackage.myclass" %> ,
<%@ include file="/header.jsp" %> ,
<%@ taglib uri="tlds/taglib.tld" prefix="mytag" %>

<%! -- delcaration
used to declare some Java Methods at the start of jsp

To write HTML Code, We need to output using out.println("test") or we can use <%=<variablename< which internally converts that into out.println and prints output

If you want to use a Java Bean inside Jsp use jsp:useBean

<jsp:useBean id="user" class="user.UserData" scope="session"/>

setProperty sets all the properties value taken from the form directly into the bean id provided. Here it is "user" bean
The only condition for this to happen automatically is, the getters and setters defined in java bean file must be same as fieldName
For example: if there is field firstName then the bean should have setFirstName and getFirstName

<jsp:setProperty name="user" property="*"/>

summary of jsp tags :

jsp:include
The jsp:include action work as a subroutine, the Java servlet temporarily passes the request and response to the specified JSP/Servlet. Control is then returned back to the current JSP page.

jsp:param
The jsp:param action is used to add the specific parameter to current request. The jsp:param tag can be used inside a jsp:include, jsp:forward or jsp:params block.

jsp:forward
The jsp:forward tag is used to hand off the request and response to another JSP or servlet. In this case the request never return to the calling JSP page.

jsp:plugin
In older versions of Netscape Navigator and Internet Explorer; different tags is used to embed applet. The jsp:plugin tag actually generates the appropriate HTML code the embed the Applets correctly.

jsp:fallback
The jsp:fallback tag is used to specify the message to be shown on the browser if applets is not supported by browser.
Example:
<jsp:fallback>
<p>Unable to load applet</p>
</jsp:fallback>

jsp:getProperty
The jsp:getProperty is used to get specified property from the JavaBean object.

jsp:setProperty
The jsp:setProperty tag is used to set a property in the JavaBean object.

jsp:useBean
The jsp:useBean tag is used to instantiate an object of Java Bean or it can re-use existing java bean object.


A Ram Prasad

Saturday, July 17, 2010

Hibernate No CurrentSessionContext Exception

Hi

While starting hibernate you might get the following exception

org.hibernate.HibernateException: No CurrentSessionContext configured!

The reason for the above error is the property "current_session_context_class" is missing

The solution you need to follow is to add
<property name="current_session_context_class">thread</property>
in the hibernate.cfg.xml file

A Ram Prasad

Adding a custom jar to maven-repo effectively

Hi

Maven gives a wonderful way to add custom jars to maven-repo and make use of the jar in the project. Here is how it is :

If You have a jar not available in maven repository but it is needed for your project
thereby when maven build your project, you want maven to pick from the local repository instead of downloading it from the web or maven URL (http://repo1.maven.org/maven2/)

For Example :

I have a hibernate-jpa-custom jar which i want to use the jar available with me while doing maven build

Steps to follow that:

1) First install the jar into local maven-repository using

mvn install:install-file -DgroupId=org.hibernate -DartifactId=hibernate-jpa-custom -Dversion=2.0 -Dpackaging=jar -Dfile= -DgeneratePom=true


Going by the command :

groupId - denotes the group of the artifact
artifactId - name of the artifact
version - version of the artifact which you want to use in your project
file - the jar library
generatePom - to generate pom for the jar file automatically
this option is very important in order to avoid errors like

[INFO] Copying 0 resource
Downloading: http://repo1.maven.org/maven2/org/hibernate/hibernate-jpa/2.0/hibernate-jpa-2.0.pom
[INFO] Unable to find resource 'org.hibernate:hibernate-jpa:pom:2.0' in repository central (http://repo1.maven.org/maven2)

Since there is no pom available to look in the local repository, maven tool assumes that there is no jar in local repository and has to contact maven repo URL to download the jar thereby showing the error message "unable to download"

2) make a entry in the pom.xml of your project for the jar which you just added to local maven repo
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpa-custom</artifactId>
<version>2.0</version>
</dependency>

3) use mvn install to do a install of the project with newly added jars

Now your project will make use of the custom jar

let me know in case of any queries

Thank You
Ram Prasad