Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet not available

 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After a couple of hours reading about servlet mappings, I still can't figure this one out. (yes, I'm a greenhorn).

I have two pages using the same servlet. The page using this code works just fine.


But the page that submits a form using this code results in "The requested resource (/ProdGoalsServlet) is not available."


I realize the first is doing a GET and the second is doing a POST, but I've overridden both methods and have a breakpoint set on each one. The code is never reaching the breakpoint.

A hint, please?
 
Mike Zal
Ranch Hand
Posts: 144
Fedora Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you map the URL to your Servlet? Can you post your Annotations you used on your Servlet or you web.xml file? Have you also verified that your Servlet's .class file is in the proper location for your web app?
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a better google search I found the answer. The code in the second page has to modified like so:


So now the question is why? Almost all the examples I found use the same format as my original post. Is the problem with my servlet mapping?

Here's the entry in web.xml:


Any and all enlightenment is welcome. Thanks.
 
Mike Zal
Ranch Hand
Posts: 144
Fedora Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way would be to use the c:url tag in JSTL to create your URLs.

This code is a little cleaner and it give you the option to reuse the url later in the page is you want. TO enable JSTL all you have to do is copy the JSTL jar files into you lib directory.

The reason your previous code was not working was that the URL did not include your web app name. Your Servlet's real url is www.host:8080/yourProjectName/ProdGoalsServlet , but your code was omitting the ProdGoalsServet part and linking to www.host:8080/ProdGoalsServlet.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without the context path, how would the container know which web application the request is targeted to?
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tip, Mike, I'll use that. I'd still like to know the why of it all.

Bear, you are exactly right and that seems obvious now, so why do so many examples like this one...

http://www.servletworld.com/servlet-tutorials/servlet-form-processing.html

...leave out that critical detail? Is it the way they map the servlets?

Sorry for questions that seem to have obvious answers, but this is all part of my process of learning JSTL and EL. I inherited a boatload of ugly scriptlet code, so as I'm making changes and additions I'm writing it all with "real" jsp and an MVC model with servlets and DAOs. The webserver here had NO servlets running at all. It makes for a long, steep learning curve, but I'll prevail...eventually.

Thanks again. This site is an invaluable resource. I'm learning the fastest way to find an answer with google is with "site:coderanch.com".
 
Mike Zal
Ranch Hand
Posts: 144
Fedora Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jk Robbins wrote:
http://www.servletworld.com/servlet-tutorials/servlet-form-processing.html

...leave out that critical detail? Is it the way they map the servlets?


If you take a close look at the following line in the tutorial

Note that the value inside the action attribute does not start with a slash. In this case, it is providing a relative url. Since your Servlet started with a slash, it is an absolute url.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Zal wrote:In this case, it is providing a relative url. Since your Servlet started with a slash, it is an absolute url.

Not quite.

Absolute URLs start with the protocol; e.g. http:

Page-relative URLs start with no slash or protocol. These are fragile and should always be avoided.

Server-relative URLs start with the context path and are the preferred format to use in web apps.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the Ranch FAQ entry on that specific question: RelativeLinks. I wish I had read it before writing a web application, now I have a whole lot of places where the context name is hard-coded in my links. Cleaning that up is high in my list of refactoring issues.
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Mike Zal wrote:In this case, it is providing a relative url. Since your Servlet started with a slash, it is an absolute url.

Not quite.

Absolute URLs start with the protocol; e.g. http:

Page-relative URLs start with no slash or protocol. These are fragile and should always be avoided.

Server-relative URLs start with the context path and are the preferred format to use in web apps.


Ah, at last the light bulb goes on. That's what I was looking for. Thanks again, everyone.
 
Mike Zal
Ranch Hand
Posts: 144
Fedora Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear, thank you for that correction. I've been doing mostly self study for web/Java EE programming so it's nice to be corrected when using the wrong terminology.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic