• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about my Servlet  RSS feed

 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one section of an ungraded, unpaid, web project that I'm working on with 7 of my classmates. Just a basic distribution center inventory with login, invoices, etc.. My portion is specifically the business logic for the main product page a user will see after logging in successfully. Using Tomcat7/Eclipse when I select Run --> run as --> run on server, I'm getting a 404 error on both the internal Eclipse browser and in Chrome. My question, being as new to EE and Eclipse in general as I am, would be, without everyone else's portions in place, is this what I should be seeing, and is completely normal? (I do have a redirect to Amazon.com that I thought would work for testing, but I guess not unless the code is falling short somewhere else) If that is the case, what would be the easiest way to confirm my code is working? Build a simple .jsp or something?

Thanks in advance, here's what I have:




And here is the class with the pDBQuery method:



Thanks again in advance for any and all help
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consult the Javadocs for HttpServletResponse or any other HTTP error code listing to see that 404 means "requested resource not available."

Thus your code never saw the request - the servlet container could not match your request to your code. Usually due to an error in configuration, typically in your web.xml file.

Search this forum for "404" for examples.

Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or see the ServletsFaq for likely causes.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for the responses. Again though, without my other 7 classmates' portions involved (at this point), is a 404 normal at this point I guess is what I'm wondering. I assume by looking at my code you would know if something's wrong on my end. You don't have to point it out for me if that's the case. I just have to find out whether my stuff has errors, or I'm getting the 404 due to being a singular file in what will be a larger collection of files to work as one web app.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, a 404 is not normal, and it's not a problem with your code. It means that the web app isn't running or isn't finding the servlet.

Advice: ditch running in eclipse. It adds a whole lot of complexity that's not necessary. Learn how to deploy a real web application in Tomcat (or whatever you are using).
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Brogden wrote:Consult the Javadocs for HttpServletResponse or any other HTTP error code listing to see that 404 means "requested resource not available."

Thus your code never saw the request - the servlet container could not match your request to your code. Usually due to an error in configuration, typically in your web.xml file.

Search this forum for "404" for examples.

Bill


related question I suppose; when starting a new dynamic web project in Eclipse, and checking the box that says generate deployment descriptor this is what the web.xml looks like by default:



Is this not sufficient? I did try erasing the <welcome-file-list> tag and its contents in exchange for a servlet tag with the necessary servlet-name/servlet-class/servlet-name/url-mapping tags and was still getting the same error. Would this still lead you to believe something is wrong with the web.xml? thanks again!
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Again, a 404 is not normal, and it's not a problem with your code. It means that the web app isn't running or isn't finding the servlet.

Advice: ditch running in eclipse. It adds a whole lot of complexity that's not necessary. Learn how to deploy a real web application in Tomcat (or whatever you are using).


for the sake of learning the ins and outs of Eclipse in general, I may keep using it, because I know I need to familiarize myself with it. That's in addition to Eclipse being specifically what we're using in our classes. However our last instructor definitely spent time having us interact specifically with dos/terminal and the tomcat manager page so we wouldn't be so dependent on Eclipse when we would finally start using it.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not. The servlet needs to be declared and mapped.

More advice: stop relying on the IDE to do things for you and learn how all this stuff works. IDEs prevent you from doing that (as this issue is pointing out).
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:It is not. The servlet needs to be declared and mapped.

More advice: stop relying on the IDE to do things for you and learn how all this stuff works. IDEs prevent you from doing that (as this issue is pointing out).


thank you again for the response! So that being said, again, since I did erase the welcome file tags and did declare and map the servlet, but was still getting the same 404, in your opinion would my problem still lie in the web.xml?

EDIT: also, thanks for the ServletFAQ page. In there it says the servlet should be in the WEB-INF hierarchy, which so far we've kept under Java Resources/src/package/servlet.... The only files in our WEB-INF directory are our .jsp's and the web.xml. Is putting the servlet in the WEB-INF/ directory commonplace? Maybe something the instructor hasn't gotten to yet for the sake of simplicity/learning?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, given that you have a WebServlet annotation, I don't think you need to map the servlet in your web.xml. In fact, I would guess that might possibly cause problems if both mappings were inconsistent. Which URL are you hitting that causes the 404?
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Actually, given that you have a WebServlet annotation, I don't think you need to map the servlet in your web.xml. In fact, I would guess that might possibly cause problems if both mappings were inconsistent. Which URL are you hitting that causes the 404?


well at first I set the annotation to just @WebServlet("") and the context root to "/" while leaving the web.xml alone (no mapping), and was getting the 404.

Then I set the annotation to @WebServlet("/BusinessLogic") and the url mapping to "/BusinessLogic" and was still getting the same error so who knows...

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I missed the annotation.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Which URL are you hitting that causes the 404?
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
Ulf Dittmer wrote:Which URL are you hitting that causes the 404?


depending on which Annotation/Servlet Mapping (or lack thereof) I've had at a given time, I've tried pretty much every URL. I tried http://localhost:8080/, I tried http://localhost:8080/BusinessLogic, etc, etc,. But 404 seems to be the monkey on my back for all.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the URL needs to end in "/BusinessLogic", because that's what it's mapped to. Let's stick to that mapping.

Both URLs you mention lack the web app name, so they would only work if the web app is the ROOT web app - which I highly doubt it would be if you run it inside an IDE. (It also generally isn't the ROOT web app if you run it in a standalone Tomcat, BTW.)
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wild guess that your web-app context name is Capstone, so does http://localhost:8080/Capstone/BusinessLogic work or return something different?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaikiran Pai wrote:Wild guess that your web-app context name is Capstone, so does http://localhost:8080/Capstone/BusinessLogic work or return something different?


Yes, that should be your URL.
Also be careful how you call it.
Will your colleagues be using POST or GET?
Does that match up with the Servlet method you have overriden?
Then concerning testing, split things up (you seem to have done this relatively ok) and test them separately.
So test your database logic outside of your servlet environment. Make the servlet so that when your friends later say that they now want to call a webservice instead of a servlet then you won't have to copy any code from the servlet.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The only files in our WEB-INF directory are our .jsp's and the web.xml.

The jsps and the WEB-INF should normally be in the same directory. Is your web app located under the ROOT directory or webapps directory? If it is under ROOT directory, you can move your servlets in <tomcat-home>/webapps/ROOT/WEB-INF/classes/ and see if it will run them.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help and suggestions guys, I got it fixed. I won't say I figured out the problem, because I didn't, but it most likely had to do with the mapping like everyone was suggesting. What I did was create a new project entirely and left the context root, and the mappings to defaults, then recreated each file and copy/pasted verbatim, and now everything works. That'll teach me to mess around with settings I don't completely understand yet

Thanks again
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:
The jsps and the WEB-INF should normally be in the same directory.

No, not normally. In a properly structured web app they are under a folder inside the WEB-INF to prevent them from being directly served without their page controllers.

Please see this article.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In a properly structured web app they are under a folder inside the WEB-INF to prevent them from being directly served without their page controllers.

Bear,
Thank you for the precision. In many web apps i had deployed in Tomcat server, i always did put the associated jsp files in a directory under the webapps folder. But in each of the jsps, i had to test for null values of certain session variables so that a jsp page called directly from a browser would not be served but rather displays a customized page.
The article you wrote is also very instructive.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!