1

I have tried to look for answers else where on this website but i have not been able to find anything that helps me. When ever press the create actor button to send the information to a controller i always get "HTTP 404 Status - not found: The requested resource is not available" I was just wondering if any could help me sort out this frustrating problem

This is the code for my index.jsp where i submit the information

<!DOCTYPE html> 
<html> 
    <head> 
        <meta charset= "UTF-8" > 
        <title>Index Page</title> 
    </head> 

    <body> 
        <h1>Movie Example Index Page</h1> 
        <!--submit button goes to the controller--> 
        <form action="Controller" method="post"> 
           <p> 
                ID <input TYPE = "text" name = "actorID"/> 
                Name <input TYPE ="text" name ="name"/> 
                Birth Year <input TYPE ="text" name ="birth_year"/> 
                <input TYPE="submit" value="Create Actor" /> 
           </p> 
       </form> 
    </body> 
</html>

And this is my Controller code

package MVC_Movie_Example;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// Class definition 
public class Controller extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        // Make an instance of a new actor 
        int id = Integer.parseInt(request.getParameter("actorID"));
        String name = request.getParameter("name");
        int birthYear = Integer.parseInt(request.getParameter("birth_year"));
        Actor actor = new Actor(id, name, birthYear);

        // Set the actor attribute within the request 
        request.setAttribute("theActor", actor);

        // Send it on to a different View 
        request.getRequestDispatcher("outputView.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

}

The actor class code is

package MVC_Movie_Example; // Package declaration 
// Class definition 

public class Actor {

    private int id;
    private String name;
    private int birthYear;

    public Actor() {
     } // Default constructor 

    // Value based constructor 
    public Actor(int externId, String externName, int externBirthYear) {
        this.id = externId;
        this.name = externName;
        this.birthYear = externBirthYear;
    }

    // Actor id getter method 
    public int getId() {
        return this.id;
    }

    // Actor id setter method 
    public void setId(int externId) {
        this.id = externId;
    }

    // Actor name getter method 
    public String getName() {
        return this.name;
    }

    // Actor name setter method 
        public void setName(String externName) {
            this.name = externName;
        }

      // Other methods follow, including birthYear getter and setter 
     } // End of class definition

And finally this is the code for where the controller sends the output

<!DOCTYPE html> 
<html> 
    <head> 
        <meta charset= "UTF-8" > 

        <title>Movie View Example - Output View</title> 
    </head> 
    <body> 
        <h1>Movie View Example – Out View Page</h1> 
        <jsp:useBean id="actor" type="MVC_Movie_Example.Actor" scope="request" /> 

        <table border="1"> 
            <tr> 
                <th>ID</th> 
                <th>Actor Name</th> 
                <th>Year of birth</th> 
            </tr> 
            <tr> 
                <td><jsp:getProperty name= "theActor" property="id" /></td> 
                <td><jsp:getProperty name= "theActor" property="name" /></td> 
                <td><jsp:getProperty name= "theActor" property="birthYear" /></td> 
            </tr> 
         </table> 
     </body> 
 </html>
dannyb1071
  • 43
  • 1
  • 1
  • 8

2 Answers2

1

WebServlet annotation above Controller class is missing

 @WebServlet(name="Controller" urlPatterns={"/Controller"} ) 
 public class Controller extends HttpServlet
 {
    //To-DO
 }
Manjeet Brar
  • 914
  • 1
  • 9
  • 27
1

You can also manually configure the web.xml file by adding the tags:

 <servlet>
    <servlet-name>Controller</servlet-name> //a name to be used for mapping requests.
    <servlet-class>MVC_Movie_Example.Controller</servlet-class> //the servlet's package and class name.
</servlet>

and ....

<servlet-mapping>
    <servlet-name>Controller</servlet-name> //must be the same as in tag <servlet-name> above
    <url-pattern>mycontoller</url-pattern> //now..this is the name to use in the action attribute of your form.
</servlet-mapping>

The <servlet-mapping> tag will map the any url pattern matching what is specified in between the <url-pattern> tag to the servlet class specified in <servlet-name>.

This is an alternative to the annotation based solution. Hope it helps someone.

Skelli
  • 283
  • 2
  • 10