Pages

Monday, July 19, 2010

Deploying a web application in JBoss AS 5 [JBoss EWP]

Share it Please

In this article, we will be creating a sample web application which contains a sample servlet and deploy the application in JBoss AS 5[EWP]. We will access a data source from the jndi and access a table and display the result.

As we are aware of the web application, the structure is similar in this too like
  

                    

The important thing that we need to consider is new descriptor files that are provided with the JBoss server. We have 3 deployment descriptor files with the JBoss

Web.xml, context.xml and jboss-web.xml.

As we are aware of the web.xml file, we will have a look at the context and JBoss-web files.

Context.xml file is used when we need to place some configuration that is accessible in all web applications.We will be defining a context.xml file and place that in the META-INF directory of a web application so that configuration defined in the context.xml file is accessible to that particular web application.

JBoss web server is built on Tomcat server and the context.xml file in TOMCAT_HOME/conf/context.xml is overridden by jboss-web.xml in JBoss. So we can define configuration in jboss-web.xml which we can define in context.xml in Tomcat. But we need to use the context.xml file when we need to define the valves.

jboss-web.xml is sufficient for all web application,if we need to specify a jboss releated information in your deployment descriptor ,jboss-web.xml is used.




The available elements that we can use in jboss-web.xml file are

class-loading: helps in changing the class-loader behavior for a web application [ in my next article ]
servlet: define a servlet
context-root: helps in defining a context for the web application
Security-domain: specifies which security domain is to be used by the web application for authentication and authorization.
virtual-host: defines a virtual host
resource-env-ref:helps in allowing servlet & jsp to use Jndi to find any administrative objectes [ like datasources e.t.c ]
resource-ref:helps in setting up a Jndi factory for objects to be used by servlets and jsp.

There are other few elements like ejb-ref,ejb-env-ref and use-session-cookies e.t.c.

Now for our web application we will define the web.xml ,

<servlet>
    <description></description>
    <display-name>SampleServlet</display-name>
    <servlet-name>SampleServlet</servlet-name>
    <servlet-class>com.JbossAsSampleApp.SampleServlet</servlet-class>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>SampleServlet</servlet-name>
    <url-pattern>/SampleServlet</url-pattern>
  </servlet-mapping>
 
    <resource-ref>
     <description>Data Source For MSAcess DataBase</description>   
     <res-ref-name>jdbc/StudentDB</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
    </resource-ref>

We have defines the servlet and a resource-ref to a data source object which we will get by using Jndi.for configuring a data source here [we will use the same one configured here]
We defined a resource, the reference name, and resource type and resource authorization.

Now we will see the contents of the jboss-web.xml file ,

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

We will be defining the resource name, type and name that is bound in the JBoss jndi. We will use this name to access the object in our application.

Now the servlet looks like,

public class SampleServlet extends HttpServlet {
       private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SampleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    static {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch(ClassNotFoundException e) {
            System.err.println("JdbcOdbc Bridge Driver not found!");
        }
    }
      
       /**
        * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
        */
       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                          
              try {
                     System.out.println("in Get Method");
                    
                     Context initCtx = new InitialContext();
                     DataSource bean = (DataSource) initCtx.lookup("java:/StudentDB");
                    
                     Statement st=bean.getConnection().createStatement();
                     ResultSet myresult=st.executeQuery("select * from Details");
                    
                     while(myresult.next()){
                           response.getWriter().println(myresult.getString(2).toString());
                     }
                    
                     }catch(Exception e){
                           e.printStackTrace();
                     }
             
       }

       /**
        * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
        */
       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              // TODO Auto-generated method stub
       }

      
       /** 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;
    }
   
}

Create a war file for this web application and copy that to the deploy directory of the server instance your stated. 

The application can be downloaded from here


More articles to come , So Happy Coding....

No comments :

Post a Comment