1

I'm trying to pass a value input to a textbox in a JSP page to a servlet that will store the value as a variable. But when I click the submit button the servlet isn't found. I get an error stating the requested resource is not available

Servlet Class:

    //parse input from hello.jsp input box 
    //and assign to fibNum variable
    
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        
        
        
        
    }
    

}

I've looked at some questions like this: <form action="/sampleServlet" giving me exception but changing the path didn't change the outcome.

Does anyone know how to fix this problem calling of a servlet? Or is there a step I'm missing in linking up the servlet?

res

package

Also this is the structure of my project tree:

Community
  • 1
  • 1
Brian Var
  • 6,029
  • 25
  • 114
  • 212

5 Answers5

1

You need to create servlet mapping in your web.xml. See here as well. So in your web.xml define;

<servlet>
  <servlet-name>hello</servlet-name>
  <servlet-class><package name>.HelloServlet</servlet-class>
</servlet>

Then create mappings (url patterns) for the servlet.

<servlet-mapping>
  <servlet-name>hello</servlet-name>
  <url-pattern>/say_hello/*</url-pattern>
</servlet-mapping>

Now in your JSP refernce the servlet like

 <form action="say_hello" method="get">            
   <b>Fibonacci Sequence Length </b>  <br>
   <input type="text" name="fibNum"size="20px" style="font-size:30pt;height:60px" >
   <input type="submit" value="submit" style="font-size:30pt;height:60px" > <br>  
   Value [1-100]<br>
 </form>  
Community
  • 1
  • 1
Jerome Anthony
  • 7,823
  • 2
  • 40
  • 31
  • I tired the above but it seems there is a problem with on one of the tags at the line: `.HelloServlet`. The error I'm geting is `Attribute name "name" associated with an element type "package" must be followed by the ' = ' character.` – Brian Var Dec 09 '14 at 14:56
  • I also had to change the form action to `post` to match the servlet. – Brian Var Dec 09 '14 at 18:57
  • The above solution worked, just had to correct the mappings as pointed out, How could I pass an integer array to the jsp page instead of just an integer value? I was thinking something like this:`resp.sendRedirect(("result.jsp?fibSeq=" + fibSeq));` but then I get the eeror: `fibSeq cannot be resolved to a variable` I'm guessing there is a different method involved for passing an array? The integer array is declared like `int[] fibSequence` – Brian Var Dec 10 '14 at 22:48
1

you have to pass the servlet name same as your url_pattern(web.xml) to action tag of form.

Sanjay
  • 2,481
  • 1
  • 13
  • 28
1

you have to do mapping in web.xml with url pattern - HelloServlet.

Rajasekhar
  • 787
  • 6
  • 13
  • 31
1

I think in your code you just missed out some servlet declaration in web.xml file

<servlet>
   <servlet-name>...</servlet-name>
   <servlet-class>...</servlet-class>
</servlet>

<servlet-mapping>
   <servlet-name>...</servlet-name>
   <url-pattern>...</url-pattern>
</servlet-mapping>
V.Rohan
  • 945
  • 2
  • 14
  • 27
0

Final Solution: Tomcat 10 has problem please remove. download tomcat 9 from https://tomcat.apache.org/download-90.cgi.

I tried in Eclipse and Intellij with Tomcat 9 it works but the same code with Tomcat 10 has problem.

Ashraf Gardizy
  • 357
  • 4
  • 9