J2EE Enterprise Design Patterns | how to configure web.xml (Or Deployment descriptor) | Java Application using MyEclipse Debugging | Scala Language

Archive for the ‘Deployment descriptor’ Category

Configuring an Application with a Deployment Descriptor

Oh my goodness, I know the power of deployment descriptor or say web.xml. If you don’t know the power of web xml then this blog might help you to explore your knowledge of web xml. I have many times seen that so many java developers don’t know the power of deployment descriptor and also seen that many java developer ask question about most web application on deployment descriptor or most of question solution on just depend on web.xml in web application. As I have explain in my previous blog Enterprise Design pattern I have explain about some design principal and in that I have explain about Deployment descriptor. For any web application web.xml is the heart or also says the look up file for web application. When any web application deploy to server tomcat make look first into web xml and make configuration according to what we have written into web xml. So, we can also say web xml is start up of any web application.

In this blog I have try to explain about web xml and give configuration of web xml. In any web application we have so many commands through which we can control whole application. For that we don’t have to need person who have knowledge of java, non technical person also change in web xml and change the whole behavior of web application without changing and recompiling java class file.

But one main thing always wex.xml file under web application WEB-INF directory at tomcat deployment environment. Like if we take example “ExampleWebXml” then our web xml in,

Tomcat   –>  webapps   –>   “ExampleWebXml”  –>  WEB-INF  –>  And your web.xml file.

Now, we start with basic configuration of any application web xml file.

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app id=”WebApp_9″ version=”2.4″
xmlns=”http://java.sun.com/xml/ns/j2ee&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”&gt;

<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>

</web-app>

That is basic configuration of any web application. When we are not creating any servlet and any model, view and controller. First I give the idea about mapping of servlet into xml.

In deployment descriptor Url mapped with two tag.

1) <servlet> tag.

2) <servlet-mapping> tag.

Url :- http://localhost:8080/ExampleWebXml/spark

<servlet>
<servlet-name>SparkLines</servlet-name>
<servlet-class>com.service.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SparkLines</servlet-name>
<url-pattern>/spark</url-pattern>
</servlet-mapping>

When user hit request with name /spark in browser tomcat look into <servlet-mapping> tag and get servlet reference name with this reference get the servlet class for process this request hit by user in format of /spark. Using this configure all the servlet classes configure for particular request and assign particular servlet for particular request.

<servlet-class>com.service.Controller</servlet-class> tag contains fully qualified name of the servlet class.

<url-pattern>/spark</url-pattern> this is how we want the client to refer to the servlet. And one more thing don’t forget to start with a slash “/”.

OR

<servlet>
<servlet-name>SparkLines</servlet-name>
<jsp-file>/TestController.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>SparkLines</servlet-name>
<url-pattern>/jsp/TestCont.jsp</url-pattern>
</servlet-mapping>

You can also give Jsp instead of servlet. Because we all know after compiling jsp it converts into servlet so, our <jsp-file> tag says, ” apply everything in this <servlet> tag to the servlet created from this JSP page….”

When you define a servlet for a JSP, you must also define a servlet mapping to the JSP page.

If you want to configure any init parameter with any servlet then you can put <init-parm> tag.

<servlet>
<servlet-name>SparkLines</servlet-name>
<servlet-class>com.service.Controller</servlet-class>
<init-param>
<param-name>email</param-name>
<param-value>nishan5656@yahoo.co.in</ param-value>
</init-param>
</servlet>

Using this you can set init parameter for the servlet Controller and you can get using

getServletConfig().getInitParameter(“email”);

We can also declare more than one init-param for the servlet. But this is what we talk about only for the Controller servlet so, what if we want to use all servlet in our application. Then <context-param> tag use for this purpose.

<context-param>
<param-name>email</param-name>
<param-value>nishan5656@yahoo.co.in</param-value>
</context-param>

And to get context servlet context use,

getServletContext().getInitParameter(“email”);

Configuring Listener into deployment descriptor.

But, when you are configure init parameter at deployment descriptor no matter it is servlet init parameter or context init parameter, it is always be a String value. So, how to initialize parameter which is other than string or say how to convert your string init parameter to object before call any servlet. So, what if you want to an application init parameter that’s a database DataSource ??

We have to convert our string parameter into an actual data source reference that all parts of the web application can share. We can’t really put that code in a servlet, because which servlet you put your data source code no guarantee that that code execute first and give data source object to that servlet for use in that servlet.

So, you need method like main() which execute first when any servlet or jsp called. We can make a separate class, not a servlet or JSP, that can listen for the web application for the two key event in ServletContext’s life creation and destruction. That separate class implements ServletContextListener. That is why for initialize listener class before any servlet call and set servlet context init parameter with object we have to configure at deployment descriptor.

<listener>
<listener-class>com.example.MyServletContextListner</listener-class>
<listener>

Configuring Session time Out at wex.xml

Almost web application all over the world user always maintain trough maintaining the user session at application. When user log-in into the application developer registered user session and identified user log-in into the application and according to generate the response to user. But that is good part of application, if you want to make your application well maintain you have to consider all scenario. Consider the situation once user log in after finished work user not click on Log-out link or say direct close browser windows in that case user session always be in application. So, how to make session invalid in that case. Don’t worry web xml have solution for that. Use session timeout tag .

<session-config>
<session-timeout>25</session-timeout>
</session-config>

But, one thing keep in mind that figure 25 written between <session-timeout> tag that argument is in minute. So, the “25” is in minutes this says if the client doesn’t make any request on this session for 25 minutes, kill it.

Restrict developer use of scripting into JSPs using Web.xml

As we know if we want to make our application code which is follows the standard coding standards or make development easily maintainable in future at the time of upgrading functionality then we have to make standard code or say JSPs without scriplets.

It’s important, you can make it invalid for a JSP to have scripting elements (Scriptlets, Java Expressions, or declarations) b putting a

<scripting-invalid> tag in the Deployment Descriptor.

<web-app ……  >

…………

<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</jsp-pattern>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
…………

</web-app ……  >

This disable scripting element for all JSPs in the application because we use the wildcard *.jsp as the URL patterns.

Before we can also use

<%@ page isScriptingEnabled=”false”  %>

This doesn’t work now because isScriptingEnabled attribute is no longer in JSP specification. The only way to invalidate scripting now is through the <scripting-invalid> DD tag.

Like scripting disable using DD we can also disable EL (Expression language) using DD. But we know that EL is good thing that’s going to save the world as we know.

<web-app ……  >

…………

<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</jsp-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
</jsp-config>
…………

</web-app ……  >

Another way to disable using isELIgnored page directive attribute.

<%@ page isELIgnored=”true”  %>

Configuring error pages in the DD

You can declare error pages in the DD based on either <exception-type> or the HTTP status <error-code> number. It is better showing error page rather that error. So always makes error page configuration with DD which makes application good.

Declaring a catch all error page.

This environment applies to every exception find at your application.

<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/jsp/errorPage.jsp</location>
</error-page>

Declaring an error page for a more explicit exception.

This configuration an error page that’s called only when there’s an ArithmeticException. If you have both this declaration and the catch all above, any exception other than ArithmeticException will still end up the “errorPage.jsp”.

<error-page>
<exception-type>java.lang. ArithmeticException</exception-type>
<location>/jsp/arithmeticErrorPage.jsp</location>
</error-page>

Declaring an error page based on an HTTP status code.

This configuration an error page that’s called only when status code for the response is “404” page not found.

<error-page>
<error-code>java.lang. ArithmeticException</error-code>
<location>/jsp/pageNotFoung.jsp</location>
</error-page>

Configuring welcome files in the DD

You already know that if you type in the name of web site and don’t specify a special file, your application still show you something, and that is your welcome file.

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

Configuring <mime-mapping> tag in DD.

You can configure mapping between an extension and a mime type in DD.

<mime-mapping>
<extension>mpg</extension>
<mime-type>video/mpeg</mime-type>
</mime-mapping>

As at the starting of that blog post I have already told that Deployment descriptor is most powerful part of any web application. Still there are so many thing we can configuring using DD like we can configuring custom tag support to application, web application security, ejb configuration, filter and many more.

So, always keep focus at the time of development of your application. If you make most declaration into DD then you can control application without compiling your java code and get change behavior of application.

Thanks,
Nishan Patel
SCJP 1.5, SCWCD 1.5
Java Developer
Enterprise Design Patterns
Configuring an Application with a Deployment Descriptor


Advertisements