• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Servlet Not Working

 
Mike London
Ranch Hand
Posts: 1290
6
  • Mark post as helpful
  • send pies
  • 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.



 
Mark Wa
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
even better use a relative address in your form's action

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
why use the EL ${pageContext.request.contextPath} bear?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 1290
6
  • Mark post as helpful
  • send pies
  • 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
  • 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
Ranch Hand
Posts: 1290
6
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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?

 
Udaya Sankar Pillutla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 1290
6
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 1290
6
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 1290
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your EL works fine now. Thanks Bear!

mike
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now it will work regardless of where you deploy the app.
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • 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.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!