September 28, 2020
Hot Topics:

Creating Web Services from J2EE Components

  • By Nandhini Arumugam and Sujata De
  • Send Email »
  • More Articles »

Creating a Web Service from a Servlet

A servlet is a Java program that extends the functionality of a Web server, generating dynamic content and interacting with Web applications using a request-response paradigm. Creating a Web service from a servlet involves deploying the servlet in Weblogic. The example of a HelloWorld servlet is given below.

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.io.*;

public class HelloWorldServlet extends HttpServlet
   public void doGet(HttpServletRequest request,
                     HttpServletResponse response)
                    throws IOException, ServletException
      PrintWriter out = response.getWriter();
   public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
                     throws IOException, ServletException


Copy the HelloWorldServlet.java into HelloWorldServlet directory. Compile the Java file into the HelloWorldServlet/Web-inf/classes directory. Make sure that Servlet.jar is there in the CLASSPATH before compiling. The deployment descriptor needed for Servlet deployment is web.xml. It is given below.

<!DOCTYPE web-app
          PUBLIC "-//Sun Microsystems, Inc.
                   //DTD Web Application 2.3//EN"
      <servlet-name> HelloWorldServlet </servlet-name>
      <servlet-class> HelloWorldServlet </servlet-class>
      <servlet-name> HelloWorldServlet </servlet-name>
      <url-pattern>/ HelloWorldServlet /*</url-pattern>


In case of parameters, you can also set these tags in web.xml:


The directory structure that is needed for deploying a servlet is as follows:



In the Weblogic administration console, select Web Application Modules under Deployment and select Deploy a new Web Application Module. Select the directory HelloWorldServlet and click Target Module. Click Deploy. Wait for few seconds until the success message is displayed. Now the servlet is deployed. It can be tested with the following URL: http://hostname:port/HelloWorldServlet/HelloWorldServlet. "Hello world" will be displayed in the browser.

Creating a Web Service from JSP and HTML

JSP is an extensible Web technology that uses template data, custom elements, scripting languages, and server-side Java objects to return dynamic content to a client. Typically, the template data is HTML or XML elements. As HTML/JSP files are autodeployed in Weblogic, just copy the .html/.jsp file into a directory that is already deployed in Weblogic. Any directory can be deployed under the Web Application Module, provided it is of the following format. For example, see the above-mentioned HelloWorldServlet directory structure.

<Directory to be deployed>

You can directly access the HTML file by using http://hostname:port/<DirectoryName>/filename.html. For example, http://localhost:7001/TestHtmls/HelloWorld.html.

<%@ page import="java.util.Date"%>
The current time is <%= new Date().toString() %>


Page 2 of 3

This article was originally published on September 20, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date