Friday, May 22, 2015

SIMPLE SERVLET PROGRAM

Aim:
            To write a simple servlet program using HTTP in java.

Algorithm:

  1. Create a servlet program using http.
  2. Set classpath where servlet-api.jar file resides.
  3. Compile the servlet program using javac programname.java
  4. Place the class file …\Tomcat 5.5\webapps\ROOT\WEB-INF\classes\ folder.
  5. modify the web.xml file using your servletClassName.
  6. Invoke the class file using http://localhost:8080/servetClassName from your browser

 Program:

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

public class mailamHome extends HttpServlet
{
  public void doGet(HttpServletRequest request, HttpServletResponse response)
                                   throws ServletException,IOException
  {
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    pw.println("<html>");
    pw.println("<head><title>Hello World</title></title>");
    pw.println("<body>");
    pw.println("<h1>Mailam College of Engineering</h1>");
    pw.println("<h2>An ISO 9001:2000 Certified Institution</h2>");
    pw.println("<h3>Affliated to Anna University</h3>");
    pw.println("</body></html>");
  }
}


web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--<!DOCTYPE web-app
 PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd"> -->
<web-app>
 <servlet>
  <servlet-name>Mailam</servlet-name>
  <servlet-class>mailamHome</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Mailam</servlet-name>
 <url-pattern>/mailamHome</url-pattern>
 </servlet-mapping>
</web-app>

No comments:

Post a Comment