Java servlet hello world dynamic web module 2.5

Java servlet hello world dynamic web module 2.5

Java Servlet hello world

This demo will show you how to create servlet dynamic web application on dynamic web module 2.5 in java.

Please note: We are using dynamic web module 2.5 for this application where servlet mapping will be done in web.xml file.

Below are needed:

Please follow below steps:

  • Please choose dynamic web module 2.5 while creating dynamic web project
  • Create dynamic web project in eclipse name: JavaServlet2 (Please use this link if you are not familiar how to create dynamic project in eclipse: Create Dynamic Web Project Eclipse)

Project structure:

Java servlet hello world dynamic web module 2.5

  • Create servlet class name: TestServlet.java inside com.javahonk package in src folder.
package com.javahonk;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestServlet
 */
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, 
     * HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, 
            HttpServletResponse response) throws ServletException, 
            IOException {
        request.setAttribute("message", "Hello from servlet");
        request.getRequestDispatcher("/servletWelcome.jsp").
            forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, 
     * HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) 
                    throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

 

  • Create index.jsp inside WebContent folder:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>Insert title here</title>
</head>
<body>
<form action="TestServlet">
<h1>Hello world!!!</h1>
<input type="submit" value="Submit Request to servlet">

<c:if test="${not empty message}">
<h1>${message}</h1>
</c:if>
</form>
</body>
</html>

 

  • Create servletWelcome.jsp inside WebContent folder:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>Insert title here</title>
</head>
<body>
<c:if test="${not empty message}">
<h1>${message}</h1>
</c:if>
</body>
</html>

 

  • Copy paste below code on web.xml file. (Please note if you create servlet class in eclipse using create servlet window then you don’t need to copy this file because eclipse will automatically create 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://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>JavaServlet2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>TestServlet</display-name>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>com.javahonk.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/TestServlet</url-pattern>
  </servlet-mapping>
</web-app>

 

  • Include jstl-1.2 jar inside lib folder (Jar is already included in project which you could download in the bottom of page)
  • Final project structure:

servlet8

 

  • Now we are ready to run this project.To run this project right click project –> Click Run As –> Run on Server as below:

Java servlet hello world dynamic web module 2.5

  • You will see below first welcome screen click Submit Request to servlet button to submit request to the servlet and get response back:

Java servlet hello world dynamic web module 2.5

  • Servlet will send response back as below:

Java servlet hello world dynamic web module 2.5

download2 Download Project: JavaServlet2

Java servlet hello world dynamic web module 2.5

Leave a Reply

Your email address will not be published. Required fields are marked *