Dubbo学习-1 请求jsp

jd对外提供服务:https://wx.jdcloud.com

https://wx.jdcloud.com/market/datas/5/10665  对应短信接口,里面有文档的格式

项目中实现天气服务--实践:

1.dubbo单一应用-发起请求jsp

1.1>步骤:

新建web应用->

新建index.jsp->

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>dubbo获取天气数据</title>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("button").click(function(){
$.ajax({
url:"queryWearth",
data:{
cityCode:"101010100"
},
type:"post",
success:function(data){
alert(data)
}
})
})
})

</script>
</head>
<body>
index.jsp发送ajax请求
<button>获取城市的天气</button>
</body>
</html>

将min.js导入webContent下新创建的js文件夹中->

编写页面内容如下->

编写服务端->

beans:

package com.weather.beans;

public class Weather {
private String name;
private int temp;
private String wet;
private String desc;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getTemp() {
return temp;
}

public void setTemp(int temp) {
this.temp = temp;
}

public String getWet() {
return wet;
}

public void setWet(String wet) {
this.wet = wet;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}

@Override
public String toString() {
return "Weather{" +
"城市='" + name + '\'' +
", 温度=" + temp +
", 湿度='" + wet + '\'' +
", 描述='" + desc + '\'' +
'}';
}
}

service:

package com.weather.service;

import com.weather.beans.Weather;

public interface WeatherService {
Weather queryWeather(String cityCode);
}

package com.weather.service.impl;

import com.weather.beans.Weather;
import com.weather.service.WeatherService;

public class WeatherServiceImpl implements WeatherService {

@Override
public Weather queryWeather(String cityCode) {
Weather weather = new Weather();
if("101010100".equals(cityCode)){
weather.setName("北京");
weather.setWet("99%");
weather.setTemp(37);
weather.setDesc("天气潮湿");
}else if("101020100".equals(cityCode)){
weather.setName("石家庄");
weather.setWet("80%");
weather.setTemp(39);
weather.setDesc("天气燥热");
}else{
weather.setName("全国");
weather.setWet("70%");
weather.setTemp(33);
weather.setDesc("天气较热");
}
return weather;
}
}

servlet:

 项目中添加servlet-api.jar包,idea中右键项目,Open module settings 添加lib(可以从tomcat的lib中查找对应的jar包)

package com.weather.servlet;


import com.weather.beans.Weather;
import com.weather.service.WeatherService;
import com.weather.service.impl.WeatherServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/queryWearth")
public class WeatherServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取参数cityCode
String cityCode = req.getParameter("cityCode");
//调用业务方法
WeatherService weatherService = new WeatherServiceImpl();
Weather weather = weatherService.queryWeather(cityCode);
resp.setContentType("text/html;charset=utf-8");
PrintWriter pw = resp.getWriter();
pw.print(weather.toString());
pw.flush();
pw.close();
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
tomcat启动项目->
http://localhost:8080/dubbo/ (此处的dubbo是项目部署的名称) 点击按钮alert出来信息


猜你喜欢

转载自www.cnblogs.com/healthinfo/p/9547129.html