• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unsuccessful redirecting of servlet based using string query parameters

 
Alex Monari
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Situation: In the code below I'm attempting to learn how to use form parameters specifically redirecting Java servlets by propagating string query parameters.

Problem: I can't seem to figure out why I'm facing a problem with re-directing the user from the form i.e. index.html using the desired string query paramters to the correct page.

Below are the steps I took before posting this up:
I made sure the URL pattern for the @WebServlet annotation is correct i.e. in my case /CityManagerWebStarter/mainmenuresponder.do

I made sure my content-root when looking at the URL is correct i.e. /CityManagerWebStarter and I can confirm this as when I launchthe following URL http://localhost:8080/CityManagerWebStarter/ it displays the index.html page as expected.

Below is my servlet code and following that is my index.html code and ListCities.html is an example of a page I'm attempting to re-direct the user to:

servlet code:



index.html:


ListCities.html:


I forgot to add my descriptor file content.



Thanks for any suggestions/help.
 
K. Tsang
Bartender
Posts: 3604
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your code even get inside the servlet doPost?

The servlet url pattern should not need to include the context path (/CityManagerWebStarter), "/mainmenuresponder.do" will do

The action in the html form again doesn't need the context path. Just "mainmenuresponder.do" should be enough

For Java EE 6, the servlet and servlet-mapping tags are not needed in web.xml. If present, I think they will override the url pattern in the servlet class but not sure. Anyhow, both should be the same.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:The action in the html form again doesn't need the context path. Just "mainmenuresponder.do" should be enough

I strongly disagree. Making the URL page-relative makes it fragile and likely to stop working at some point in the future when something gets moved around. All resource URLs in a Java web app should be server-relative, starting with the context path. This makes them bulletproof against structural changes.

Of course, the context path should not be hard-coded. See the JspFaq for information on obtaining it programmatically.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!