How to use attributes from servlet in several HTML pages - html

I am learning web dev on my own and building a website to search for restaurants in your area using the YelpAPI. I've got the search working but I want users to be able to click on one of the results and open up a expanded.jsp page where it displays additional details about that restaurant. The issue is I have no idea how to continue using the data I retrieve from Yelp in my servlet on multiple pages. Below is a rundown of my code:
I have a servlet that is using the YelpAPI to get a list of 10 restaurants. It then sets this list as an attribute and forwards to the results page.
List<Restaurant> restaurants = getRestaurants(name, location);
request.setAttribute("data", restaurants);
request.getRequestDispatcher("/results.jsp").forward(request, response);
I am then (inside of results.jsp) using some JSTL to just create the results page. This all works great!
<c:forEach var="restaurant" items="${data}">
<img src="${restaurant.image_url}">
<p>${restaurant.name}</p>
<p>${restaurant.location}</p>
Reviews
</c:forEach>
When the user clicks one of the images (nested in anchor tag), it obviously redirects to expanded.jsp but all of the attributes have been cleared to null and there seems to be no way to access any of this data for a specific restaurant. I'm not looking for anyone to code for me, but I would love some insight into what approach I should be taking to be able to have the user click a restaurant image and continue to a page with several more details about that restaurant.

The approach is to have a separate Servlet for the restaurant detail. This Servlet would fetch the restaurant detail based on a unique ID.
<c:forEach var="restaurant" items="${data}">
<img src="${restaurant.image_url}">
(...)
</c:forEach>
In the servlet, you would fetch the restaurant detail based on the id.
String id = request.getParameter("id");
Restaurant restaurant = getRestaurant(id);
request.setAttribute("restaurant", restaurant);

Related

How can I differentiate between two url requests from different HTML pages but with the same namespace in views.py?

I am creating a simple eBay like e-commerce website to get introduced with django. For removing an item from the watchlist, I placed two same links in two different HTML files, that is, I can either remove the item from the watchlist.html page or either from the item's page which was saved as listing.html. The url for both the pages look like this:
Remove from watchlist
Now, in my views.py, I want to render different pages on the basis of the request. For example, if someone clicked Remove from watchlist from listing.html then the link should redirect again to listing.html and same goes for the watchlist.html.
I tried using request.resolver_match.view_name but this gave me 'removeFromWatchlist' as the url namespace for both of these request is same.
Is there any way I can render two different HTML pages based on the origin of the url request?
Also, this is my second question here so apologies for incorrect or bad formatting.
You could check the HTTP_REFERER in the request.META attribute of the view to get the url that referred the request as so:
from django.shortcuts import redirect
def myview(request):
...
return redirect(request.META.get("HTTP_REFERER"))#Or however you prefer redirecting
https://docs.djangoproject.com/en/3.1/ref/request-response/#django.http.HttpRequest.META

Script to pass the user information to another Jsp page using button click

I'm a beginner and looking for help
I have a query related to posting a user_id on to Jsp page.
Designing an application to book a photographer where if I click on book for a photographer I should get the photographer details in the next jsp page.
<input type="submit" name="book" value="<%=rs.getInt("id")%> onclick="javascript:window.location='booking.jsp';"/>
You can append user_id in jsp url like following:
javascript:window.location='booking.jsp?user_id=<%=rs.getInt("id")%>'
And you can get the user_id on booking.jsp by calling
request.getParameter("user_id");

Is there any way to show all the components data from /jcr:content/par/ location

I have a query regarding the data rendering of the different page at one place. As every page is build using many components and all the components data gets stored under jcr location of page ie. /jcr:content/par/{components list}. The data is properly rendering on this page.
Now I have a situation where I need to create a component to search the page(i.e unique product), if this product page is available in the repository, I need to render its data just under the search box. For this I am creating json which I will use to render the content after search found.
But if there is any other way i can include this component from the /par location of the page to just display the data as it is, rather than building json(of all the components data) and then reading it at the time of display.
I am wondering if we have any method to display all the components data by just including the /par/{components} on a page. This way I can speed up the development, and it looks faster way to display the content as well.
thanks in advance....
If you have static page, then you can go through list of search results (product resources) and include component, which renders product, for each of them. Like:
<c:forEach items="${productsList}" var="productPath">
<cq:include path="${productPath}" resourceType="/apps/you-project/components/product-component-name"/>
</c:forEach>
If you show results dynamically - then you can do ajax requests for product resources. Something like this:
var productHtml = CQ.shared.HTTP.get(productPath + ".html");
Or the same using JQuery. Then you can add html to your page.
However, with second approach you should add clientlibs from component /apps/you-project/components/product-component-name to search results page yourself, because they will not be loaded with ajax request.

Relating PAGEID to sectionid-PageID in hyperlink

We have many pages which have link to other OneNote Pages. Section-ID and Page ID in the hyper link is different from the paged returned by the URL.
Example: For Page-ID: 1-422f7679a4e647f29bf990e2d7011caa!1-7028fe47-2ab9-4568-b5f9-fdc5f035eed1 section-Id is ={0590BF60-B259-42F5-9B1C-E21C7C3FE16D} in URL and page-id is {BAF4533A-381F-476E-A194-8397347952C2}
How do we convert 1-422f7679a4e647f29bf990e2d7011caa to 0590BF60-B259-42F5-9B1C-E21C7C3FE16D and 0590BF60-B259-42F5-9B1C-E21C7C3FE16D to BAF4533A-381F-476E-A194-8397347952C2
See this answer:
Any way in OneNote API to extract link to another OneNote page?
Id's in URLs and in the API are different.
For pages: You can relate them by extracting the page id from the oneNoteClientUrl property in the GET ~/pages API.
For sections: The GET ~/sections API does not return oneNoteClientUrls, but you can use the GET ~/pages API to get the parentSection.Id of a page, and the oneNoteClientUrl property of the page to retrieve the section id.

getting URL of pages with ajax

This is a webpage with a number of "subpages" embedded in ajax for all the user reviews.
http://www.goodreads.com/book/show/13636400-the-bone-season#
I am trying to get the specific URL for particular pages (say ?page=4), so I can point people to particular reviews directly instead of going to the page and clicking on page4. below is a snippet from the source code for that page:
<a href="#" onclick="new Ajax.Request('/book/reviews/13636400-the-bone-season?page=4', {asynchronous:true, evalScripts:true, method:'get', parameters:'authenticity_token=' + encodeURIComponent('XsOgyhAC4p0tAm8wGIqTQGY+PxGpDieI45AYry0NlE4=')}); return false;">
when i type http://www.goodreads.com/book/reviews/13636400-the-bone-season?page=4, i get a blank page.
What do I need to do get the page to show? Thank you!
You can get the url for the specific reviews, but I was unable to find anyway to do so for the "page". If you want to direct someone to a specific review, then you can get that url from the "see review" link or the date link on that particular review.
New answer: you need to submit a GET request (using cURL for example) with the parameter mentioned ('authenticity_token=' + encodeURIComponent('XsOgyhAC4p0tAm8wGIqTQGY+PxGpDieI45AYry0NlE4='), and parse what is sent back. Then you'll get your review. Linking to it is not possible unless you find a URL where your review is always displayed.