结合Ajax类使用:
javascript代码:
servlet代码:
一定要设置编码,这对于使用中文的数据很重要。
request.setCharacterEncoding("UTF-8");response.setContentType("text/text;charset=utf-8");
package com.gordon.servlet;import java.io.IOException;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet(name = "IsExistUserAjaxServlet", urlPatterns = { "/IsExistUserAjaxServlet" })public class IsExistUserAjaxServlet extends HttpServlet { private static final long serialVersionUID = 1L; public IsExistUserAjaxServlet() { super(); } public void init(ServletConfig config) throws ServletException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/text;charset=utf-8"); String name = request.getParameter("name"); String sex = request.getParameter("sex"); int age = Integer.valueOf(request.getParameter("age")); System.out.println(name + ", " + sex + ", " + age); response.getWriter().println("name:" + name + "age:" + age + "sex:" + sex); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}