Monday, January 11, 2010

Deploying Servlets & JSPs

Java Servlets & JavaServer Pages (JSPs) form the backbone of Web Application Development in Java. Java Servlet Technology and JSP are a part of the Java Platform, Enterprise Edition (JEE). The latest release of JEE (version 6), which was released a few days ago supports Servlets 3.0 and JSP 2.2. I am not going to emphasize the changes made in these versions here, but a quick Google search should give you an idea on the enhancements made.

A Java Servlet is primarily a class which conforms to the Java Servlet API, a protocol by which a Java class may respond to HTTP requests. A servlet may be used to add dynamic content to a Web Server using the Java platform. The generated content is commonly HTML, but may also be other data such as XML, etc. Similar non-Java technologies are CGI and ASP.NET.

JSP is a server side technology similar to that of Servlets. Like Servlets, it is also used to create dynamically generated web pages with HTML, XML, etc. in response to Web client requests. Architecturally, JSP may be viewed as a high-level abstraction of Java Servlets.

Though Servlets and JSPs are used to achieve the same goal, they were designed for two different profiles - developers and designers respectively. A well designed JSP may serve dynamic content using Java without any actual Java code in the JSP; this is achieved using other technologies of Java EE like Expression Language, Custom Tags, etc. Such a model was built to help Web page designers, who are good at HTML, build JSP pages without any need to understand the Java code involved. The developers can support these designers by building custom tags, developing the servlets in patterns like MVC, etc. If you are new to the world of Web Application Development in Java, this concept may seem a little overwhelming in the beginning, but as time progress you will understand this approach better.

To accept HTTP request and to give the users the required responses, a web/app server is used. A web server primarily accepts request and gives responses along with optional data content like images, etc. An extension to the web server is the app server. An app server can also expose business logic and business processes for third-party applications. Further, app servers have features like transaction management, security, etc. Apache Tomcat is a popular web server with Tomcat as the servlet container and GlassFish is a popular app server. Servlets and JSPs have to be deployed on these servers for the outside world to communicate with them. This post will be covering both these servers.

In this blog-post, I will be covering only the deployment phase of Servlets and JSPs. It is assumed that the reader is comfortable with Java and is well acquitted with the basics of JSPs and Servlets. I won’t be explaining any code in this post as most of it is self-explanatory.

Software needed

Note - Installing Java EE 6 will also install GlassFish v3.

To help the readers get a better understanding of the entire process, I will be developing a simple Hello World Application with a Hello World JSP and a Hello World Servlet here.

Here are the source codes of the JSP and the Servlet -

Compile the servlet using the javax.servlet.jar available in glassfishV3\glassfish\modules directory, which is obtained after installing JEE 6 with the following command -

javac HelloWorld.java -classpath install_path\ glassfishV3\glassfish\modules\javax.servlet.jar

Understanding the directory structure

Every web application has a typical directory structure. The JSPs and Servlets along with the required descriptors have to be placed in this directory structure. I will emphasize this structure using the Hello World example. The structure is as follows -

Any file/folder present in the root of the Web Application (like helloWorld.jsp) here is directly available to the client. Even folders present here (except WEB-INF and META-INF) are available directly to the users.

The root also contains a directory called the WEB-INF. This is a required directory and contains three important sub-folders - classes, lib and tags. Every web application also has a deployment descriptor (web.xml). This is also placed here.

The classes directory contains the .class files. The servlet class files are also placed here (like HelloWorld.class).

The lib directory contains any external JAR files that are used in the web application. In this example we have no files in this directory.

The tags directory contains the TLD along the .tag files. As this is an introductory example, we will keep this directory empty. If there are any custom tags being developed in the web application, they should be placed here.

The deployment descriptor defines the mapping between the servlet name and the servlet class.

Download the entire application here.

Deploying the application in Apache Tomcat

Copy the directory HelloWorldApplication built in the above step to the webapps directory of Tomcat i.e. install_path\Apache Software Foundation\Tomcat 6.0\webapps.

Start Tomcat from install_path\Apache Software Foundation\Tomcat 6.0\bin\tomcat6.exe.

To view the JSP page, type http://localhost:8080/HelloWorldApplication/HelloWorld.jsp in the browser and to view the servlet page type http://localhost:8080/HelloWorldApplication/HelloWorld in the browser.

Shut down tomcat6.exe to stop the server.

Deploying the application in GlassFish V3

Start GlassFish using install_path\glassfishV3\glassfish\bin\startserv.bat.

Open http://localhost:4848/ in the browser.

Login with the admin password and navigate to Applications Task in the left-side panel. Click on deploy and select "Local Packaged Directory" and specify the directory built above.

Select the type as "Web Application" and click on "Deploy".

To view the JSP page, type http://localhost:8080/HelloWorldApplication/HelloWorld.jsp in the browser and to view the servlet page type http://localhost:8080/HelloWorldApplication/HelloWorld in the browser.

Stop GlassFish using install_path\glassfishV3\glassfish\bin\stopserv.bat.

That’s it. We have written a web application and deployed it on Apache Tomcat and GlassFish. Still there is one more catch - what if I have to move the web application from one physical server to another physical server? Do I have to build the directory structure again? Well look for the solution in my next blog-post - Building Java Web Archives (WARs).

13 comments:

Anonymous said...

nice blog, i have a question,
is it true that ASP.NET is slowly replacing javaEE ?

i want to concentrate on a web technology field , javaEE , or ASP.NET , or LAMP .
which one do you recommend for now and the future of web applications

Unknown said...

Hi!

I wouldn't really say ASP.NET is replacing JavaEE, but it is giving a tough fight.

There are several application servers (GlassFish, WebLogic) available for Java when compared to IIS for ASP.NET, giving JavaEE an edge.

All the three languages which you are thinking about - Java, .NET and PHP aren't going to die anytime soon. So perfection is any one will be advantageous in future.

I personally would recommend a specialization in one of the three and at least some basic knowledge in the other two.

I started with Java, followed by ASP.NET.

Anonymous said...

thank you for your advise, i have another question.

what program do you use to develop a complete jsp servelt websites,
i am looking for something like visual web developer for javaEE web development,
visual web developer lets me prototype very fast.
netbeans is good but it lacks the (what you see is what you get) of visual web developer.

do you known of any programs for javaEE ?

Unknown said...

well, I understand you are targeting three programming languages - Java, .NET and PHP.

These languages are for dynamic content and programming stuff, not for markup or visual display. For creating web-pages we still have to rely on HTML.

Applications like Adobe Dreamweaver give a WYSIWYG experience. This markup and be used in association with JSPs, Servlets and ASP.NET pages to combine the power of markup and programming.

Design patterns like MVC (Model-View-Controller) help a lot in achieving such interoperability.

sankalp said...

i am a newbie at java so was tryin out ma class samples on glassfish.but seems m having some prob.
heres d code:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Excelservlet extends HttpServlet {

PrintWriter out;
public void service(HttpServletResponse res,HttpServletRequest req) throws ServletException,IOException
{res.setContentType("text/html");
out=res.getWriter();
out.println("please servlet show urself on d browser");
}}
i compiled it and got a .class file.
heres my web.xml


Excelservlet
Excelservlet


Excelservlet
/run





i put it in WEB-INF.webinf also has the classes folder and lib.classes again contain my .class file.
i made a .war file which contains classes,lib n web.xml at root.

the error aftr deployment is"There is no installed container capable of handling this application com.sun.enterprise.deploy.shared.FileArchive@17f2faa".
whats this.
would like ur help on this.

SANKALP said...

sorry my web.xml didnt paste correctly.
here it is again


"

Excelservlet
Excelservlet


Excelservlet
/run



"

Unknown said...

Hi Sankalp!

Can you upload the code (the servlet and the web.xml) at some place and send me a link of it. I will go through it and come back to you.

madhavi said...

how to deploy a war file in glassfish.do we have to set classpath for glassfish.

Unknown said...

JavaOne 2013 is apparently being held at Hyderabad this year as well (8-9 May) at the same place guys…. They have also started accepting registrations (check this: http://bit.ly/YMPeJ8 )

Java Tutorial said...

Your blog has excellent information On JSP.

java training institute said...

This is one of the awesome and Important post.I like your blog description.

navya said...

I have read your blog its very attractive and impressive. I like it your blog.

.Net Training in Chennai | .Net Online Training | Dot Net Training in Chennai

Dot Net Online Training | LINQ Online Training

Unknown said...

JSP is a backbone of web application of java .its realy help to me becz am new in java so i learnerd and i got idea about your article .thanks for shering this information to us and specialy to me .in this blog i gain .
java training institutes in chennai |
java j2ee training institutes in velachery