1.web.xml

 <servlet><servlet-name>serverlet名</servlet-name><servlet-class>项目类路径</servlet-class></servlet><servlet-mapping><servlet-name><span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">serverlet名</span><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;"></servlet-name></span>
<url-pattern>/请求路径</url-pattern></servlet-mapping>

2.java

package com.pul.sam.area;import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONObject;import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.context.support.WebApplicationContextUtils;public class AreaQuery extends HttpServlet{private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {this.requestManage(req,res);;}protected void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {this.requestManage(req,res);}/*** 处理请求* @throws IOException */private void requestManage(HttpServletRequest req, HttpServletResponse res) throws IOException{//配置编码类型res.setContentType("text/html;charset=UTF-8;pageEncoding=UTF-8");  //控制浏览器的编码行为res.setCharacterEncoding("UTF-8");//目的是用于response.getWriter()输出的字符流的乱码问题,如果是response.getOutputStream()是不需要此种解决方案的;因为这句话的意思是为了将response对象中的数据以UTF-8解码后发向浏览器;res.setHeader("content-type","text/html;charset=UTF-8"); req.setCharacterEncoding("UTF-8");    String type = req.getParameter("type");switch (type) {case "prov":this.queryProv(req, res);break;case "city":this.queryCity(req, res);break;case "area":this.queryArea(req, res);break;default:break;}}/*** 省份查询* @throws IOException */@SuppressWarnings("rawtypes")private void queryProv(HttpServletRequest req, HttpServletResponse res) throws IOException {System.out.println("111111111");//配置编码类型res.setContentType("text/json;charset=UTF-8;pageEncoding=UTF-8");  //控制浏览器的编码行为res.setCharacterEncoding("UTF-8");//目的是用于response.getWriter()输出的字符流的乱码问题,如果是response.getOutputStream()是不需要此种解决方案的;因为这句话的意思是为了将response对象中的数据以UTF-8解码后发向浏览器;res.setHeader("content-type","text/json;charset=UTF-8");  req.setCharacterEncoding("UTF-8");        PrintWriter out = res.getWriter();     // 配置与数据库链接的属性ServletContext sc = req.getSession().getServletContext();ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);SessionFactory sessionFactory=(SessionFactory) ac.getBean("sessionFactory");//创建工厂HibernateTransactionManager htm = new HibernateTransactionManager(sessionFactory);DefaultTransactionDefinition def = new DefaultTransactionDefinition();def.setReadOnly(false);def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);TransactionStatus status = htm.getTransaction(def);HibernateTemplate ht=new HibernateTemplate(sessionFactory);//增删改查的模版     //查询数据库String hql = "select distinct _t1.provCode,_t1.prov from TArea _t1";List list = ht.find(hql);htm.commit(status);  //真正提交到后台数据库Map<String, Object> map = new HashMap<String, Object>();map.put("result", list);JSONObject json = JSONObject.fromObject(map);out.print(json);out.close();}/*** 城市查询* @throws IOException */@SuppressWarnings("rawtypes")private List queryCity(HttpServletRequest req, HttpServletResponse res) throws IOException {//配置编码类型res.setContentType("text/json;charset=UTF-8;pageEncoding=UTF-8");  //控制浏览器的编码行为res.setCharacterEncoding("UTF-8");//目的是用于response.getWriter()输出的字符流的乱码问题,如果是response.getOutputStream()是不需要此种解决方案的;因为这句话的意思是为了将response对象中的数据以UTF-8解码后发向浏览器;res.setHeader("content-type","text/json;charset=UTF-8"); req.setCharacterEncoding("UTF-8");        PrintWriter out = res.getWriter();     // 配置与数据库链接的属性ServletContext sc = req.getSession().getServletContext();ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);SessionFactory sessionFactory=(SessionFactory) ac.getBean("sessionFactory");//创建工厂HibernateTransactionManager htm = new HibernateTransactionManager(sessionFactory);DefaultTransactionDefinition def = new DefaultTransactionDefinition();def.setReadOnly(false);def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);TransactionStatus status = htm.getTransaction(def);HibernateTemplate ht=new HibernateTemplate(sessionFactory);//增删改查的模版     //查询数据库String hql = "select distinct _t1.provCode,_t1.prov from TArea _t1";List list = ht.find(hql);htm.commit(status);  //真正提交到后台数据库Map<String, Object> map = new HashMap<String, Object>();map.put("result", list);JSONObject json = JSONObject.fromObject(map);out.print(json);out.close();      return list;}/*** 区查询* @throws IOException */@SuppressWarnings("rawtypes")private List queryArea(HttpServletRequest req, HttpServletResponse res) throws IOException {System.out.println("111111111");//配置编码类型res.setContentType("text/json;charset=UTF-8;pageEncoding=UTF-8");  //控制浏览器的编码行为res.setCharacterEncoding("UTF-8");//目的是用于response.getWriter()输出的字符流的乱码问题,如果是response.getOutputStream()是不需要此种解决方案的;因为这句话的意思是为了将response对象中的数据以UTF-8解码后发向浏览器;res.setHeader("content-type","text/json;charset=UTF-8");  req.setCharacterEncoding("UTF-8");        PrintWriter out = res.getWriter();     // 配置与数据库链接的属性ServletContext sc = req.getSession().getServletContext();ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);SessionFactory sessionFactory=(SessionFactory) ac.getBean("sessionFactory");//创建工厂HibernateTransactionManager htm = new HibernateTransactionManager(sessionFactory);DefaultTransactionDefinition def = new DefaultTransactionDefinition();def.setReadOnly(false);def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);TransactionStatus status = htm.getTransaction(def);HibernateTemplate ht=new HibernateTemplate(sessionFactory);//增删改查的模版     //查询数据库String hql = "select distinct _t1.provCode,_t1.prov from TArea _t1";List list = ht.find(hql);htm.commit(status);  //真正提交到后台数据库Map<String, Object> map = new HashMap<String, Object>();map.put("result", list);JSONObject json = JSONObject.fromObject(map);out.print(json);out.close();      return list;}
}

3.js

 $.ajax({    type:'post',    url:'/logistic/queryArea?type=prov',    cache:false,    dataType:'json',    success:function(data){
//           alert("success");console.log(data);console.log(eval(data));//前台json字符串转换json数据},    error:function(e){console.log("失败");console.log(e);}    }); 

serverlet 返回json相关推荐

  1. php将json分页,php处理分页数据并返回json

    header('content-type:application/json;charset=utf-8');//设置浏览器解析格式为json header('Access-Control-Allow- ...

  2. python response.json()报错_Django JsonResponse json格式报错 解决Django响应JsonResponse返回json格式数据报错问题...

    想了解解决Django响应JsonResponse返回json格式数据报错问题的相关内容吗,彭世瑜在本文为您仔细讲解Django JsonResponse json格式报错的相关知识和一些Code实例 ...

  3. HTML POST提交参数给PHP并返回json,上传execl文件

    微信小程序开发交流qq群   173683895    承接微信小程序开发.扫码加微信. 正文: 需求:AJAX post带参数请求接口,PHP接收后存入数据库:然后返回json数据循环渲染到HTML ...

  4. SpringMVC 返回json

    1.页面传递json数据,ajax传递 jsp <script type="text/javascript">$(document).ready(function(){ ...

  5. Spring MVC 返回json数据 报406错误 问题解决方案

    将jackson jar包改为jackson-databind-2.5.0.jar  jackson-core-2.5.0.jar  jackson-annotations-2.5.0.jar(这个版 ...

  6. Mui.ajax请求服务器正确返回json数据格式

    ajax: mui.ajax('http://server-name/login.php',{data:{username:'username',password:'password'},dataTy ...

  7. 返回json格式数据乱码

    本文为博主原创,未经允许不得转载: 原本返回json格式数据的代码: @ResponseBody@RequestMapping(value = "getListByTime", m ...

  8. spring MVC 返回json

    spring MVC如何返回json呢? 有两种方式: 方式一:使用ModelAndView Java代码   @ResponseBody @RequestMapping("/save&qu ...

  9. 根据传入url请求,返回json字符串

    /** * 根据传入url请求,返回json字符串 * @param url * @return * @throws UnsupportedEncodingException */ public st ...

最新文章

  1. Oracle开发:normal ,sysdba,sysoper区别
  2. AI破解脑电波,准确率超80%!高度还原你眼中最美的ta
  3. bmp类型转成Halcon的Hobject类型
  4. 大话西游之Office应用实例系列! 13
  5. mysql简单外连接查询
  6. LeetCode 497. 非重叠矩形中的随机点(前缀和+二分查找)
  7. 初探webpack之编写plugin
  8. UVa 12206 (字符串哈希) Stammering Aliens
  9. Leetcode 169 Majority Element
  10. 2017CCPC哈尔滨 H:A Simple Stone Game
  11. Java:下拉列表绑定后台数据
  12. [WebApi] 捣鼓一个资源管理器--数据库辅助服务器文件访问
  13. C++ 控制台编译时显示‘ ld returned 1 exit status’
  14. QEMU imx6ul开发板环境搭建
  15. python比c语言好学吗-总算找到c语言和python哪个好学
  16. 计算机网络ap参数,酒店计算机网络(含无线AP)系统主要技术参数.docx
  17. 分糖果(candy)
  18. 【CSS】下划线与文字间距,下划线粗细以及下划线颜色的设置
  19. Ubuntu如何设置显示器不熄屏
  20. 交互技术前沿学习分享——利用眼动追踪改良广告界面

热门文章

  1. 厦门理工学院计算机考研难吗,2019厦门理工学院考研难度
  2. Obsidian Pandoc导出带图片的word docx
  3. 香烟爱上火柴试听,歌曲香烟爱上火柴mp3下载,歌词
  4. 数值分析:数据插值方法
  5. 如何解决对象不支持此属性或方法
  6. 2022DASCTF Apr X FATE 防疫挑战赛复现
  7. 分分钟教你掌握linux命令
  8. js从60秒减到0停止_秒懂爱车丨关于爱车的心脏,你了解吗?
  9. 蓝桥杯算法训练 礼物 C++详解
  10. OpenFace粗糙使用备忘