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

时间:2014-02-02 06:29:52

标签: java jsp servlets

我有一个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的逻辑端(与DB联系并检索数据)。如何避免在中间使用servlet,只是将数据从一个JSP传递到另一个JSP?

3 个答案:

答案 0 :(得分:2)

您可以使用request.redirect(URL)方法来执行此操作。 或者您可以使用请求。前进(req,resp)。

See example

答案 1 :(得分:1)

将业务逻辑与字体结尾分开,无需重定向到中间servlet。最佳实践是将业务逻辑放在单独的类中,并在目标页面中实例化该类。这是一个例子:

  1. mainPage.jsp - 创建类似于 employee5_searchByName.jsp 的网页。现在,此页面会发布您输入的数据。
  2. 创建一个名为 - dbData.java 的后备类(在您的情况下为DatabaseInventory) - 将所有数据库查询放在此处并运行函数以检索您想要的内容。像 public String searchText(String param)(类似于getProductDetailsByName(productName))这样的函数,它基本上会从数据库中获取搜索结果。
  3. 现在最重要的部分 - 在目的地 SearchResults.jsp 页面中实例化此类,并以与此类似的方式显示您获得的任何数据:
  4. <%@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>
    

    行业标准是遵循MVC Architecture。接下来,将创建清晰易懂且易于维护的应用程序。

答案 2 :(得分:0)

尝试使用此代码转发参数

<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