接下来我们讲讲后台的实现,Servlet+XML配置,看了前端篇的朋友可能会问为什么用xmlHttp,这是因为原装的才是最好的。

后台我们只写了一个Servlet类,还有小小的配置了基本xml。

让我们轻松的看看吧。

xml配置这个我直接上代码啦!

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>search</display-name>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  

  <servlet>

      <servlet-name>search</servlet-name>

      <servlet-class>com.blackcat.SearchServlet</servlet-class>

  </servlet>



  <servlet-mapping>

      <servlet-name>search</servlet-name>

      <url-pattern>/search</url-pattern>

  </servlet-mapping>

</web-app>

上面的search是前台编写AJAX请求url的时候设定的,要匹配。

就是单纯的servlet配置,真的没啥不好说的。

servlet类的话,更加简单了。

因为我是用模拟数据,然后用doGet方法,设置一下request、response的字符集UTF-8


@Override

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 

			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");

		response.setCharacterEncoding("UTF-8");

		//获得客户端发送来的数据keyword

		String keyword=request.getParameter("keyword");

		//处理关键字,得到关联数据

		List<String> listData=getData(keyword);

		//返回json格式

		response.getWriter().write(JSONArray.fromObject(listData).toString());;

	}

然后在写一个模拟的匹配方法就好啦。