ajax实现页面请求jsp数据,有大神能给写个例子么?
4个回答
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>
<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>
展开全部
$.ajax({
type:"POST",
url:"${ctx}/stats/energyTrend!getElectricInfo.action",//请求地址
data:{
year:year,
month:month
},//传递参数json格式
dataType:"json",//ajax返回类型还要html/text等
success:function(data){//请求成功data为返回的数据
}
});
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
展开全部
有
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询