Java – Calling servlet from a JSP page’s form action

javajspservlets

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:

Best Answer

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>  
Related Topic