Nov 01

Servlet Tutorial 6: Importance of web.xml and elements in servlet

Number of View: 11

We can configure servlet in two ways :

1)   Using annotations

2)   Deployment descriptor

Deployment descriptor is an xml document named web.xml that is deployed with application and contains configuration settings for application.In this tutorial we are discussing some of the mandatory elements that need to defined in web.xml. Continue reading

Nov 01

Servlets tutorial 5 : Servlet Hello World example in Eclipse with tomcat server

Number of View: 10

1) Java Servlets are efficient and powerful solution given by sun for creating dynamic content for the web.

2) Servlets are the main and central processing unit for web application

3) Java Servlets are designed to respond with dynamic content to client requests.

In this tutorial we will do Servlet HelloWorld example with Eclipse IDE and Tomcat Server.

1)      Open Eclipse IDE workspace and Open File->New.It opens the following wizard .

NewWizard

2) Search for dynamic web project in the above wizard as follows :

NewWizard2

3) Click on the Next button in the above wizard and click on the Next button  which opens the below screen.

DynamicWebProject3

In the above screen give the name of the project in Project name screen as ServletsExamples and in Target runtime select the tomcat server version installed by clicking on the New Runtime button and click on Finish button.

4) It creates the dynamic web project with the following project structure.

ProjectStructure4

5)In the above Project structure Create a new package ->com.servlets under src and create a new class HelloWorldServlet in above created package as shown below:

createclass5

6) After creating project make sure servlet-api.jar is in your build path of your project .If not add servlet-api.jar to your build path as follows :

1) Right click on ServletExamples project and select Properties and then Select Buildpath option.

2) After that click on Libraries tab and click on AddExternal jars and select the servlet-api.jar which will be available in tomcat-server downloads.Whenever you download tomcat server we will get servlet-api.jar with tomcat server under lib folder.

3) If we don’t add servlet-api.jar to build path we get compilation errors.

7) In this step we will write a servlet class HelloWorldServlet by extending HelloWorldServlet.

Servlet interface has different doxxx methods to handle HTTP GET,POST,PUT,DELETE requests.In this example we are overriding doGet method.doGet method has two parameters :

1)      HttpServletRequest and HttpServletResponse are used to capture the request and response of web cient.

2)      Developer has to populate HTTPResponse and send the response to client/browser.

In the above example we overrided doGet method

HelloWorld Servlet extends HttpServlet and override get method and wrote the implementation which sends Hello World to the browser.

8)web.xml

Every servlet application will have deployment descriptor. Web.xml is an xml document which contains mapping information of servlet and configuration settings of servlet.Make sure web.xml is placed in the project class path.

<?xml version=“1.0″ encoding=“UTF-8″?>

 

<web-app>

<servlet>

<servlet-name>HelloWorld</servlet-name>

<servlet-class>com.servlets.HelloWorldServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>HelloWorld</servlet-name>

<url-pattern>/HelloWorld</url-pattern>

</servlet-mapping>

</web-app>

Web.xml starts with root tag web-app

In the above web.xml we are mapping HelloWorldServlet to HelloWorld Servlet name.

In turn the HelloWorld servlet name is mapped to /HelloWorld url pattern.we will see the importance of web.xml and how to use each element in web.xml in next tutorial.

9) Finally right click on HelloWorldServlet class and select Run As ->Run on Server option and run the class to diplay HelloWorld output.

Output

Oct 19

Servlets Tutorial 4 : Explain life cycle of Servlets?

Number of View: 52

Servlets follow three phase life cycle initialization,service and destruction.

1) init() method :

1) init() method : init method is called when the servlet is first created.

2) init() method is called exactly once after the instantiating Servlet.

3) init() method is used to write initialization code such as loading database drivers and initialization values so on.

4) When invoking this method servlet container passes a Servlet-Config.

5) One of the most important and common task that init() method performs is reading server specific initialization parameters.

6) The init method is important  because the servlet container passes a ServletConfig object, which contains the configuration values given in the web.xml file.

Signature : public void init(ServletConfig servletConfig) throws ServletException

init() method can throw servlet exception.When init() method throws Servlet exception servlet container does not executes service() method.

servletException is also thrown when the method does  not return with in the time period.

Continue reading

Oct 19

Servlets Tutorial 2 : Explain Servlet api and its methods?

Number of View: 48

Explain Servlet api and its methods ?

1) All Servlets must  implement javax.servlet.Servlet interface either directly or indirectly.

2) Servlet container will create an instance of Servlet class and uses it to handle incoming requests.

3) Servlet interface defines methods that all servlets must implement.

4) Servlet class defines a contract between Servlet and Servlet container.

Continue reading