1新建一个servlet xml中相应配置(一般自动)
2创建service方法
3接受参数,做操作,返回数据
比如页面发送ajax请求到SomeServlet
$.post("SomeServlet的请求路径",{param:"param"},function(data){
//data为返回的数据以json形式
alert(data.id + " " + data.name + " " + data.age );
},"json");
Servlet
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String param = request.getParameter("param");//获取参数
//你的操作
//返回数据
String json = "{\"id\":1,\"name\":\"张三\",\"age\":18}";
out.print(json);
}
用Jquery框架么?