KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > wssample > servlets > wsclient > JOnASJaxRpcClientServlet


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JOnASJaxRpcClientServlet.java,v 1.3 2004/05/14 07:57:21 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.wssample.servlets.wsclient;
26
27 import java.io.IOException JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.servlet.ServletConfig JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.http.HttpServlet JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import javax.xml.rpc.ServiceException JavaDoc;
39 import javax.xml.rpc.Stub JavaDoc;
40
41 import org.objectweb.wssample.gen.jaxrpc.JaxRpcEndpointInterface;
42 import org.objectweb.wssample.gen.jaxrpc.JaxRpcEndpointInterfaceService;
43
44 /**
45  * @author Guillaume Sauthier
46  */

47 public class JOnASJaxRpcClientServlet extends HttpServlet JavaDoc {
48
49     /**
50      * Initializes the servlet.
51      * @param config ServletConfig
52      * @throws ServletException thrown by super.init(config);
53      */

54     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
55         super.init(config);
56     }
57
58     /**
59      * Destroys the servlet.
60      */

61     public void destroy() {
62
63     }
64
65     /**
66      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
67      * methods.
68      *
69      * @param request servlet request
70      * @param response servlet response
71      *
72      * @throws ServletException default servlet exception thrown
73      * @throws IOException default servlet exception thrown
74      */

75     protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc,
76             IOException JavaDoc {
77         response.setContentType("text/html");
78         PrintWriter JavaDoc out = response.getWriter();
79
80         out.println("<html>");
81         out.println("<head>");
82         out.println("<title>JOnAS JaxRpc WebService Test Page</title>");
83         out.println("</head>");
84         out.println("<body>");
85
86         try {
87
88             // Get the service instance
89
//---------------------------------------------
90
Context JavaDoc ctx = new InitialContext JavaDoc();
91             JaxRpcEndpointInterfaceService service = (JaxRpcEndpointInterfaceService) ctx
92                     .lookup("java:comp/env/service/jaxrpc");
93             //----------------------------------------------
94

95             // Get the port where execute methods
96
//---------------------------------------------
97
JaxRpcEndpointInterface port = service.getJaxRpcEndpoint1();
98             //---------------------------------------------
99

100             // Execute Methods :
101
String JavaDoc hello = port.sayHello(request.getParameter("name"));
102             int cotes = port.getCotes();
103
104             // Display results :
105
out.println("Working with URL : <i>" + ((Stub JavaDoc) port)._getProperty(Stub.ENDPOINT_ADDRESS_PROPERTY)
106                     + "</i><br/>");
107             out.println("<b>result of sayHello(name) method :</b><i>" + hello + "</i><br/>");
108             out.println("<b>result of getCotes() method :</b><i>" + cotes + "</i><br/>");
109
110         } catch (ServiceException JavaDoc se) {
111             out.println("Something goes wrong when calling the WebService");
112             se.printStackTrace(out);
113         } catch (NamingException JavaDoc ne) {
114             out.println("Exception while retrieving InitialContext");
115             ne.printStackTrace(out);
116         }
117
118         out.println("</body>");
119         out.println("</html>");
120
121         out.close();
122     }
123
124     /**
125      * Handles the HTTP <code>GET</code> method.
126      *
127      * @param request servlet request
128      * @param response servlet response
129      *
130      * @throws ServletException default servlet exception thrown
131      * @throws IOException default servlet exception thrown
132      */

133     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
134         processRequest(request, response);
135     }
136
137     /**
138      * Handles the HTTP <code>POST</code> method.
139      *
140      * @param request servlet request
141      * @param response servlet response
142      *
143      * @throws ServletException default servlet exception thrown
144      * @throws IOException default servlet exception thrown
145      */

146     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc,
147             IOException JavaDoc {
148         processRequest(request, response);
149     }
150
151     /**
152      * @return Returns a short description of the servlet.
153      */

154     public String JavaDoc getServletInfo() {
155         return "Short description";
156     }
157
158 }
Popular Tags