webservice篇之高级开发(四)

通过ajax与jquery访问webservice服务端数据.这里涉及到域的问题。但这不是重点!

首先我们发布一个接口:

接口类:

package com.azj.service;


import javax.jws.WebService;


@WebService
public interface oderSel {
public String getName(String name);


}


接口实现代码:

package com.azj.service;


import javax.jws.WebService;


@WebService(endpointInterface="com.azj.service.oderSel",serviceName="orderSeiImpl")
public class orderSeiImpl implements oderSel {


@Override
public String getName(String name) {

return "你好:"+name;
}


}


发布:

package com.azj.service;


import javax.xml.ws.Endpoint;


public class Te {


public static void main(String[] args) {
EndpointImpl endimpl=(EndpointImpl)Endpoint.publish("http://localhost:8080/fb", new orderSeiImpl());

endimpl.getInInterceptors().add(new logininterceptor());
System.out.println("发布成功!");


}

}

在游览器上输入访问http://localhost:8080/fb?wsdl 看是否生成xml文档。

访问页面:


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>艾壹系统-AYXT</title>
        <link rel="shortcut icon" href="<%=request.getContextPath()%>/images/pu.ico"/>
        <link type="text/css" rel="stylesheet" media="all" href="<%=request.getContextPath()%>/styles/global.css" />
        <link type="text/css" rel="stylesheet" media="all" href="<%=request.getContextPath()%>/styles/global_color.css" />
        <script src="/ajaxsel/js/jquery-3.0.0.min.js" type="text/javascript"></script>
        <script>
$(function(){
        $("#tj").click(function(){
                //clientServlet一个servlet,这里必须通过servlet来进行访问
        $.post("clientServlet",{"name":$("#name").val()},function(data){
                alert($(data).text());
                  


        });
           });


});
</script>


</head>
<body>
<center>
 username:<input type="text" id="name"/>
 <input type="button" id="tj" value="jquery.click"/>
</center>
</body>
</html>


下面是servlet类:主要的东西

package com.client.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class clientServlet
 */
@WebServlet("/clientServlet")
public class clientServlet extends HttpServlet {
private static final long serialVersionUID = 1L;


   
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String data="<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><ns2:getName xmlns:ns2=\"http://interceptor.service.com/\"><arg0>"+name+"</arg0></ns2:getName></soap:Body></soap:Envelope>";

//webservice参数,这里的参数哪里来的呢,这里为啥qian部分服务端发布的代码里添加

//endimpl.getInInterceptors().add(new logininterceptor())的原因啦,你必须先进行访问一次,你可以通过wsdl文档生成相应的类,进行调用(较笨的一种方法)你也可以通过开发工具里的webservice浏览器进行访问,效果一样的,服务端就会生成一段响应数据的代码:在开发工具的Console下:

/*<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://wether/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
   <q0:send>
     <arg0>rainjm</arg0>
   </q0:send>
 </soapenv:Body>
</soapenv:Envelope>*/

//通过修饰下就是上面所展示的。
URL url=new URL("http://localhost:8080/fb");//webservice请求路径
HttpURLConnection con=(HttpURLConnection) url.openConnection();//HttpsURLConnection进行跨域请求

//con.setRequestMethod("post");//请求方式
con.setDoOutput(true);//是否获取写入服务端数据
con.setDoInput(true);//是否接受服务端数据;
con.setRequestProperty("Content-type", "text-xml;charset=utf-8");//设置写入编码方式

OutputStream out = con.getOutputStream();
out.write(data.getBytes("utf-8"));//写入数据

int code = con.getResponseCode();
if(code==200){//判断是否请求成功
 InputStream in  = con.getInputStream();
 System.out.println(in.available());
 
 response.setContentType("text-xml;charset=utf-8");//响应数据编码
 ServletOutputStream outputStream = response.getOutputStream();//写入数据到ajax里去
 byte[]buff=new byte[1024];
 int len=0;
 while((len=in.read(buff))!=-1){
 outputStream.write(buff,  0, len);
 }
 outputStream.flush();
}

}


}

这下就完成啦。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
课程通过实际项目融入常用开发技术架构,讲授风格独特,提供详细上课日志及答疑,赠送配套的项目架构源码注释详细清晰且表达通俗,均能直接在实际项目中应用,正真的物超所值,价格实惠任务作业:综合运用《C#/.Net企业级系统架构设计实战精讲教程》课程所学知识技能设计一个学生成绩管理系统的架构。要求:1.系统基于MVC的三层架构,各层单独建不同的解决方案文件夹。2.采用Model First开发方式,设计架构时只需要设计学生表(TbStudent)和课程表(TbCourse)。学生表必须有的字段是ID、stuName、age;课程表必须有的字段是ID、courseName、content。3.数据访问层采用Entity Framework或NHibernate来实现,必须封装对上述表的增删改查方法。4.必须依赖接口编程,也就是必须要有数据访问层的接口层、业务逻辑层的接口层等接口层。层层之间必须减少依赖,可以通过简单工厂或抽象工厂。5.至少采用简单工厂、抽象工厂、Spring.Net等技术中的2种来减少层与层之间的依赖等。6.封装出DbSession类,让它拥有所有Dal层实例和SaveChanges方法。7.设计出数据访问层及业务逻辑层主要类的T4模板,以便实体增加时自动生成相应的类。8.表现层要设计相关的控制器和视图来验证设计的系统架构代码的正确性,必须含有验证增删改查的方法。9.开发平台一定要是Visual Studio平台,采用C#开发语言,数据库为SQL Server。10.提交整个系统架构的源文件及生成的数据库文件。(注意: 作业需写在CSDN博客中,请把作业链接贴在评论区,老师会定期逐个批改~~)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ctrl+C+V程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值