前端HTML
<body>
<button id="getvalue">
请点击
</button>
<div id = "value"></div> </body>
Ajax代码
$("#getvalue").click(function(){
$.ajax({
url:"/servlet/doajax",
cache:true,
type:"GET",
data:{username:"admin"},
async:false,
datatype:"html",
error:function(request){
alert("cuowu");
},
success:function(data){
$("#value").html(data);
}
});
});
servlet代码
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String username = request.getParameter("username");
System.out.println(username);
PrintWriter out = response.getWriter();
out.print("welcome "+username);
out.flush();
out.close();
}
xml配置
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>doajax</servlet-name>
<servlet-class>servlet.doajax</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>doajax</servlet-name>
<url-pattern>/servlet/doajax</url-pattern>
</servlet-mapping>
__YXQ__