Pages Navigation Menu

Coding is much easier than you think

Servlet + Jsp Hello World Example

 
In our previous tutorial we got introduced to what a JSP is, In this article we are going to cover how to write Hello world example using both Servlet and Jsp.
 

Final Project Structure

 
Hello world using Servlet and  jsp
 

Servlet

 

package com.servlet;

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 HelloWorldServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		RequestDispatcher rd = request.getRequestDispatcher("hello.jsp");
		rd.forward(request, response);
	}
}

 

web.xml

 
Make sure that your web.xml file has following mapping

<web-app ..>
	<display-name>HelloWorldServlet</display-name>
	<servlet>
		<servlet-name>HelloWorld</servlet-name>
		<servlet-class>com.servlet.HelloWorldServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>HelloWorld</servlet-name>
		<url-pattern>/helloworld</url-pattern>
	</servlet-mapping>
</web-app>

 

JSP Page

 

<html>
<head>
<title>Hello world in Servlet + JSP</title>
</head>
<body>
	<p>Hurray!! This Servlet worked!!!</p>
</body>
</html>

 

Demo

Deploy the application tomcat server and enter the URL to access HelloWorldServlet “http://localhost:8089/HelloWorldServlet/helloworld”
 
Run hello world servlet  JSP
 

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.