Monday, November 8, 2010

Step 2 : First Servlet and it's basic flow

Before the servlet architecture flow; let's analyse on the Tomcat architecture flow in the figure below:

The request is in the form of urls, specified from the client-side.


Tomcat Configuration in web.xml:When tomcat starts, it first reads the conf/web.xml file, which is the heart of the web application. This file is also known as web application descriptor file.

We need to modify the web.xml for our context. We can use the $CATALINA_HOME/conf/web.xml if we want all of our contexts to share the same configuration, but because every web project is different, use: $CATALINA_HOME/webapps/app1/WEB-INF/web.xml for app1 example.

1. Turn on the automatic reload, so if i change a JSP i don't have to restart the tomcat server every time but the server will detect this change automatically and compile properly. Turn it off when the site goes live, to improve efficiency. To make this change open $CATALINA_HOME/conf/web.xml and near the end, before it should say:

(DefaultContext reloadable = "true")

The request url and the related servlet or file mentioned in the client-side request needs to be specified in this descriptor file. for this purpose, 2 tags needs to be coded in the xml file as below.
note: "<" and ">" will be replaced with ( and )

(!-- mapping for the default servlet--)
(servlet-mapping)
(servlet-name)default(/servlet-name)
(url-pattern) / (/url-pattern)
(/servlet-mapping)

(-- mapping for the invoker servlet--)
(servlet-mapping)
(servlet-name)invoker(/servlet-name)
(url-pattern) /servlet/* (/url-pattern)
(/servlet-mapping)

The above configuration allows you to invoke any servlet that is in the "classes" folder of your web application. But let's say you want to invoke only certain servlets and restrict access to the others. Then you would need to avoid using the above method and declare only the servlet that will be accessible from the web.

Let's say we only want the servelt helloworld to be accessible, then in the web.xml file
in ( $CATALINA_HOME/webapps/app1/WEB-INF/web.xml ) do the following:

A) add to web.xml first this servlet declaration statements:

(-- mapping for the default servlet from app1--)
(servlet)
(servlet-name)helloworld(/servlet-name)
(servlet-class)helloworld(/servlet-class)
(load-on-startup)5 (/load-on-startup)
(/servlet)

B) then add a mapping for this servlet like this, in the same web.xml:

(-- mapping for the helloworldservlet--)
(servlet-mapping)
(servlet-name)helloworld(/servlet-name)
(url-pattern) /helloworld (/url-pattern)
(/servlet-mapping)

At this point you will be able to access your servlet helloworld as follows:
http://localhost:8080/app1/helloworld

code for heloworld.java servlet

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

public class helloworld extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("Hello World from GET method ");
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("Hello World from POST method ");

}
}

How does the request flow go?
1) The user requests through URL, say, request URL contains, /servlet/my_servlet?param=value.
2) At the receipt of this request, the server(tomcat) will match this URL, with the list of (servlet-mapping) tags from the web.xml.
3) At a found match, the Mapping will be traced to the corresponding (servlet-name) and the respective class is triggered from within the web.xml file. The web. xml file is located at the conf folder of the server.

The basic Servlet lifecycle:(references: http://www.roseindia.net/servlets/introductiontojavaservlet.shtml )

> Java servlets are server side programs . They are applications that run on servers that comply HTTP protocol.
>The javax.servlet and javax.servlet.http packages provide the necessary interfaces/classes for servlets.
>Servlets: extend the HttpServlet class ; override the doGet /doPost methods. Other methods such as init, service and destroy also called as life cycle methods might be used .





Further reading :



PS: If you are plannign for developin Servlets in Eclipse, Then update the Eclipse plugins for J2EE development by installing and updating eclipse through 'Help' menu. Once done,


Once updated eclipse, You need to create a 'dynamic web project using eclipse project wizards.


Now create your first servlet in the (src) folder. However, this will throw errors in your source code due to the lack of javax.servlet libraries. Hence import this library form the 'plugins' folder of eclipse using the 'Buildpath' context menu of the project. This shoudl resolve the errors.