我正在Eclipse中创建一个动态Web项目,无法弄清楚如何将查询结果发送到jsp文件。
这是servlet doGet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession();
String name = session.getAttribute("user").toString();
String query = "SELECT DISTINCT t.text, t.user, t.date"
+ " FROM users u, tweets t, follows f"
+ " Where t.parent is null"
+ " AND u.id ='"+name + "'"
+ " AND ( f.follower = u.id"
+ " AND f.followed = t.user"
+ " OR t.user = u.id)"
+ " ORDER BY t.date DESC;";
try {
ResultSet rs = Dao.executeQuerySQL(query);
while (rs.next()){
//Get all tweets -> THIS IS THE INFO I WANT TO RETRIEVE
rs.getString(1);
}
}
这是timeline.jsp:
<script>
$(document).ready(function(){
});
</script>
This is the timeline!
我如何在jsp中检索信息?
提前致谢。
答案 0 :(得分:1)
对于doGet()的Servlet部分
@WebServlet("/products")
public class ProductsServlet extends HttpServlet {
@EJB
private ProductService productService;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Product> products = productService.list();
request.setAttribute("products", products); // Will be available as ${products} in JSP
request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);
}
}
<table>
<c:forEach items="${products}" var="product">
<tr>
<td>${product.name}</td>
<td><a href="product?id=${product.id}">detail</a></td>
</tr>
</c:forEach>
</table>
这是另一个将Map<String, String>
显示为<option>
的示例:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String, String> options = new LinkedHashMap<>();
options.put("value1", "label1");
options.put("value2", "label2");
options.put("value3", "label3");
String json = new Gson().toJson(options);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
}
$(document).on("click", "#somebutton", function() { // When HTML DOM "click" event is invoked on element with ID "somebutton", execute the following function...
$.get("someservlet", function(responseJson) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response JSON...
var $select = $("#someselect"); // Locate HTML DOM element with ID "someselect".
$select.find("option").remove(); // Find all child elements with tag name "option" and remove them (just to prevent duplicate options when button is pressed again).
$.each(responseJson, function(key, value) { // Iterate over the JSON object.
$("<option>").val(key).text(value).appendTo($select); // Create HTML <option> element, set its value with currently iterated key and its text content with currently iterated item and finally append it to the <select>.
});
});
});
与
<select id="someselect"></select>
答案 1 :(得分:0)
使用jaery ajax函数之一,如.ajax或.get来调用servlet。 请参阅以下API链接。
http://api.jquery.com/jquery.ajax/
http://api.jquery.com/jquery.get/
这样的事,
$.get( "servlet url here", function( data ) {
$( ".result" ).html( data );
alert( "Load was performed." );
});
在servlet中将数据转换为json或html片段并写入响应。