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

Servlets from an HTML anchor

 
Jim Moffat
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Head First Java book, there are code snippets that suggest you can put a servlet on an anchor, as below (P. 627, if anyone is interested):






But the broswer won't bring up the HTML document. I get "page cannot be displayed." I am running Orion, so how should I code the link to the servlet class? The Cattle Drive code page showed the URL using "servlet/classname" and that didn't work either. So it is possible to link a servlet like that? The Head First book was not too clear on this.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Moffat, Have you configured your web.xml file for this Servlet? Please post that configuration here.
 
karthikeyan Chockalingam
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let us assume the context root of your web application is myapp.

Also your server is on localhost port number 8080.

You can access the application using
http://localhost:8080/myapp/servlets/MyServletA (incase the anchor is in a file on your local machine or on some other machine in the network)

or using a relative path (if the anchor is in a resource obtained from the same web application on the server in localhost) as /myapp/servlets/MyServletA

You should have a mapping in web.xml as follows
<servlet-mapping>

<servlet-name>ServletNameXXX</servlet-name>

<url-pattern>/servlets/MyServletA/*</url-pattern>

</servlet-mapping>

where servlet name is provided(in web.xml) as

<servlet>

<servlet-name>ServletNameXXX</servlet-name>

<servlet-class>com.abc.def.somepkgname</servlet-class>

</servlet>
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can try

<a href="/servlets/MyServletA">Text</a>

instead of

<a href="servlets/MyServletA">Text</a>

say you want to visit something like

http://cool.com/myApp/servlets/MyServletA

Sometime back I faced a similar problem.A similar code like what you have mentioned worked in OC4J / UNIX , but did not work in Websphere MVS.
 
Jim Moffat
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, all. I finally figured it all out. The Head First Java book is not very good at explaining this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic