• Post Reply Bookmark Topic Watch Topic
  • New Topic

web.xml finds servlet but JSP called by servlet cannot get back to the servlet  RSS feed

 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet class that calls a JSP. The servlet loads okay and I see the JSP but when I hit the submit button on the JSP I get an error message.

The servlet is located at C:\tomcat\webapps\Reverse3Servlet\WEB-INF\classes\com\javaranch\drive\Reverse3Servlet.class

Here is my C:\tomcat\webapps\Reverse3Servlet\WEB-INF\web.xml file


The error message after I click on the jsp's submit button.


If I edit the web.xml changing the url-pattern (corrected example below).


then Tomcat can no longer load the servlet



This is the C:\tomcat\webapps\Reverse3Servlet\reverse3.jsp




 
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
<url-pattern></url-pattern>

You omitted the URL pattern.
 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I screwed up the posting when I was cutting and pasting.
Once the web.xml has the pattern tomcat will not load it.




Tomcat reaction
 
Swastik Dey
Rancher
Posts: 1782
14
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<url-pattern>/Reverse3Servlet</url-pattern>

am I right?
 
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
Please use code tags, not quote tags when posting code.

You made the url pattern "/com/javaranch/drive"? Why would you name it that? You should name it something that makes sense with respect to what the servlet does.

Some people use the servlet name, as Swastik posted, but I'm not a big fan of that. Using the word "servlet" anywhere in the URL reveals implementation details.
 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<url-pattern>/Reverse3Servlet</url-pattern>


Tried that too and it did not work.




By the way each time I modify the web.xml I am completely stopping the tomcat server and shutting down the browser.
Tomcat is Version 7, java is jdk 1.6, behaviour occurs on Windows XP and Windows 7.
This is not run from a WAR file, I am moving the files into the tomcat\webapp directory manually and have verified there is no older version of a war for this project in tomcat.



 
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
margaret gillon wrote:Tried that too and it did not work.

It sounds as if you do not understand what the URL pattern is for. You don't change it to "make things work", you set it to what you want the URL to get to the servlet to be. So it makes no sense to keep changing it. You set it, and change other things to match it.

Set it to something that makes sense, and then change the URL (in this case, the form action) to match it.

I can't tell anything about what Reverse3Servlet is supposed to do by its name (so it's probably not a good one), but let's run with it.

So let's make the URL mapping: <url-pattern>/reverse3</url-pattern>

Remember, you make this what you want it to be. Just make sure it's unique within the web app. This mapping is now the servlet path for the servlet.

Then, the action for your form would be:

Don't forget that the servlet path should be prefixed with the context path.
 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I was misunderstanding the URL pattern and trying to use it as a directory mapping tool.



The pageContext is what I was missing in the reverse3.jsp

The form works now and the web.xml and reverse3.jsp are using the /reverse3 URL for the servlet.

Thank you, Bear.
 
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
 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For reference I am posting the final version of the two files.

The C:\tomcat\webapps\Reverse3Servlet\WEB-INF\web.xml file



This is the C:\tomcat\webapps\Reverse3Servlet\reverse3.jsp file.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!