Pr.Pg Next Pg

Servlets - Examples tutorials

  • A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model.

  • Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes and interfaces such as javax.servlet.http.HttpServlet and javax.servlet.Servlet

  • A simple servlet program which service HTTP request, this program have to extend javax.servlet.http.HttpServlet class.

 

 

Example: Simple Servlet program to printing Hello Servlet

Sam Sir

//Program name NewServlet.java

// Simple program to printing Hello Servlet

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class NewServlet extends HttpServlet {

private String str;

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

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

PrintWriter out = response.getWriter();

try {

str="Hello Servlet";

out.print("<h1>"+str+"<h1>");

} finally {

out.close();

}

}

}

Output

Servlets example

 

Compiling a Servlet

  • Let us put above code if NewServlet.java file and put this file in C:\ WebApplication1 (Windows) or /usr/WebApplication1 (Unix) then you would need to add these directories as well in CLASSPATH.

  • Assuming your environment is setup properly, go in WebApplication1 directory and compile NewServlet.java as follows:

$ javac NewServlet.java

And for windows

C:\WebApplication1>javac NewServlet.java

  • If the servlet depends on any other libraries, you have to include those JAR files on your CLASSPATH as well. I have included only servlet-api.jar JAR file because I'm not using any other library in Hello World program.

  • This command line uses the built-in javac compiler that comes with the Sun Microsystems Java Software Development Kit (JDK). For this command to work properly, you have to include the location of the Java SDK that you are using in the PATH environment variable.

  • If everything goes fine, above compilation would produce NewServlet.class file in the same directory. Next section would explain how a compiled servlet would be deployed in production.

Servlet Deployment

  • By default, a servlet application is located at the path <Tomcat-installation-directory>/webapps/ROOT and the class file would reside in <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes.

  • If you have a fully qualified class name of com.myorg.MyServlet, then this servlet class must be located in WEB-INF/classes/com/myorg/MyServlet.class.

  • For now, let us copy NewServlet.class into <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes and create following entries in web.xml file located in <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/

<web-app>

<servlet>

<servlet-name>NewServlet</servlet-name>

<servlet-class>NewServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>NewServlet</servlet-name>

<url-pattern>/NewServlet</url-pattern>

</servlet-mapping>

</web-app>

 

  • You are almost done, now let us start tomcat server using <Tomcat-installation-directory>\bin\startup.bat (on windows) or <Tomcat-installation-directory>/bin/startup.sh (on Linux/Solaris etc.) and finally type http://localhost:8080/ WebApplication1/NewServlet in browser's address box. If everything goes fine, you would get following result:

 

 

Servlet Example

Pr.Pg border                                             Next Pg