gpt4 book ai didi

java - 从 JSP 转发到另一个 JSP 而不在它们之间使用 servlet?

转载 作者:行者123 更新时间:2023-12-01 23:13:56 24 4
gpt4 key购买 nike

我有一个 JSP 页面:

employee5_searchByName.jsp

<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>

<!-- EMPLOYEE wants to search items by their name -->

<!DOCTYPE html>
<html>
<head>

<title>Employee - Search for items by name</title>
<link rel="stylesheet"
href="./css/styles.css"
type="text/css"/>

<link rel="stylesheet" type="text/css" href="css/jquery.autocomplete.css" />
<script src="http://www.google.com/jsapi"></script>
<script>
google.load("jquery", "1");
</script>
<script src="js/jquery.autocomplete.js"></script>
<style>
input
{
font-size: 120%;
}
</style>
</head>
<body>

<h1>Employee - Search for items by name</h1>
<h1>
Search for items by name
</h1>

<fieldset>
<legend>Please enter the item's name :</legend>
<form action="Employee5_After">
Item's name : <input type="text" name="prod_name" id="myProduct"><br>
<script>
$("#myProduct").autocomplete("autocompleteFromDb.jsp");
</script>
<input type="submit" value="Register">
</form>
</fieldset>

</body></html>

它转发到此 servlet:

Employee5_After

package controller.employee;

/**
* Retrieve the record of a given product by its name
* using hibernate
* @author X
*
*/
@WebServlet("/Employee5_After")
public class Employee5_After extends HttpServlet {

private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// grab the product's name that the user entered
String productName = request.getParameter("prod_name");

// create DB instance
DatabaseInventory inventory = new DatabaseInventory();

// get the details
String details = inventory.getProductDetailsByName(productName);
HttpSession session = request.getSession();

// forward answer to JSP
synchronized(session)
{
if (details != null) // then the product has been found
{
session.setAttribute("foundProd", details);
String addressPath = "/WEB-INF/results/employee/employeeResult5.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(addressPath);
dispatcher.forward(request, response);
}
}
}

}

那个 servlet 正在做他的事情,然后转发到第二个 JSP,称为:

employeeResult5.jsp

<!-- Employee - get a description of a product by its name -->


<!DOCTYPE html>
<html>
<head><title>The details for the product you requested are below</title>
<link rel="stylesheet"
href="./css/styles.css"
type="text/css"/>
</head>
<body>
<h1>Here are the details from the product you request :</h1>
<h2>${foundProd}</h2>
<h1>We wish you well - bye bye!</h1>


<fieldset>
<legend>Go back to Employee's web-page</legend>
<form action="blablabla">
<a href="backToEmployeeMenu">Press here to continue</a>
</form>
</fieldset>

</body></html>

我想我可以使用<%%>在 JSP 中执行 servlet 的逻辑部分(联系数据库并检索数据)。如何避免在两者之间使用 servlet,而只是将数据从一个 JSP 传递到另一个 JSP?

最佳答案

您可以使用 request.redirect(URL) 方法来完成此操作。或者您可以使用请求。转发(请求,响应)。

See example

关于java - 从 JSP 转发到另一个 JSP 而不在它们之间使用 servlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21507762/

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