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
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
HelloWorldServlet HelloWorld com.servlet.HelloWorldServlet HelloWorld /helloworld
JSP Page
Hello world in Servlet + JSP Hurray!! This Servlet worked!!!
Demo
Deploy the application tomcat server and enter the URL to access HelloWorldServlet “http://localhost:8089/HelloWorldServlet/helloworld”
Great! Keep up the good work!