gpt4 book ai didi

java - doGet 方法和 JSP 页面之间通信的最佳方式是什么?

转载 作者:行者123 更新时间:2023-12-02 03:32:33 26 4
gpt4 key购买 nike

我正在 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);

}

}

这是时间轴.jsp:

<script>
$(document).ready(function(){


});
</script>
This is the timeline!

如何在 jsp 中检索信息?

提前致谢。

最佳答案

对于 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);
}

}

对于 JSP:

<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>

资源链接:

doGet and doPost in Servlets

<小时/>

AJAX 更新 1:

以 JSON 形式返回 map

这是另一个显示 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);
}

和 JSP:

$(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>

资源链接:

How to use Servlets and Ajax?

关于java - doGet 方法和 JSP 页面之间通信的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37885170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com