How do I use href anchor and expression language to send parameter to servlet?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
This work fine:
This is hwat I am tring to do:
Where am I going wrong?]Thanks and best regards.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
There are 2 things that need to be taken into account, though:
1. An href is going to always create a GET HTTP request, so if a servlet was looking for a POST, it will be disappointed.
2. That's a relative URL, so the full URL might not be what you expect and therefore might not be routed to the servlet that's supposed to handle it. The best way to avoid that is to construct an absolute URL.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Tim Holloway wrote: is to construct an absolute URL.
It may just be a terminology issue, but what you really want is a server-relative URL. You don't want to hard-code the protocol and host into the URL (which is what an absolute URL would entail) but rather, you want the URL to start with the context path of the web app. You can get that from the request using the expression language.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Norm Radder wrote:A difference I see between the two lines of code is that the first one has 's surrounding a variable and the second one does not.
True. Usually, you wouldn't have the "'"s, though. URL parameters are self-delimiting.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Bear Bibeault wrote:what you really want is a server-relative URL.
Maybe I do, maybe I don't. The two are functionally equivalent.
Bear Bibeault wrote:You don't want to hard-code the protocol and host into the URL (which is what an absolute URL would entail) but rather, you want the URL to start with the context path of the web app. You can get that from the request using the expression language.
And again, maybe I do, maybe I don't. The effective URL invoked by the client must always be absolute down to the server and port level, since HTTP doesn't keep persistent connections.
The main advantage of absolute URLs is that you absolutely know where they are pointed without having to reference where the URL was acquired from. The advantage of a server-relative URL is that you no longer have a specific server domain name/IP hard-coded into the URL. The advantage of a fully-relative URL is that it's the shortest form of all. But also the most likely to bite you.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Tim Holloway wrote:But also the most likely to bite you.
Will have to disagree on this point. The safest and best-practice approach is to use server-relative URLs, and dynamically obtain the context path. That way the code works regardless of where it is deployed, immune to changes in context paths external to the application, and adopting the protocol of the page (thus avoiding protocol mismatch errors).
The latter point is less important than it used to be with browsers enforcing the use of secure protocols.
(This all assumes a servlet/JSP environment -- best practices may be different for JSF, and are certainly different for modern JS/TypeScript apps.)
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Bear Bibeault wrote:Will have to disagree on this point.
You misread. The "bite" comes when you use a completely relative path. Because not only does that not include the application context path, it also may be coming from a different resource directory within the WAR. The most common example of that is something like coding a URL for "images/bird.jpg" on a page that comes from "browse/birdPage.jsp". Because the effective URL would be "browse/images/bird.jpg" and would fail if the actual WAR resource path was "/images/bird.jpg".
Of note is that you cannot simply use a URL of "/images/bird.jpg" because it is URL relative and not webapp relative, since it lacks the webapp's URL context. That would cause it to get routed to the ROOT application (contest path "/"), which likely wouldn't know what to do with it.
The HTML PAGE directive has sometimes been used to unsnarl this sort of mess, but it has its own pitfalls.
And yes, JSF is kind enough to automatically supply the application context path, although most internal JSF requests are done via FORM POSTs, not anchor tags since one of JSF's primary strengths is in providing editable forms that keep throwing back the form with errors displayed until you get it right. There are 2 JSF tags that emit different types of GET URLs, but since we're not talking JSF, I won't go into them. Anyone who wants to know should ask in the JSF forum (or just read the autodocs).
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Cezar Apulchro wrote:I think that this is the syntax of href.[/code]... And now you're REALLY going to set Bear off!
SCRIPTLETS!!!
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Cezar Apulchro wrote:Ok I'll change to JSF.
No... you're already using the JSP EL in the second half of the latest code you posted. Don't change that. The first half of the code, it should be in the servlet which forwards to the JSP you posted and not in a JSP.
| Oh. Hi guys! Look at this tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |













