页面:-------------------------------------------------------------------------------------------
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
<script type="text/javascript" src="js/prototype-1.6.0.3.js"></script>
<script type="text/javascript">
function getXmlHttpRequest(){
var xhr = null;
if((typeof XMLHttpRequest)!='undefined'){
xhr = new XMLHttpRequest();
}else {
xhr = new ActiveXObject('Microsoft.XMLHttp');
}
return xhr;
}
function change(v1){
var xhr = getXmlHttpRequest();
xhr.open('get','prod.do?name='+v1,true);
xhr.onreadystatechange=function(){
if(xhr.readyState == 4){
var txt = xhr.responseText;
$('d1').innerHTML = txt;
}
}
xhr.send(null);
}
</script>
</head>
<body style="font-size:30px;">
<select style="width:120px;" id="s1" οnchange="change(this.value);">
<option value="qq">QQ</option>
<option value="bmw">宝马</option>
<option value="ff">法拉利</option>
</select>
<div id="d1"></div>
</body>
</html>
后端:------------------------------------------------------------------------------------------------------
public class ActionServlet extends HttpServlet {
public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String uri = request.getRequestURI();
String path = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
if(path.equals("/prod")){
String name = request.getParameter("name");
System.out.println("name:" + name);
if(name.equals("qq")){
out.println("性价比高");
}else if(name.equals("bmw")){
out.println("驾驶性能出众");
}else{
out.println("好车,也贵");
}
}
out.close();
}
}