这里我通过AJAX调用从JSP调用servlet。在servlet中,从DB获取数据并使用Gson将这些数据转换为JSON格式。之后我想将此json对象发送到此JSP。但是我在这里不能发送json对象。请指教我? 这里是AJAX Call的代码:
<script type="text/javascript">
$(document).ready(function() {
$('#subbtn').click(function(event) {
console.log("Hello");
var searchfield = $('#searchfields').val();
var operator = $('#operator').val();
var fieldvalue = $('#searchstring').val();
var filter=searchfield+operator+"'"+fieldvalue+"'";
console.log(filter);
$.get('SearchData', {
filter : filter
}, function(responseText) {
$('#example').html(responseText);
console.log('dddd');
});
});
});
</script>
这是servlet COde:
String query = "select * from searchsample";
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
while (resultSet.next()) {
renderingengine = resultSet.getString("RenderingEngine");
browser = resultSet.getString("Browser");
platform = resultSet.getString("platform");
engineversion = resultSet.getString("EngineVersion");
cssgrade = resultSet.getString("CSSGrade");
}
Gson gson = new Gson();
JsonVo jsondata=new JsonVo();
jsondata.setRenderingengine(renderingengine);
jsondata.setBrowser(browser);
jsondata.setPlatform(platform);
jsondata.setEngineversion(engineversion);
jsondata.setCssgrade(cssgrade);
String toJson = gson.toJson(jsondata);
System.out.println("Json Format : " +toJson);
request.setCharacterEncoding("utf8");
response.setContentType("application/json");
答案 0 :(得分:0)
我认为您只需要在下面写一行就可以JSON
向JSP
发送servlet
。
response.setContentType("application/json");
response.getWriter().write(toJson);