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>
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; } }
$.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); } });