[英]Forward from JSP to another JSP without using a servlet between them?
I have a JSP page : 我有一个JSP页面:
employee5_searchByName.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>
It forwards to this servlet : 它转发到此servlet:
Employee5_After 员工5_之后
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);
}
}
}
}
That servlet is doing his stuff , and then forwards to a 2nd JSP ,called : 那个servlet正在做他的工作,然后转发到第二个JSP,称为:
employeeResult5.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>
I guess that I can use the <%
and %>
in the JSP to do the logic side of the servlet (contacting to DB and retrieve data) . 我想,我可以使用
<%
和%>
在JSP做Servlet的逻辑侧(接触以DB和检索数据)。 How can I avoid using a servlet in between , and just pass the data from one JSP to another JSP ? 如何避免在两者之间使用servlet,而仅将数据从一个JSP传递到另一个JSP?
You can use the request.redirect(URL) method to do it. 您可以使用request.redirect(URL)方法来执行此操作。 Or you can use request.
或者您可以使用请求。 forward(req, resp).
前进(要求,分别)。
Separate the business logic from the font end, there is no need to redirect to an intermediate servlet. 将业务逻辑与字体结尾分开,无需重定向到中间servlet。 The best practice is to put the business logic in a separate class and instantiate that class in the destination page.
最佳实践是将业务逻辑放在单独的类中,然后在目标页面中实例化该类。 Here is one example:
这是一个例子:
<%@page import="mysource.dbData"%> <% searchParam = request.getParameter("searchStr"); dbData data = new dbData(); String result = data.searchText(searchParam); %> <HTML> <BODY> The result is: <% out.print(result); %> </BODY> </HTML>
The industry standard is to follow an MVC Architecture . 行业标准是遵循MVC体系结构 。 Following that will create applications which are clear to understand and easy to maintain.
接下来,将创建易于理解且易于维护的应用程序。
Try this code to forward with parameteres 尝试使用此代码来转发参数
<jsp:forward page="URL">
<jsp:param nama="param1" value="hello"/>
<jsp:param nama="param2" value="hello2"/>
<jsp:param nama="param3" value="hello3"/>
<jsp:param nama="param4" value="hello4"/>
.
........... and so on
</jsp:forward
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.