This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Can a servlet be called by clicking on a link? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can a servlet be called by clicking on a link?" Watch "Can a servlet be called by clicking on a link?" New topic
Author

Can a servlet be called by clicking on a link?

Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Hi guys,

I just wanted to know if you can call a servlet clicking a link? Hwo would I code something like this?

If this is possible, could you point me in the direction of some examples?

Thanks in advance

Regards
Zein
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30075
    
149

Zein,
Yes. You can hardcode the parameters in the link. For example,

/MyServletPath?param1=value1¶m2=value2


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

Just use the URL to the servlet as the URL of the link.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

http://www.technicalfacilitation.com/get.php?link=wsad

Hey dude.

Sure, just provide the IP Address, context root, and name of the Servlet in the URL. Then the Servlet will be invoked directly. It think that's what you're suggesting.

The above like it to a tutorial where I create a Servlet and invoke the Servlet by the name: www.examscam.com/StrutsStuffWeb/FirstServlet

The first tutorial shows the creation of the Servlet:

http://www.technicalfacilitation.com/multimedia/wsad/02/createjspandservlet.html

The second link shows the creation of a test server, and the actual running of the Servlet:

http://www.technicalfacilitation.com/multimedia/wsad/03/servercreationandtesting.html


Enjoy!
[ August 27, 2006: Message edited by: Bear Bibeault ]
Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Thanks guys for the speedy reply.

I will experiment with your advice above.

Regards
Zein
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

"Kameron McKenzie", please contact me via email.

bear
JavaRanch Sheriff
Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Hi guys

Further the above, which method will be invoked if the servlet is called directly? Would it go to the init, and then wherever I take it??

RE: Kameron McKenzie, is he for real??
Further to what he said, do I really provide the full path to my servlet, IP....? Root context? Wouldnt that comprise my system?

Thanks for your thoughts,
Regards
Zein
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

The first time the Servlet is ever called, the init() method will be invoked. Servlets are shy creatures that just mind their own business, but get a few drinks into them and get them LOADED, and they're the life of the party. One call to init, and they've got a lampshade on their head until the server is taken down.

If the Servlet is invoked by a URL, the get invokation is being called, or the doGet method. It's pretty much the default - clicking a link, typeing a URL into an address bar, clicking a bookmark - that's all get. Even a form submission will default to get if the HTML developer doesn't explicity state method=POST

As far as calling a Servlet by providing the URL and context root and Servlet name: not sure there's any other way to do it. Clients have to get to your Servlet somehow, although an appliation will likely have a relative linking to the Servlet, as opposed to hardcoding everything into it. Of cousre, who accesses a Servlet can be restricted by a security contraint. That's the whole reason for their existence.

Regards,

-Cameron
Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
K/Cameron,

although an appliation will likely have a relative linking to the Servlet, as opposed to hardcoding everything into it


Is this relative reference to the mapping in a web.xml?

Thanks in advance
Regards
Zein

PS: the videos in the above link are great, bravo, keep up the good work
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

When I was saying a relative linking, I was just talking about the URL, and how an HTML form might invoke the Servlet.

Seriously, there's no fear with people invoking the Servlet directly. In fact, that's what we WANT them to do. Servlets are controllers, and they can implement security, state management, and even flow control, so if someone is invoking a Servlet when they shouldn't be, the Servlet can redirect them somewhere else.

The web.xml file maps an alias name for the Servlet to the actual code for the servlet. So, I might use the url:

http://www.pulpjava.com/webapp/HelloWorldServlet

But the name HelloWorldServlet will be mapped back, through the web.xml file, to a servlet perhaps fully names as com.examscam.web.servlet.Hello
Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Thank you cameron,

I will be experimenting to see how it works for me.

Regards
Zein
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can a servlet be called by clicking on a link?
 
Similar Threads
Can i call filter after particulate event?
All topics posted by me
how to call Http Servlets from other Http Servlets
Applet - Servlet Communication
clickable text