• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can I call servlet without <form> and submit button?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Imagine I have a header:



selectedTankList.jsp is to display the results of select from databse. So when user click the link <a href="selectedTankList.jsp"> I have to call Servlet that is responsible for the select action and that after call the selectedTankList.jsp with results. but I only know one way to call servlet - using form and submit button. How can I call servlet without <form> and submit button?

cross ref
 
Sheriff
Posts: 67746
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
You can use the same URL for the servlets as you use for a form action.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. Your URLs should not be page-relative. Use server-relative URLs that start with the context path. See the JspFaq for more information.
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works:



My mistake was: one time I didn't put "/" before address, another time I didn't put .java, third time opposite....
Well link above is correct.
Thanks, Bear!
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Additional question. As I call the Servelt first, then I do not have "doGet" "doPost" methods. So how can I start any processes in that Servlet? which method I shall use?
 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sergey Lotvin wrote:Additional question. As I call the Servelt first, then I do not have "doGet" "doPost" methods. So how can I start any processes in that Servlet? which method I shall use?


When hyperlink is clicked, it will go to the Servlet and "doGet()" method will be called.
You can write your code in "doGet".
 
Bear Bibeault
Sheriff
Posts: 67746
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
You don't need the ".java" unless that's the way that the servlet mapping has been declared (which is really odd, by the way). The URL must match the servlet mapping.
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tapas Chand wrote:

Sergey Lotvin wrote:Additional question. As I call the Servelt first, then I do not have "doGet" "doPost" methods. So how can I start any processes in that Servlet? which method I shall use?


When hyperlink is clicked, it will go to the Servlet and "doGet()" method will be called.
You can write your code in "doGet".


For some reason doesn't work for me. Example of test project. There it woarks for <form>+submit, but doesn't for href
JSP


Servlet


project structure

cross ref on russian SO

Please help!
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what happens when you click that link?
 
Bear Bibeault
Sheriff
Posts: 67746
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
What is action="act"?

Show us your servlet mappings. (And no, I'm not going to another link to see your code. Post it here.)
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:So what happens when you click that link?


Just blank page with URL in address bar:

http://localhost:8084/mS/ns.java



What is action="act"?



Just name that is connected through web.xml to Servlet
web.xml



And no, I'm not going to another link to see your code. Post it here.



No, you don't have to. For sure. It's just according to rules one must notify if ask the same questions in different places.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so the servlet mapping is "act" (we could have a big conversation on properly naming things, but we'll save that for later).

So why did you use something different in the link href?

Also, see my post above about server-relative URLs. Your form action may work now, but because it is page-relative, it is fragile and is likely to break when you make changes in the future. The URLs for form actions and other resources should always start with the context path. See the JspFaq for how to best do that.
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So why did you use something different in the link href?



I don't why. I somehow decided or misunderstood the book...I don't know, but I haven't even a thought to use <url-pattern>
So for someone like me here the right code:


The same (+"/") one can use when call Servlet from another Servlet:



Use just what you put between <url-pattern>...</url-pattern> tags.
And doGet will work in your target Servlet.

Big thanks, Bear!
Thanks to All!
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sergey Lotvin wrote:
Use just what you put between <url-pattern>...</url-pattern> tags.
And doGet will work in your target Servlet.


Not quite; you seem to be continually ignoring my advice about server-relative URLs.

Your paths my accidentally work now, but are likely to break with even a small future change.

If the context path (name of war file) is "whatever", the the URL should be /whatever/url-pattern. You can obtain the context path via a method on the request, which is easily accessed via the EL. Again, see the JspFaq.

And, please, choose names more descriptive than "act".
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:...continually ignoring my advice...


Sorry me :) I'm like want to get the result faster...you know like a baby, though I'm not :)
Honestly, I went by your link to JspFaq, but there are so much questions that I even didn't find which should help me. Could you please rub my nose in the relative questions, I mean specify the link to topic server-relative URLs and proper names for JSP/Servlet etc.
Thanks in advance!
 
Bear Bibeault
Sheriff
Posts: 67746
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
https://coderanch.com/how-to/java/ResourceUrlProblems
 
permaculture is giving a gift to your future self. After reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic