Forward from JSP to another JSP without using a servlet between them?

I have a JSP page :

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 :

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

}

That servlet is doing his stuff , and then forwards to a 2nd JSP ,called :

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) . How can I avoid using a servlet in between , and just pass the data from one JSP to another JSP ?

Answers


Separate the business logic from the font end, there is no need to redirect to an intermediate 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:

  1. mainPage.jsp - create the page similar to your employee5_searchByName.jsp. Now this page posts the data you enter.
  2. Create a backing class called - dbData.java (DatabaseInventory in your case)- put all your database query here and functions to retrieve what your want. A function like public String searchText(String param) (similar to getProductDetailsByName(productName)) which will essentially fetch your search results from database.
  3. Now the most important part - Instantiate this class in your destination SearchResults.jsp page and show whatever data you get in a manner similar to this:
<%@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. Following that will create applications which are clear to understand and easy to maintain.


You can use the request.redirect(URL) method to do it. Or you can use request. forward(req, resp).

See example


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   

Need Your Help

pivot query doesn't work

mysql pivot

I'm trying for the first time in my life to use the PIVOT on my database table.

The type or namespace name 'ServiceHost' does not exist in the namespace 'System.ServiceModel'

wcf visual-studio-2015 iot

I'm trying to add a wcf service in an application for Windows Iot (Raspberry Pi) using Visual Studio 2015.