Servlet Jsp Hello World Example

 Posted On  | Yashwant Chavan 

In this article we are going to cover how to write Hello world Servlet Jsp web application. Define servlet mapping in Deployment descriptor, Forward request to the Jsp Page.

Define HelloWorld.java servlet

Helloworld servlet sometimes called controller extends "javax.servlet.http.HttpServlet". Here we override the doGet() method. Which handles your request.

package com.technicalkeeda;

import java.io.IOException;

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

public class HelloWorld extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher rd = request.getRequestDispatcher("hello.jsp");
		rd.forward(request, response);
	}

}

Deployment Descriptor (web.xml)

There is only one deployment descriptor for the web application. web.xml contains the configuration details of your application. Below helloworld servlet configuration is defined. When request comes to server it pick up the respected servlet and hadover the request to the controller. In above code we forward to hello.jsp file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>SampleProject</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<servlet>
		<servlet-name>HelloWorld</servlet-name>
		<servlet-class>com.technicalkeeda.HelloWorld</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>HelloWorld</servlet-name>
		<url-pattern>/hello</url-pattern>
	</servlet-mapping>
</web-app>

view Page(hello.jsp)

Nothing special about below jsp code, it looks like sample html code.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Sample Hello World Example</title>
	</head>
<body>
	<h1>Hello World</h1>
</body>
</html>

Finish

Deploy the application tomcat server and enter the URL to access helloworld servet "http://localhost:8080/SampleProject/hello"

Servlet Jsp Hello world example

Tags Jsp  Servlet  Hello World 


HTML Comment Box is loading comments...

Hi I am Yashwant Chavan founder of www.technicalkeeda.com, Purpose of this website to share the programming knowledge in the form post , blogs and articles.

Latest Updates

Stay updated with latest post, articles and technical discussions.

Most Popular Articles



© technicalkeeda.com 2014