You are currently browsing the tag archive for the ‘tomcat’ tag.

If source named ExcelExample then directory structure will be like bellow:

servlet directory structure in tomcat

servlet directory structure in tomcat

Need to add the following text in server.xml to run on tomcat:

  <Context path="/ExcelExample"
          docBase="webapps/ExcelExample"
          crossContext="true"
          debug="0"
          reloadable="true"
          trusted="false" >
  </Context>

Source

What are Java Servlets?

Servlets are snippets of Java programs which run inside a Servlet Container. A Servlet Container is much like a Web Server which handles user requests and generates responses. Servlet Container is different from a Web Server because it can not only serve requests for static content like HTML page, GIF images, etc., it can also contain Java Servlets and JSP pages to generate dynamic response. Servlet Container is responsible for loading and maintaining the lifecycle of the a Java Servlet. Servlet Container can be used standalone or more often used in conjunction with a Web server. Example of a Servlet Container is Tomcat and that of Web Server is Apache.

Servlets are actually simple Java classes which must implement the javax.servlet.Servlet interface. This interface contains a total of five methods. Most of the time you don’t need to implement this interface. Why? Because javax.servlet package already provides two classes which implement this interface i.e. GenericServlet and HttpServlet. So all you need to do is to extend one of these classes and override the method(s) you need for your Servlet. GenericServlet is a very simple class which only implements the javax.servlet.Servlet interface and provides only basic functionality. On the other hand, HttpServlet is a more useful class which provides methods to work with the HTTP protocol. So if your Servlet works with HTTP protocol (in most cases this will be the case) then you should extend javax.servlet.http.HttpServlet class to build Servlets and this is what we are going to do in this article.

Servlets once initialized are kept in memory. So every request which the Servlet Container receives, is delegated to the in-memory Java Servlet which then generates the response. This ‘kept in memory’ feature makes Java Servlets, a fast and efficient method of building Web Applications.

Requirements to run this sample application:

  1. Download and install jdk from (http://java.sun.com/javase/downloads/index.jsp) I have used jdk 1.6.0_14 for this example.
  2. Downlad and install Tomcat from (http://tomcat.apache.org/) I have used Apache Tomcat-6.0.20 and my installation directory C:\apache-tomcat-6.0.20

Write and Run Your Java Servlet:

1. Create Your Servlet:

Create a java file named “MyServlet.java” in C:\apache-tomcat-6.0.20\webapps\myapp\WEB-INF\classes\com\myapp\servlet folder.

2. Write code for your servlet

Put the following code in Myservlet.java


package com.myapp.servlet;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {

res.setContentType(“text/html”);
PrintWriter out = res.getWriter();
out.println(“”);
out.println(“MyServlet”);
out.println(“\t“);
out.println(“”);
out.println(“”);
out.println(”

This is my first servlet out put.

“);
out.println(“”);
out.close();
}
}

Explanation:

Our TestServlet is extremely simple. It displays the current date and time on the server to the user.

We overrode just one method of HttpServlet class. doGet() is called when a HTTP GET request is received by the Servlet Container. In this method we set the content type of our response to ‘text/html’ (informing the client browser that it is an HTML document). Then we get hold of PrintWriter object and using its println() method, we send our own HTML content to the client browser. Once we are finished, we call its close() method

  1. Compile your written servlet by writing the following command in command line


cd C:\apache-tomcat-6.0.20\webapps\myapp\WEB-INF\classes\com\myapp\servlet
javac -cp %CATALINA_HOME%\lib\servlet-api.jar MyServlet.java

Here I have loaded servlet-api.jar to classpath, if you did not added CATALINA_HOME as environment variable you can simply write the tomcat lib path here I mean in our case C:\apache-tomcat-6.0.20\lib
If no error it will create a MyServlet.class file after executing this command.

3. Write web.xml for your application:

web.xml file, also known as “Web Deployment Descriptor” allows us to configure our web application inside the Servlet Container. Here we can specify the name of our web application, define our Java Servlets, specify initialization parameters for Servlets, define tag libraries, and a whole lot more. This file is placed inside the /WEB-INF folder.

Add the following text in your web.xml file to run your servlet:

<?xml version="1.0"?>
<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_2_5.xsd"
               version="2.5">  
               <servlet>
                               <servlet-name>MyServlet</servlet-name>
                               <servlet-class>com.myapp.servlet.MyServlet</servlet-class>
               </servlet>
               <servlet-mapping>
                               <servlet-name>MyServlet</servlet-name>
                               <url-pattern>/MyServlet</url-pattern>
               </servlet-mapping>
</web-app>

4. Finally Run your servlet:

Start/ Restart your tomcat and point your browser to http://localhost:8080/myapp/MyServlet if everything ok it will print “This is my first servlet out put.” in browser

Got great help from this source

May 2024
S M T W T F S
 1234
567891011
12131415161718
19202122232425
262728293031  

RSS Hima’s Blog

  • An error has occurred; the feed is probably down. Try again later.