0

Updated: I am Sending data through a jquery as shown below.

<button onclick="call()">Order</button>
<button onclick="call()">Order</button>
<script>
function call(){
    window.location.href = "hotel?resName="+$(event.target).parent().siblings().find('.shop-name').text();
}
</script>

Those <buttons> have their respected siblings which contains a div with class="shop-name"

My Servlet Code

@WebServlet(name = "hotel", urlPatterns = {"/hotel"})                //servlet name
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try  {
        if(request.getParameter("resName") != null){
            HttpSession session = request.getSession();
            fetchFoodList fetchFL = new fetchFoodList(session);

            ArrayList<foods> foodList = fetchFL.getAllFoods(request.getParameter("resName"));
            request.setAttribute("foodList", foodList);

            request.getRequestDispatcher("hotel.jsp").forward(request, response);
        }
    }catch(Exception e){
        System.out.println(e);   
    }
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

}

I am getting ArrayList<foods> from another java class and it's returning a value.

hotel.jsp here I need to come from servlet (some part of code)

<%
    System.out.println("In JSP");                         //It's printing in output console
    ArrayList<foods> foodList = (ArrayList<foods>)request.getAttribute("foodList");
    System.out.println(foodList);                         //It's also printing [Model.foods@6659e940]
    for(foods food: foodList){
%>
<div class="foodlist-outer">
   <div class="foodlist-inner">
      <div class="img-container">
         <div class="img-wrapper">
            <div class="img-div"></div>
               <img src="images/side-img1.webp" alt="">
            </div>
         </div>
         <div type="veg" class="logo-container">
            <div class="logo"></div>
         </div>
         <div class="food-details-container">
            <div class="food-details-inner">
               <div class="food-details-wrapper">
                  <h4 class="heading food-name"><%= food.getFoodName() %></h4>
                  <div class="cost">
                     <span>$<%= food.getFoodCost() %></span>
                  </div>
               </div>
               <div class="order-btn-container">
                  <div class="order-btn">
                     <input type="button" value="-" class="negative">
                     <span class="add txt">Add</span>
                     <input type="button" class="qty add" value="">
                     <input type="button" value="+" class="positive">
                  </div>
                  <span class="customize"></span>
               </div>
            </div>
            <p class="description"><%= food.getFoodDescription() %></p>
         </div>
      </div>
   </div>
 <% 
 }
 %>

Finally, All the values are computing and server is successfully sending ArrayList<foods> to JSP. It has been confirmed by println() statements but the hotel.jsp is not at all loading...

Thank You!!!

Shashank Gb
  • 902
  • 1
  • 6
  • 14
  • You cannot redirect from your servlet as you have use ajax so `response` will come back to ajax inside success function or error function.You can write redirect code inside `success` function of ajax i.e : `window.location.href="hotel.jsp";` – Swati Nov 07 '20 at 11:53
  • @Swati Thanks for Reply, I did as you said it works but it will give `java.lang.NullPointerException` because ajax also wants data back but I am using `ArrayList` in jsp :( Is there any other way apart from ajax? – Shashank Gb Nov 07 '20 at 12:37
  • You need to display that `arraylist` on new page ? why use ajax then ? Simply use `form` or `a` tag to do same – Swati Nov 07 '20 at 12:43
  • @Swati What I am going to do is when a respected restaurant button is clicked, restaurant name is go through ajax and server will give `arraylist` which contains `foodlist` It has to be display in `hotel.jsp` with some complex `div` tags. Please look at once **hotel.jsp** . Is it possible using `form` and `a` tags – Shashank Gb Nov 07 '20 at 12:51
  • 1
    Only the way of calling servlet will change everthing else i.e : `displaying data in jsp` code will remain same you don't need to make any changes there .So , suppose you need to call servlet using same `function callAjax()` just use `window.location.href="hotel?resName="+$(event.target).parent().siblings().find('.shop-name').text()` this will call your servlet with parameter `resName` . – Swati Nov 07 '20 at 12:58
  • @Swati I am beginner for servlet and jsp, Can you please help me out in answer section. It is empty page again!!. – Shashank Gb Nov 07 '20 at 13:04
  • I would have answer this question is already closed :D . Did you check if `request.getParameter("resName")` has any value ? Its getting redirect to servlet or not ? Also did you remove ajax code and added above code inside your function `callAjax()` ? – Swati Nov 07 '20 at 13:09
  • @Swati Yes it has value and it is computating every logic but not redirecting to `hotel.jsp`. Yes I did.. – Shashank Gb Nov 07 '20 at 13:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224248/discussion-between-swati-and-shashank-gb). – Swati Nov 07 '20 at 13:17

0 Answers0