• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Simple Servlet Not Working

 
Bartender
Posts: 1876
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created a test servlet as a place holder, but I can't get it working!

Here's what I did:

Any suggestions welcome

mike

-----------

JSP:

The JSP just has a simple name/password with this form action:

<form action="http://localhost:8080/servlet/TestServlet" method="get">


-----------------------------

WEB.XML



-------------------------

The Servlet is in the referenced path under Tomcat, but when I click Submit on the JSP, I get that: "The requested resource (/servlet/TestServlet) is not available."

What could be wrong here?

Thanks in advance.



 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is your applications context root? Ie http://www.myaddress.com/myappname

Should you be adding that into the URL?

http://localhost:8080/myappname/servlet/TestServlet
 
Mark Wa
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
even better use a relative address in your form's action

 
Sheriff
Posts: 67642
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Wa wrote:even better use a relative address in your form's action

Correct. never include all that unnecessary crap. It makes your app non-portable.

Assuming the mapping is correct, the form action should be:


 
Mark Wa
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why use the EL ${pageContext.request.contextPath} bear?
 
Bear Bibeault
Sheriff
Posts: 67642
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Wa wrote:why use the EL ${pageContext.request.contextPath} bear?



It adds the context path to the URL. You don't want to hard-code it anywhere -- what if it changes?
 
Mike London
Bartender
Posts: 1876
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

Thanks for your reply.

Actually, the whole localhost:8080 string came from Marty Hall's "Core Servlets, volume 2", but is probably just test code. I removed that part of the action.

In any case, it still doesn't work if I just use, as I tried before, <form action="/servlet/TestServlet" method="get">

I still get a non-available message for the Servlet.

The servlet works when I run it from within Intellij IDEA, but when I move that "exploded" directory over underneath Tomcat's web apps, then it doesn't work.

What could be causing this?

Thanks.

mike
 
Mark Wa
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I expect you need to create a war or something first, but then I have never used that IDE
 
Mike London
Bartender
Posts: 1876
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,

A war file is one option, but you can do exploded as well.

Thanks.

I checked the IDEA directory and the Tomcat webapps folder. They're identical.

???
 
Bear Bibeault
Sheriff
Posts: 67642
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your action URL will not work unless your app is deployed as the root web app. Very fragile. Add the context path as I already posted.

Did you use Tomcat's manager app to verify that the web app is even running?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Noticed the class name in the deployment descriptor looks invalid. Is that a typo in the copy?

net.mycompany..servlets.TestServlet
 
Mike London
Bartender
Posts: 1876
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although a good idea, Bear's suggestion didn't solve the problem, but I ended up solved the problem by including the root directory name.

Changed the form action to:

<form action="/TestWeb/servlet/TestServlet" method="get" >

(TestWeb is the directory in webapps under which the web app is)

Thanks for your reply.

Thanks.

-- M
 
Bear Bibeault
Sheriff
Posts: 67642
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hard-coding the context path is a BAD IDEA. Use the EL expression that I provided. If it "isn't working" (I hate that phrase), something that needs to be fixed is wrong.
 
Mike London
Bartender
Posts: 1876
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The walrus has spoken.

You're right. Now that "it's working", I'll retrofit your EL.

Thanks Bear as always!!!

Best,

mike
 
Mike London
Bartender
Posts: 1876
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your EL works fine now. Thanks Bear!

mike
 
Bear Bibeault
Sheriff
Posts: 67642
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Now it will work regardless of where you deploy the app.
 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike London wrote:Although a good idea, Bear's suggestion didn't solve the problem, but I ended up solved the problem by including the root directory name.



I hate to nit pick, but you're including the application context name, not the root directory name. In some cases, the application context name is derived from the name of the directory where the application is deployed, but not always. Also, the "root" application means something else on an application server. Best not to get those two terms confused.

 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic