This allows the tomcat to create a log file under the logs directory in tomcat home and create a file with the prefix access_log and add the requests that came .
A excerpt from the access_log in my box ,
[24/Jun/2010:01:22:33 -0500] HTTP/1.1 cookie:- request:- GET /manager/html 200 GET /manager/html HTTP/1.1
[24/Jun/2010:01:22:36 -0500] HTTP/1.1 cookie:- request:- GET /SimpleSample/ 200 GET /SimpleSample/ HTTP/1.1
[24/Jun/2010:01:22:36 -0500] HTTP/1.1 cookie:- request:- POST /SimpleSample/SimpleSampleServlet 200 POST /SimpleSample/SimpleSampleServlet HTTP/1.1
JBoss web server provides a single deployment platform for all light weight java applications.it integrates Apache Tomcat, Apache Web Server and all of the common connectors.It provides deployment support for java server pages and java servlet technologies ,Php and CGI.It uses a genuine high performance hybrid technology that incorporates the best of the most recent OS technologies for processing high volume data, while keeping all the reference Java specifications.
In this articles we will see how we can access a DataSource Configured in Jboss Web Server by uisng Jndi.
The Sample application that we create connects to Microsoft Access using ODBC . JBoss web provides a Context implementation for all the web applications running under it.there are some elements that are to be configured in WEB-INF/web.xml in order to refer the resources.
Resources referenced in web.xml can be defined in 2 ways
either as a global resource which can defined in CATLINE_HOME/conf/context.xml or
for a application specific which is defined in Application/META-INF/context.xml
in this articles we will define the Datasource which connects to ODBC and defined it in CATLINE_HOME/conf/context.xml.
lets see how we can configure a ODBC DataSource .
Click Start, point to Control Panel, double-click Administrative Tools, and then double-click Data Sources(ODBC).
Click the User DSN tab, and then click Add.
Select "Microsoft Access Driver" and Finish
in Datasource name "mySource" and select the database "student".[ student is also available to download]
5.in advanced options provide the username and password which will be used when defining the resource
Once datasource is configured , we will create the resource in CATLINE_HOME/conf/context.xml
<Resource name="jdbc/StudentDB" auth="Container"
type="javax.sql.DataSource" username="jagadesh" password="jagadesh"
driverClassName="sun.jdbc.odbc.JdbcOdbcDriver" url="jdbc:odbc:mySource"
maxActive="8" maxIdle="4"/>
we just added a resource to the context.xml file which is now available to all applications and can be obtained by Jndi . lets see the Resource element
name=the name of the dataSource which is accessed from Jndi.
auth=Specify whether the web Application code signs on to the corresponding resource manager programatically, or whether the Container will sign on to the resource manager on behalf of the application. The value of this attribute must be Application or Container.
type=the java class name which the application expects when it performs a lookup.
username=userrname to connect to datasource.
password=Password to connect to datasource.
driverClassName=Class name of the driver to connect.
url=the url to connect. maxActive: Maximum number of dB connections in pool. maxIdle: Maximum number of idle dB connections to retain in pool.
so now we have defined the Resource in the CATALINE_HOME/conf/context.xml file. we need to refer the resource in our web.xml file
so in web.xml we have ,
<resource-env-ref>
<description>Data Source For MSAcess DataBase</description>
<resource-env-ref-name>jdbc/StudentDB</resource-env-ref-name>
<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>
The resource-env-ref element contains a declaration of a Web application's reference to an administered object associated with a resource in the Web application's environment.
resource-env-ref-name - the resource reference name
resource-env-ref-type - the resource type
Now we have defined the resource and we also made a reference to the resource from our web application.now we will see how can we access the resource from the application code .
/** Creates a Connection to a Access Database */
public static Connection getAccessDBConnection() throws SQLException {
Connection con=null;
try {
con=DriverManager.getConnection("jdbc:odbc:mySource");
}catch(Exception e){
e.printStackTrace();
}
return con;
}
} We have seen how we can configure a datasource in CATLINE_HOME/conf/context.xml file and refer it in the web.xml . we also seen how we can access the resource in application code.
In This article , we will be seeing how we can create a sample web application in maven and deploy that in tomcat server .So we will start creating a web application in maven by executing the command ,
So we have the POM file, now will see the directory structure
We have web.xml in WEB-INF which contains,
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
Now we will add a simple servlet to the web application, for this we need to create the package as we defined in the command [for creating a web project above] .The structure looks like this after package creation.
So I have created the “SampleServlet” in web directory. We need to update the web.xml also like
<web-app>
<display-name>Archetype Created Web Application</display-name>
So now we will try to execute the command “mvn compile” to compile all the source files. This command throws the exception, since we don’t have the libraries available
C:\WebApplication\src\main\java\com\SampleWeb\WebApplication\web\SampleServlet.java:[4,20] package javax.servlet does not exist
C:\WebApplication\src\main\java\com\SampleWeb\WebApplication\web\SampleServlet.java:[5,25] package javax.servlet.http does not exist
C:\WebApplication\src\main\java\com\SampleWeb\WebApplication\web\SampleServlet.java:[6,25] package javax.servlet.http does not exist
C:\WebApplication\src\main\java\com\SampleWeb\WebApplication\web\SampleServlet.java:[7,25] package javax.servlet.http does not exist
So for this we will make sure that maven downloads the required artifacts [jars] for compiling the source code. We need servlet-api.jar and jsp-api.jar. In order to make sure the jars are available to the maven, we need to make some modifications to the POM file like adding the dependencies. The complete POM looks like
We have an artifact Search available, by which we can search the artifact by the name and many other ways. We can search for any type of artifact in this.
Once we made the changes to the POM file, we will once again run “mvn clean install” .once this executes it downloads all the necessary artifacts and compiles the source code, create a target directory and also creates a war file with the name specified in the finalName element in Build.
So now we have created a sample web application which contains a simple servlet.Now we will see how we can deploy the war file in Tomcat and Jetty Server using plug-ins.
Deploying an Application in Tomcat
In order to deploy the application in tomcat, we need to make use of the tomcat plug-in.for using the tomcat plug-in we need to make some changes to the POM file for downloading the necessary plug-in. So add the plug-in element to the build element in POM file like
<build>
<finalName>WebApplication</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<url>http://localhost:29801/manager/html</url>
<server>netcontrol</server>
<path>/simple</path>
</configuration>
</plugin>
</plugins>
</build>
So now we have added the plug-in, we need to run the install command to download the plug-in. so we execute the “mvn clean install”.This command makes sure to download the necessary tomcat plugin.lets see the configuration part in plug-in element for tomcat.
url : the url of the tomcat .
path is the web-path defined in the url-pattern of the web.xml.
The server element is the important thing, in order to provide the authentication to the tomcat server; we will create a server element in the setting.xml file which is in c: drive. Go for the user and look for .m2 directory which contains the setting.xml file. So once you find the file, add the servers element to the file like
<servers>
<server>
<id>netcontrol</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>
We have specified a server name which has the user name and password for authentication. Now execute the “mvn -e clean tomcat:deploy” . so the info in the console may look like ,
mvn -e clean tomcat:deploy
+ Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'tomcat'.
In a daily application, properties play key roles in providing data at required time. In maven also we have a facility to set properties and access them at required places. We can access system, operating system specific properties also.
Maven properties are like place holders, they can be accessed anywhere in the POM file and can be accessed using ${x}, where x is a property to be accessed.
So in this article we will see how to access system and also user defined properties in Maven by creating a sample application,
So now we want to see the value defined for the myName property. How are we going to do this, for this we will use the maven-antrun plugin.we can use the echo of Ant to display the value of the property.
Maven AntRun Plug-in
Maven AntRun plug-in allows us to run ant tasks in Maven. This plug-in was introduced mainly to provide some help in migrating from Ant based projects to Maven.
So we will add the plug-in to the POM file like
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Displaying value of 'myName' property</echo>
<echo>[myName] ${myName}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
So we added a plug-in to the POM file and now we will now run a life cycle phase to test the property of the myName.so now we will execute the validate phase
“mvn validate” – since we are telling the antRun plug-in to execute at validate phase [see phase in execution]