接下來咱們講講後臺的實現,Servlet+XML配置,看了前端篇的朋友可能會問爲何用xmlHttp,這是由於原裝的纔是最好的。前端
後臺咱們只寫了一個Servlet類,還有小小的配置了基本xml。java
讓咱們輕鬆的看看吧。web
xml配置這個我直接上代碼啦!json
<?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的時候設定的,要匹配。app
就是單純的servlet配置,真的沒啥很差說的。jsp
servlet類的話,更加簡單了。ide
由於我是用模擬數據,而後用doGet方法,設置一下request、response的字符集UTF-8url
@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());; }
而後在寫一個模擬的匹配方法就好啦。code