首页 > 其他分享 >Web Service Tip: JSPs Calling Web Services

Web Service Tip: JSPs Calling Web Services

时间:2023-09-04 17:02:09浏览次数:46  
标签:Web Service service Tip stub JSP more like



Web Service Tip: JSPs Calling Web Services

How do I call a Web service from a JSP?  Seems like a simple question but it turns out there are many ways - some quick and dirty and others more correct.  Let's not debate correctness (do you really want to do this?!) and first solve the problem.

Imagine, if you will, I have generated a stub from the wsdl of  the greeting Web service built a few days ago (again, I refer you to the OTN tutorial step 16 for more details on generating the stub).  

If I add a main method to that stub, I can easily test that stub against my Web service:

public static void main(String[] args)  {
    try {
      GreetingServiceStub stub = new GreetingServiceStub();
      System.out.println(stub.sayHello("Mike"));
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

That bit of code, will become the foundation of how to do my first cut of calling of a Web service from a JSP. The JSP page I want to hook it into looks like this:

First I would like to make a direct call from my JSP.  All I have to do is add a scriplet based on that above code: 

<%@ page cnotallow="text/html;charset=windows-1252" import="com.rpc.GreetingServiceStub" %>
<html>
<head>
<meta http-equiv="Content-Type" cnotallow="text/html; charset=windows-1252">
<title>
Web Service JSP Client - Scriptlet
</title>
</head>
<body>
<%
String parm1 = request.getParameter("parm1");
String result = "";
if (parm1 == null)
   parm1 = " ";
%>
<br>
<form actinotallow="Greeting.jsp" method="get">
  <table border="0">
    <tr> 
      <td>
        Greeting:</b>
      </td>
      <td> 
        <input type="text" name="parm1" size="14" value="<%=parm1 %>">
      </td>
      <td> 
        <input type="Submit" value="Submit"/>
      </td>
      </tr>
  </table>
</form>
<p>
<br>
<%
try {
GreetingServiceStub stub = new GreetingServiceStub();
result = stub.sayHello(parm1);
}
catch (Exception e) {
result = "Web service unreachable tonight :-(";
}
%>The response returned is: <%=result %></body>
</html>

Note the import at the top of com.rpc.GreetingServiceStub which is the stub itself.  Not particularly elegant, but it gets the job done.

Abstracting it a little further, I could investigate Oracle's Web service tag library which takes away a bit of the code and makes it a little more declarative.  The extra work required here is of course reading the WSDL to get the binding, portType and the like.  See below for the same example using the tag library:

<%@ taglib uri="http://xmlns.oracle.com/j2ee/jsp/tld/ojsp/wstaglib.tld" prefix="wstag"%><%@ page cnotallow="text/html;charset=windows-1252"%>
<html>
<head>
<meta http-equiv="Content-Type" cnotallow="text/html; charset=windows-1252">
<title>
Web Service JSP Client - Tag Library
</title>
</head>
<body>
<%
String parm1 = request.getParameter("parm1");
if (parm1 == null)
   parm1 = " ";
%>
<br>
<form actinotallow="GreetingTagLib.jsp" method="get">
  <table border="0">
    <tr> 
      <td>
        Greeting:</b>
      </td>
      <td> 
        <input type="text" name="parm1" size="14" value="<%=parm1 %>">
      </td>
      <td> 
        <input type="Submit" value="Submit"/>
      </td>
      </tr>
  </table>
</form>
<p><wstag:webservice id="greeting" 
  wsdlUrl="http://127.0.0.1:8888/ws/GreetingService?WSDL" 
  scope="page" 
  binding="GreetingBinding" 
  soapLocatinotallow="http://127.0.0.1:8888/ws/GreetingService"/>
<wstag:invoke id="resp" webservice="greeting" operatinotallow="sayHello">
 <wstag:part name="p" value="<%=parm1 %>" />
</wstag:invoke>
<br>
The response returned is:
<%=pageContext.findAttribute("resp") %></body>
</html>

If you look again at the WSDL, you will see all the settings that were passed to the tags - e.g. GreetingBinding from the binding section and operation name from the portType section.  If you plan on trying this out yourself, you are probably wondering where this Web services tag library jar and tld is located.  If you look in your OracleAS 9.0.4 distribution, you will find the ojsputil.jar located in the <Oracle_Home>j2eehomejsplibtaglib - inside is the TLD.

Most likely in a larger application, for example using a more typical MVC framework like Struts, the call to from stub would be more likely be in an Apache Struts action or delegated to from a Struts action, rather than in the JSP page itself.  However, for the folks doing quick and dirties out there, these two routes can be a quick way to hook your Web service to a JSP.

As a by the by, if you are an Oracle Portal user you may want to investigate the OmniPortlet, which makes this whole exercise a declarative effort rather than a programmatic one.

标签:Web,Service,service,Tip,stub,JSP,more,like
From: https://blog.51cto.com/u_16245757/7352669

相关文章

  • DreamWeaver+WebDav(IIS)配置团队协作开发
    作者:fbysssbasicauthentication因为如果是远程,肯定不能使用windows集成。这时的用户,应该是服务器上自行建立分配的用户(控制面板->用户).  可以通过目录的"安全"来指定每个用户的访问权限. 在Dreamweaver中新建一个站点.设置站点名称/本地根文件夹;远程信息->访问,选WebDav,然......
  • Spring事务配置笔记(实现不同Service间调用事务)
    作者:fbysss关键字:Spring,事务处理一、关键配置示例:<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><propertyname="dataSource&qu......
  • Xml2Bean:为Webservice返回Collection准备的一个实用类
    作者:fbysss关键字:Webservice,Xml2Bean背景:Webservice 返回的是xml,如何得到Collection类型呢?最简单的方式就是把xml还原成Collection。准备包:commons-digester-1.8.jar源代码1:Xml2Beanpackagecn.edu.ustb.db.task.util;/***//***<p>标题:Xml2Bean.java</p>......
  • 软件测试 | Web自动化测试
    当前绝大多数企业应用系统都是基于Web的应用系统,人们可以通过Internet浏览器便捷地访问它们。在可以预见地将来,“云计算”会进一步推动这种趋势。当前很多组织和公司,采用持续改进的开发模式来应对这种趋势。在持续改进开发模式中(例如敏捷和极限开发模式),需要不断地进行迭代测试。传......
  • Tomcat 项目迁移至weblogic 10笔记
    作者:fbysss关键字:TomcatWeblogicOS:WindowsXPWebAppServer:Tomat5.5WebLogic server10(中文版)IDE:Beaworkshopforweblogicplatform。一.IDE注意事项创建动态web项目,选择是否共享j2ee库,如果共享,则需要域的共享库里面包含项目所需的库,比如beeHivenetUi;不共享,则将所需的库......
  • 使用Flask和Vue.js构建现代Web应用
    博客主题:使用Flask和Vue.js构建现代Web应用概述在本篇博客中,我们将介绍如何使用Flask和Vue.js这两个流行的框架来构建一个现代化的Web应用。Flask是一个轻量级的PythonWeb框架,而Vue.js是一个灵活且易于使用的JavaScript框架,用于构建交互式的用户界面。我们将通过一个简单的示......
  • 致远OA webmail.do 任意文件下载 CNVD-2020-62422
    漏洞描述致远OA存在任意文件下载漏洞,攻击者可利用该漏洞下载任意文件,获取敏感信息影响版本致远OAA6-V5致远OAA8-V5致远OAG6漏洞复现fofa语法:app="致远互联-OA"登录页面如下:致远OAwebmail.do文件读取漏洞,由于/seeyon/webmail.do页面filePath参数过滤不严,导致可以......
  • BUUCTF [SWPU2019]Web1
    进入网站,注册登录,进到申请发布广告,应该就是在这里实现注入。首先尝试:1'or1=1#标题含有敏感词汇应该是哪里被过滤了。经过尝试后是or被过滤了,--+,#等其他的注释符也被过滤了。经过测试后,结尾可以用单引号闭合。再次尝试:1'showdatabases()'1'showdatabases()'空格被......
  • KVM管理工具Webvirtmgr 问题记录
    1.如果遇到虚拟机启动时报错:qemu-kvm:FailedtostartVNCserveron`172.17.42.1:0':Failedtobindsocket:Cannotassignrequestedaddres请将对应虚拟机XML配置中VNC部分改为:<graphicstype='vnc'port='-1'autoport='yes'listen='0.0.0.......
  • BUUCTF [CISCN2019 华东南赛区]Web11
    切入点如图:测试模板注入最后或者payload:X-Forwarded-For:{ifreadfile('/flag')}{/if}原理是Smarty已经废弃{php}标签。在Smarty3.1,{php}仅在SmartyBC中可用。Smarty的{if}条件判断和PHP的if非常相似,只是增加了一些特性。每个{if}必须有一个配对的{/if}。全部的PHP条件表......