Servlet XML Configuration

Step 1: Create a simple class and extend it with httpservlet and create doGet method:

package com.gs.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class XmlServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("Hello from Get method of XML servlet");

	}

}

Step 2 : Create servlet and servlet mapping in web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>SimpleServletProject</display-name>
	
	<servlet>
		<servlet-name>xmlServlet</servlet-name>
		<servlet-class>com.gs.controller.XmlServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>xmlServlet</servlet-name>
		<url-pattern>/xmlservletpath</url-pattern>
	</servlet-mapping>

</web-app>

Step 3: Run the app and hit the URL:

Step 4: Check the console:

Annotation

Xml

1) Less code and easy to write

1) More code , difficult to maintain

2) Need to build code on every change

2) No need to build simply bounce the server on every change

Last updated

Was this helpful?