Showing posts with label j2ee. Show all posts
Showing posts with label j2ee. Show all posts

Configure web.xml for Struts | specify struts-config.xml in Deployment Descriptor

One of benefits using the Struts framework is that application’s configuration can be specified in an external centralised configuration file (struts-config.xml) instead of being hard coded into the application. This simplifies development because changes can be made to the application without having to recompile any code. Struts configure files (struts-config.xml) are specified in the web.xml as shown below
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
......
</web-app>


The Struts configuration file is specified by declaring config parameter is a servlet initialization parameter . The config parameter must be set to your configuration file with the relative path of Web application , which will be the protected /WEB-INF/ directory so that it can be accessed only from server-side applications.
Struts supports the use of multiple configuration files. Configuration settings can be broken down into 2 or more files, which allows teams of developers to work on an application in parallel without having the configuration file be a point of contention. Multiple configuration files can be specified in web.xml as shown below seperated by comma . If there is any overlap among the files’ settings, Struts will use the last settings specified. For example, if struts-config.xml specifies a setting and then struts-config2.xml specifies the same setting with a different value, the setting in struts-config2.xml will override the one in struts-config.xml, provided that file struts-config2.xml is loaded after file struts-config.xml.
The following configuration shows the how to configure multiple configuration files.
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml, /WEB-INF/struts-config2.xml, /WEB-INF/struts-config3.xml,
</param-value>
</init-param>

Struts version 1.1 added support for application modules. The module feature allows applications to be broken as mini application inside a large application. For this we have to create a separate configuration file for each module, as shown below
<servlet>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>

<init-param>
<param-name>config/ModuleA</param-name>
<param-value>/WEB-INF/struts-config-moduleB.xml</param-value>
</init-param>

<init-param>
<param-name>config/ModuleB</param-name>
<param-value>/WEB-INF/struts-config-moduleA.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>


Note : For module configuration files, parameter names must begin with config/ in order for Struts to recognize them. eg. config/moduleA , config/moduleB . Following / is the logic name for the modules.




Read more...

Error : resource-ref: has no valid JNDI binding. Check the jboss-web/resource-ref - Solution

resource-ref: has no valid JNDI binding, which means resource-ref defined in the deployment descripter has no valid binding. Assume that you have configured JDBC Data Resources (JNDI) in Jboss 4.0 for any database (Db2 , Postgres, etc ..) . That is you have defined a reference lookup name (eg. jdbc/techLabsDS) to an external resource in the WEB-INF\web.xml and also you have created / copied *-ds.xml descriptor file with neccessary configurations like jndi-name , driver class in the in the Jboss application server folder Drive:\jboss-4.0.0\server\default\deploy . To use the resource adaptor defined in the deployment descripter (web.xml) , it must be integrated into the JBoss application server using a ds.xml descriptor file .
Now when you start the Jboss server or when you try to run the application for accessing the database , you may get the error "javax.naming.NamingException: resource-ref: jdbc/techLabsDS has no valid JNDI binding. Check the jboss-web/resource-ref" where techLabsDS is the JNDI name. To solve the this error , you have to copy the jboss-web.xml file with the following additional lines to Application's WEB-INF folder.

<resource-ref>
<res-ref-name>jdbc/techLabsDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<jndi-name>java:/jdbc/techLabsDS</resource-ref>

The following picture shows where the jboss-web.xml file to be copied.




Read more...

Solution for Unable to find a javac compiler error in Jboss 4.0 with Eclipse when running JSP file

When you run a JSP with JBoss Application server in eclipse IDE , the following error may occur "org.apache.jasper.JasperException: Unable to compile class for JSP"

JBoss appserver throws the following error
Unable to find a javac compiler , com.sun.tools.javac.Main is not on the classpath. Perhaps JAVA_HOME does not point to the JDK

The reason for the above error is "com.sun.tools.javac.Main" class which is bundled with tools.jar, is not in the class path . So we need to include the tools.jar file in the class path. When we add the jre to the build path , it includes all necessary jar files except tools.jar in the class path . Assume that jdk1.6.0_23 is installed in d:\ . jdk1.6.0_23 can be downloaded from java site.
tools.jar file will be located in the installed folder of jdk (i.e d:\jdk1.6.0_23\lib OR d:\ jdk1.6.0_23\jre\lib)

Steps to include the tools.jar file in the classpath in eclipse
1. Select Windows->Preferences ->Java -> Installed JRE
2. Highlight the selected jre and click in edit buttor as shown in the following screen shot

3. Click on Add External Jars and select the tools.jar file from the jdk\lib OR jdk\jre\lib folder then Finish.

Now you will be able to run the JSP file.


Read more...

Role / Use of JNDI API in Java with Example | Accessing Naming & Directory Service

The JNDI API gives you a tool for accessing a variety of naming services (like RMI, DNS ,Corba Naming) and directory services (like NIS, LDAP, NDS) using Java. JNDI Interface enables you to locate and use objects stored in naming and directory services. For example, a J2EE deployment may use a naming or directory service as a repository to store objects like EJBs or JDBC DataSource objects. JNDI gives you the tools to create applications that can manage, retrieve, and use those objects from the services.
Naming and Directory Concepts
----------------------------
A name is simply a human-readable logical value for referring to an object in a system. A Naming Service provides a mechanism for giving names to objects so you can retrieve and use those objects without knowing the address / location of the object. Objects can be located on any machine accessible from your network, not necessarily the local workstation. The concept of a naming service is which names are associated with objects and objects are found based on their names. The chief function of a naming service is to map people-friendly names to objects, such as addresses, identifiers, or objects typically required and used by computer programs. For example: Domain Name Systems (DNS) maps machine names (google.com) to IP addresses (such as 192.18.97.71)
A variety of naming are extended with a directory services. A directory service associates names with objects and also allows objects to have attributes. It is possible not only look up an object by its name but also get the object's attributes or even search for the object based on its attributes.

For example a telephone company's directory service. It is used to map a subscriber's name to his address and telephone number. A computer's directory service is very much like a telephone company's directory service in that both can be used to store information such as a telephone number or address. The computer's directory service is much more powerful because it is available online and can be used to store a variety of information that can be utilized by users, programs, and even the computer itself and other computers.



The JNDI API consists of two packages:

•javax.naming
•javax.naming.directory


javax.naming :- The naming interface organizes information hierarchically and maps user friendly names to addresses or objects that are machine friendly, e.g. www.novell.com -> 137.65.2.5. It allows access to named objects through multiple name spaces.

The core interfaces and classes in this package are: Context , Binding , Name , NameParser

javax.naming.directory : - The directory interface provides access to directory objects which can contain attributes thereby providing attribute-based searching and schema support. The major interfaces in this package are DirContext, Attribute and Attributes

The following example illustrates to search Datasources Object with the name "jdbc/TestDataSource" in the naming services and returns the address / location of the object wheather it may be in local or remote location.


public static Connection getConnection(String strLookupName) throws CDRCFatalException {

Context ctxLookup = null;
DataSource dBSource = null;
Connection connection = null;
try {

Hashtable param = new Hashtable();
parms.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
parms.put(Context.PROVIDER_URL, "iiop:///");
javax.sql.DataSource ds = null;

ctxLookup = new InitialContext(param);
dBSource =(javax.sql.DataSource) ctxLookup.lookup("jdbc/TestDataSource"); // jdbc/TestDataSource is JNDI Name

} catch (NamingException exc) {
System.out.println("INFO:Lookup fail of database " + exc.toString() + "The context is "+ ctxLookup);
throw new CDRCFatalException(exc.getMessage(), "01001");
}
try {
conn = dBSource.getConnection(username, password);
} catch (SQLException exc) {
System.out.println("Connection Fail of database " + exc.toString()+ "THe context is " + ctxLookup);
throw new CDRCFatalException(exc.getMessage(), "02006");
}

return connection;
}
Read more...

How JSP Functions ? Lifecycle of JSP .

Java Server Page (JSP) is a serverside program that is similar in design and functionality to JAVA Servlet . JSP program consists of a compination of HTML tags and JSP tags. JSP tags define Java code that is executed before the output of the JSP progam is sent to the browser.

How JSP functions or Lifecycle of JSP?
When a client requests a jsp , First time JSPs are converted into servlets and then compiled before they get executed. The code inside the JSP gets placed into the converted servlets service() method.

JSP has three methods which is automatically called when a JSP is requested.

1. jspInt() 2. jspDestroy() 3. sevice()

1. jspInt() : similar to init() method of a Servlet :- when a JSP is requested , jspInt() is called first.

2. jspDestroy() : similar to destroy() method of a servlet :- automatically called when a JSP terminates. can be used to cleanup the resources used during the execution (eg. disconnecting from database )

3. service() : similar to service() method :- automatically called and makes HTTP connection.
Read more...

error-page in Web.xml , Handling error for both JSP & Servlet

Errors occur during the execution of a web application. Errors can be throwed by Servlet or JSP. When an error occurs , we can display the srror in a specifc error screen (Page) . That means we can map the error code returned by HTTP response or a Java Language exception returned by any web component to any page (Html or Jsp) .
This mapping is done by the element in Deployment Descriptor (Web.xml) . This is as an error page for both servlets and JSP pages. That is Global to the application. The following sample error-page element illustrates how to map a particular Error code or Exception with a specic Error Page.



<error-page>
<error-code>404</error-code>
<location>/filenotfound.jsp</location>
</error-page>
<error-page>
<error-code>400</error-code>
<location>/badrequest.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.ClassNotFoundException</exception-type>
<location>/classnotfound.jsp</location>
</error-page>
<error-page>
<exception-type>javax.servlet.jsp.JspException</exception-type>
<location>/jspexcept.jsp</location>
</error-page>


If you need to use a different error page for some specific JSP pages (for example register.jsp) , you can use the page directive's errorPage attribute to override the web.xml declaration in those pages.

register.jsp
-------------

<%@ page language="java" contentType="text/html" errorPage="jspError.jsp" %>

if any error occurs in the register.jsp, then the jspError.jsp will be called instead of mapping error codes defined in Web.xml
Read more...

 
Copyright 2011 Java Online