Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can a servlet be called by clicking on a link?

 
Zein Nunna
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34396
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zein,
Yes. You can hardcode the parameters in the link. For example,

/MyServletPath?param1=value1¶m2=value2
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use the URL to the servlet as the URL of the link.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for the speedy reply.

I will experiment with your advice above.

Regards
Zein
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Kameron McKenzie", please contact me via email.

bear
JavaRanch Sheriff
 
Zein Nunna
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you cameron,

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

Regards
Zein
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic