package com.cn.customer;
import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.cn.provider.DemoService;
public class Servlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
//初始化
public void init() throws ServletException {
System.out.println("我是init()方法!用来进行初始化工作");
}
//处理GET请求
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("我是doGet()方法!用来处理GET请求");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<BODY>");
/*
* 通过Spring提供的工具类获取ApplicationContext对象
*/
ServletContext sc = request.getSession().getServletContext();
//获取失败时抛出异常
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
DemoService demoService1 = (DemoService)ac1.getBean("demoService");
String name1 = demoService1.getName("tom", "Edison");
out.println(name1);
out.println("<br>");
//获取失败时返回null
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(sc);
DemoService demoService2 = (DemoService)ac2.getBean("demoService");
String name2 = demoService2.getName("tom", "Edison");
out.println(name2);
out.println("</BODY>");
out.println("</HTML>");
}
//处理POST请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("我是doPost()方法!用来处理POST请求");
doGet(request, response);
}
//销毁实例
public void destroy() {
super.destroy();
System.out.println("我是destroy()方法!用来进行销毁实例的工作");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
一个用来测试dubbo与zookeeper整合的简单实例 本人没有整合框架,用的是自己写的servlet,更利于理解底层 提供者和消费者都可以用main方法启动,也都可以作为web项目用tomcat服务器启动 tomcat服务器启动前需先启动zookeeper注册中心 用tomcat服务器启动后在页面上输入http://localhost:8088/dubbo-customer/index会进行查看
资源推荐
资源详情
资源评论
收起资源包目录
dubbo.zip (60个子文件)
dubbo-provider
.project 1KB
logs
log 134KB
src
main
webapp
WEB-INF
classes
web.xml 709B
index.jsp 834B
META-INF
MANIFEST.MF 39B
resources
applicationContext.xml 1KB
log4j.properties 918B
java
com
cn
provider
DemoService.java 117B
impl
DemoServiceImpl.java 255B
Provider.java 435B
target
m2e-jee
web-resources
META-INF
MANIFEST.MF 104B
maven
dubbo-provider
dubbo-provider
pom.properties 265B
pom.xml 2KB
classes
applicationContext.xml 1KB
com
cn
provider
Provider.class 845B
DemoService.class 196B
impl
DemoServiceImpl.class 758B
log4j.properties 918B
test-classes
WebRoot
WEB-INF
classes
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 450B
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 430B
org.eclipse.wst.validation.prefs 50B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.prefs.xml 161B
org.eclipse.core.resources.prefs 124B
org.eclipse.wst.common.component 692B
.jsdtscope 530B
pom.xml 2KB
.classpath 962B
dubbo-customer
.project 1KB
logs
log 208KB
src
main
webapp
WEB-INF
classes
lib
dubbo-provider.jar 740B
web.xml 970B
index.jsp 807B
META-INF
MANIFEST.MF 39B
resources
applicationContext.xml 1KB
log4j.properties 918B
java
com
cn
customer
Servlet.java 3KB
Customer.java 601B
target
m2e-jee
web-resources
META-INF
MANIFEST.MF 104B
maven
dubbo-customer
dubbo-customer
pom.properties 265B
pom.xml 3KB
classes
applicationContext.xml 1KB
com
cn
customer
Servlet.class 3KB
Customer.class 1KB
log4j.properties 918B
test-classes
WebRoot
WEB-INF
classes
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 450B
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 243B
org.eclipse.wst.validation.prefs 50B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.prefs.xml 161B
org.eclipse.core.resources.prefs 124B
org.eclipse.wst.common.component 692B
.jsdtscope 530B
.classpath.bak 934B
pom.xml 3KB
.classpath 962B
共 60 条
- 1
资源评论
- qq_398658762017-12-21下载完毕,还没完全看完,可以使用
- Java_SuSheng2018-04-20非常好非常好
- qq_198329952017-11-06还没看,等看完了再评论
BESTSK
- 粉丝: 97
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功