This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes Trouble in hitting servlet via Hyperlink Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Trouble in hitting servlet via Hyperlink" Watch "Trouble in hitting servlet via Hyperlink" New topic
Author

Trouble in hitting servlet via Hyperlink

Rajat Sharmanaive
Ranch Hand

Joined: Jun 30, 2011
Posts: 41

Hi
On the login.jsp of my humble app, there is a hyperlink for new users who like to register. The purpose is to send them to register.jsp. Here it is:

If i remove slash, i get a blank page. If i keep slash i get a straightforward http 404 "/ActionServlet not found".

But i am not able to see the page when i click on the link. I am pretty sure that there is some problem with the mapping in web.xml but since i am new to J2EE i do not know tricks of the trade. Here is the web.xml


login.jsp also makes use of the servlet:

This works just fine. It does take me to the servlet when i click on the button present.

But on the very same page if i click on hyperlink, all i see is a blank page. I know there are some conceptual flaws here related tomapping. I tried tweaking web.xml (i have heard that's not recommended) too many times but in vain. At times i got downright http 404, at other times, milder versions like the blank page. I am assuming without slash (before the servlet name) is least incorrect.
Lastly, i feel the control does not come to servlet. Put some lines in doPost() and tried to print but there was a blank page. Here is what i see in the browser.
Please help.


Rajat Sharma
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

You need to prefix the context path to the servlet path in the action URL.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rajat Sharmanaive
Ranch Hand

Joined: Jun 30, 2011
Posts: 41

Bear Bibeault wrote:You need to prefix the context path to the servlet path in the action URL.


Thanks. That looks helpful. Any chances of slight elaboration. Even a redirection to some other similar post will be helpful. Also, why is there no such need of prefixing with contextpath in case of <form action="<ServletName>?<parameters>">. As i mentioned, it works just fine when i click the submit button. If the question is too basic and directly relates to J2EE fundamentals, please redirect to some helpful link.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

Page-relative addressing is a crap shoot -- it might work, it might not. There are too many variables to take the chance.

Server-relative addressing -- starting with the context path -- is always unambiguous and guaranteed to work.

The context path can be obtained from the request, or better yet, handled automatically in a JSP by the use of <c:url> to form URLs.
Rajat Sharmanaive
Ranch Hand

Joined: Jun 30, 2011
Posts: 41

Ok thanks for the insight Bear. If I understand correctly, i should totally do away with <a href> tags in my jsp and go for <c:url> instead. Off course, context path should be obtained. I think if i go by <a> tags i will still need some scriptlet code to get the same. So <c:url> wins there too. Please correct if needed. Thanks for the guidance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

Rajat Sharmanaive wrote:Ok thanks for the insight Bear. If I understand correctly, i should totally do away with <a href> tags in my jsp and go for <c:url> instead.

No. You still need the anchor tags. All <c:url> does is to help create the URLs properly.
Rajat Sharmanaive
Ranch Hand

Joined: Jun 30, 2011
Posts: 41

Hi Bear

The error is resolved. It was not about addressing or anything like that. I was missing the very basic fact that hyperlinks will always result in calling of doGet() (which in my servlet is empty, since all my JSPs explicitly submit with post method). I have overridden it now and put placed doPost() inside it.
In hindsight, i should have got this looking at the blank page. Nonetheless, i am introduced to <c:url> and cleaner JSPs. Thanks for your time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble in hitting servlet via Hyperlink
 
Similar Threads
Servlet Context Path Issue
Servlet action is currently unavailable
welcome page not comming
mapping error
NullPointerException in AutoScrollPhaseListener