ajax实现页面请求jsp数据,有大神能给写个例子么?

 我来答
匿名用户
2013-09-17
展开全部
<body>
<Table>
<Tr>
<Td>
<input type="text" name="name"/>
</Td>
</Tr>
<Tr>
<Td><div id="mydiv" style="border-style: groove"></div></Td>
</Tr>

</Table>
</body>

/*

$(function(){
$(":text[name=name]").keyup(function(){
var val = this.value;
$.get("abc",{"username":val,"age":123},callback);
});
});
function callback(data){

mydiv.innerHTML = data;
}

*/

$(function(){
$("table").attr("align","center");
$(":text[name=name]").keyup(function(){
var val = this.value;
$.get("abc",{"username":val},function(data){
mydiv.innerHTML =data;
$("#mydiv>div").mouseover(function(){
$(this).css("background-color","yellow");
$(":text[name=name]").val($(this).html());
}).mouseout(function(){
$(this).css("background-color","white");
}).click(function(){
$(":text[name=name]").val($(this).html());
$("#mydiv").html("");
});
});
});

});

package com.ajax.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ajax.dao.Dao;

public class ShwServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();

String name = request.getParameter("username");

Dao dao = new Dao();

List<String> list = dao.getList(name);

StringBuffer sf = new StringBuffer();
for(String s : list){

sf.append("<div>"+s+"</div>");
}
out.print(sf.toString());

out.flush();
out.close();
}

}

package com.ajax.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class Dao {

private Connection conn = null;
private PreparedStatement pst = null;
private ResultSet rs = null;

private Connection getConn() {
try {
String url = "jdbc:mysql://localhost:3306/mydb";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "root");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}

public List<String> getList(String name){
List<String> list = new ArrayList<String>();
try {
String sql = "select name from inf where name like '%"+name+"%'";
pst = getConn().prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next()){
list.add(rs.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
}
return list;

}

}
-<servlet> <servlet-name>ShwServlet</servlet-name> <servlet-class>com.ajax.web.ShwServlet</servlet-class> </servlet> -<servlet-mapping> <servlet-name>ShwServlet</servlet-name> <url-pattern>/abc</url-pattern> </servlet-mapping> -<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
蜀中童生
2017-07-03 · TA获得超过295个赞
知道答主
回答量:119
采纳率:0%
帮助的人:31.5万
展开全部
$.ajax({
type:"POST",
url:"${ctx}/stats/energyTrend!getElectricInfo.action",//请求地址
data:{
year:year,
month:month
},//传递参数json格式
dataType:"json",//ajax返回类型还要html/text等
success:function(data){//请求成功data为返回的数据

}
});
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2017-08-09
展开全部
HttpServletResponseresponse=ServletActionContext.getResponse();response.setHeader("ContentType","text/json");response.setCharacterEncoding("utf-8");response.getWriter().write(张三);ajax中的msg就是你传过来的值!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-09-17
展开全部
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式