• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How do I use href anchor and expression language to send parameter to servlet?

 
Ranch Hand
Posts: 684
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've a jsp table that send a parameter to my servlet using a anchor href, this process work ok and my servlet receive the parameter fine. Now in another jsp  I want to send parameter using a href anchor with a EL that when click the parameter is seted to send but this is not worked, I don't know how to write the right syntax, what I do is:
This work fine:

This is hwat I am tring to do:

Where am I going wrong?]Thanks and best regards.
 
Saloon Keeper
Posts: 28325
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see anything obviously wrong, myself, though I'm pretty blind these days.

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.
 
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Tim Holloway
Saloon Keeper
Posts: 28325
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And Tim's point about the GET is spot on; if you need a POST you'll either need to use a form, or use Ajax.
 
Tim Holloway
Saloon Keeper
Posts: 28325
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Cezar Apulchro
Ranch Hand
Posts: 684
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry guys I describe my problem wrong manner, in this case my table isn't opened when page is loaded, and I think that this is the syntax of href.
 
Tim Holloway
Saloon Keeper
Posts: 28325
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Tim Holloway
Saloon Keeper
Posts: 28325
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cezar Apulchro wrote:I think that this is the syntax of href.[/code]... And now you're REALLY going to set Bear off!

SCRIPTLETS!!!

 
Cezar Apulchro
Ranch Hand
Posts: 684
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I'll change to JSF.
 
Sheriff
Posts: 28331
97
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't listen to Steve. Just read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic