Mahol Dot Org

Icon

Weblog For All Your Need

A Hello,World Servlet Servlet Tutorial Servlet Examples Run servlet on tomcat

Basic HelloWorld Program for Servlet

Servlet DefinitionServlet is a java code which runs at the servlet side and gives output in the form of html page.Its life cycle, init(),service() and destroy() is maintained by the container.

Here we will discuss how to run a simple servlet code. We will illustrate a simple HelloWorld.java for servlet .Lets us look at the code below so that we can correlate the concept.

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType(“text/html;charset=UTF-8″);

PrintWriter out = response.getWriter();

out.println(“<html>”);

out.println(“<head>”);

out.println(“<title>Servlet HelloWorld</title>”);

out.println(“</head>”);

out.println(“<body>”);

out.println(“<h1>Servlet HelloWorld at ” + request.getContextPath () + “</h1>”);

out.println(“</body>”);

out.println(“</html>”);

}

Here the methods doGet() and doPost() are the methods which get called by the service() method depending upon the fact that whether the request is post or get.How to call this servlet depends upon the entry in the web.xml file where the url-pattern tag indicates the url through which the servlet can be called.The imports above must be noted as the container will import the servlet APIs HttpServlet is the class from which our servlet class must inherit so as to process http requests.Let us look at he web.xml file now :

<?xml version=”1.0″ encoding=”UTF-8″?>

<web-app 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”>

<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>

This is the web.xml file with HelloWorld.java as the only servlet.the servlet-class tag contains the fully qualified name of the java class file.Before that we need to compile the servlet code and put the class file into WEB-INF/classes folder of our tomcat server.The entry in the servlet-name tag represents the alias name for the servlet,meaning in the rest of the portion of web.xml file the servlet will be recoganized by this alias name.url-pattern tag denotes the url through which the servlet can be accessed from browser.Now to run the servlet type the following url in your browser :

http://localhost:portnumber/projectname/HelloWorld

Number of View :1169

Related posts:

  1. Basics of tomcat Apache-Tomcat Tutorial Download and Run Tomcat
  2. Tomcat Directory Tree structure Tomcat Fundamentals Tomcat Administration
  3. Acegi Security Grails Acegi Plugin Acegi Tutorial
  4. Introduction to Grails plugins Grails Plugins Tutorials Understanding Grails Plugins
  5. Groovy Grails Quick Start Groovy Getting Started Grails Define Groovy
Receive Updates By Email

Enter your email address:

Delivered by FeedBurner

Category: Java

Tagged:

One Response

  1. asitkr says:
    Hello World Servlet Code

Leave a Reply

Lost !! Use Advanced Search

Categories

Content Protected Using Blog Protector By: PcDrome.

Mahol Dot Org is Digg proof thanks to caching by WP Super Cache