package servlet;html
import java.io.IOException;java
import java.io.Writer;ajax
import javax.servlet.ServletException;json
import javax.servlet.http.HttpServlet;數組
import javax.servlet.http.HttpServletRequest;異步
import javax.servlet.http.HttpServletResponse;ide
/*code
* 處理異步請求htm
*/對象
public class AjaxServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//處理請求亂碼
req.setCharacterEncoding("utf-8");
//獲取註冊數據
String username = req.getParameter("username");
System.out.println("ajax------"+username);
//處理響應亂碼
resp.setContentType("text/html;charset=utf-8");
//響應客戶端PrintWriter
/*
* json:{}對象 []數組 鍵:值,
* 相應到客戶端的code=1表明用戶名已存在
* code=0表明用戶名不存在
*/
String rel="{\"code\":\"1\"}";
Writer w = resp.getWriter();
if(!"admin".equalsIgnoreCase(username)) {
rel="{\"code\":\"0\"}";
}
w.write(rel);
w.flush();
w.close();
}
}