GTU WTAD Practical 10 : Write a Servlet to display ‘Hello World’ on browser

GTU WTAD Practical 10 : Write a Servlet to display ‘Hello World’ on browser

GTU WTAD Practical 10

This program is very fist program for you to execute Java Servlet. Lets called it HelloWorld using Java Servlet. First of all i would like to recall my previous article that gives complete explanation of the how to setup the environment variable to execute your Java Servlet programs. Once you are ready with environment setup you are ready to execute your first program. Steps to execute your first Servlet Program.

Jump to GTU WTAD Practical 11

Step 1 : Write your your code in HelloWorld.java file as provided.

Step 2 : Compile HelloWorld.java using javac command and save the .class file to  Tomcat_Apache\webapps\gtu_10\WEB-INF\classes

Step 3 : Write down web.xml file as provided content and save it to Tomcat_Apache\webapps\gtu_10\WEB-INF\

Step 4 : Write down index.jsp and save it to root folder

Step 5 : Restart your server and open following url to execute your first Servlet

Step 6 : Drop thanks email to me !

File Name : index.jsp

File Location : Tomcat_Apache\webapps\gtu10\

<%
	response.sendRedirect("./HelloWorld");
%>

 

File Name : HelloWorld.java

File Location : Tomcat_Apache\webapps\gtu10\WEB-INF\classes

/*
		GTU WTAD Practical 10 : Write a Servlet to display 'Hello World' on browser.
*/

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet
{
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html><head><title>GTU WTAD Practical 10 : Write a Servlet to display 'Hello World' on browser.</title></head><body><center><h1>GTU WTAD Practical 10 : Write a Servlet to display \"Hello World\" on browser. </h1></center><br><br>HelloWorld</body></html>");
	}
}

 

File Name : web.xml

File Location : Tomcat_Apache\webapps\gtu10\WEB-INF\

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">  
  
	<display-name>GTU WTAD Practical 10 </display-name>
	<description> Write a Servlet to display "Hello World" on browser. </description>
  
  <servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>HelloWorld</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
      <url-pattern>/HelloWorld</url-pattern>
  </servlet-mapping> 
  
</web-app>

 Output

GTU WTAD Practical 10

 

Leave a Reply