August 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Creating Web Services from J2EE Components

  • September 20, 2004
  • 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
   {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("HelloWorld");
      out.close();
   }
   public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
                     throws IOException, ServletException
   {
      doGet(request,response);
   }
}

HelloWorldServlet.java

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"
                   "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
   <display-name>HelloWorldServlet</display-name>
   <servlet>
      <servlet-name> HelloWorldServlet </servlet-name>
      <servlet-class> HelloWorldServlet </servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name> HelloWorldServlet </servlet-name>
      <url-pattern>/ HelloWorldServlet /*</url-pattern>
   </servlet-mapping>
</web-app>

web.xml

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

<init-param>
   <param-nam>name</param-name>
   <param-value>nandhu</param-value>
</init-param>

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

HelloWorldServlet
   Web-Inf
      Classes
         HelloWorldServlet.class

Web.xml

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>
   WEB-INF
      Classes
      Web.xml
   *.html
   *.jsp

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"%>
<html>
<body>
The current time is <%= new Date().toString() %>
</body>
</html>

sample.jsp





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel