[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Help understanding servlet mapping so JSP form action works properly

Posted on 2010-03-31
12
Medium Priority
?
561 Views
Last Modified: 2013-12-02
I am trying to learn JSP and Java Servlets.  Tomcat is set up such that all of my files have to go into a subdirectory under the WEBAPPS directory.  

    webapps\kr

My application is a simple user registration program.

Here's what it looks like under the kr directory:

index.html
images\xxx.jpg
hw5\register.jsp
hw5\compute.jsp
WEB-INF\classes\hw5\register.class
WEB-INF\classes\hw5\updateRegistration.class

So here's my problem.  I start off the app in the index.html.  From there I select an option that takes me to form1.jsp.  I enter some data and hit submit.
Here's my form action
    <form action="../register" method="get">

This works fine the first time.

So then from the second page, I decide I want to edit something on the first page.  So I select Edit:

    <form action="updateRegistration" method="POST">
        <input type="hidden" name="actn" value="Edit">
        <input type="submit" value="Edit"/>
    </form>


This takes me to a servlet which does nothing more than send me back to the first page

        if(action.equals("Edit"))
        {
            url = "/hw5/conference.jsp";
        }

        RequestDispatcher dispatcher =
             getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);


So now I'm back on the first page and I make my change and then I click submit.  But this time I get a 404 error, "...the requested resource (/register) is not available

Please help me understand what I'm doing wrong, this is driving me nuts!!!

Here's a snippet from my web.xml file

    <servlet>
        <servlet-name>register</servlet-name>
        <servlet-class>hw5.register</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>updateRegistration</servlet-name>
        <servlet-class>hw5.updateRegistration</servlet-class>
    </servlet>

Thanks!
Karen




0
Comment
Question by:kreid1960
  • 7
  • 5
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 29299437
>     <form action="../register" method="get">

suggest not using relative url and instead specify the absolute url
0
 

Author Comment

by:kreid1960
ID: 29299533
So what would my absolute url be?
0
 
LVL 92

Expert Comment

by:objects
ID: 29299700
sounds like its /hw5/register
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 

Author Comment

by:kreid1960
ID: 29299855
I'm trying it.  You may be right!
0
 

Author Comment

by:kreid1960
ID: 29300489
Nope.  I get the 404 error with that.  I tried /krroot/hw5/register too and it also failed.

This is what is driving me crazy, I can't figure out what the url should be.
0
 
LVL 92

Expert Comment

by:objects
ID: 29300697
what mapping do you have in your web.xml
0
 

Author Comment

by:kreid1960
ID: 29300792
I put it in the original post:

    <servlet>
        <servlet-name>register</servlet-name>
        <servlet-class>hw5.register</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>updateRegistration</servlet-name>
        <servlet-class>hw5.updateRegistration</servlet-class>
    </servlet>

0
 
LVL 92

Expert Comment

by:objects
ID: 29300939
that just defines the servlet, you also need to specify the mapping eg.


   register
   /register


0
 

Author Comment

by:kreid1960
ID: 29301424
Yes, I have that too.  Sorry I forgot to post it.

    <servlet-mapping>
        <servlet-name>register</servlet-name>
        <url-pattern>/register</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>updateRegistration</servlet-name>
        <url-pattern>/updateRegistration</url-pattern>
    </servlet-mapping>
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 29301584
> sounds like its /hw5/register

sorry, ignore that. Should be

 /kr/register

got your package name and context name mixed up
0
 

Author Comment

by:kreid1960
ID: 29301904
Perfect!  It worked.

Thank you!!
Karen
0
 

Author Closing Comment

by:kreid1960
ID: 31709741
I wish I had asked weeks ago.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is about some of the basic and important steps to be used to improve the performance in web-sphere commerce application development. 1) Always leverage the Dyna-caching facility provided by the product 2) Remove the unwanted code …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question