Glassfish不提供所请求的资源

时间:2014-02-14 15:05:53

标签: java jsp servlets http-status-code-404

我试图在这个网站上找到其他答案,但我找不到任何可以帮助我的东西。当按下创建演员按钮将信息发送到控制器时,我总是得到“HTTP 404状态 - 未找到:请求的资源不可用”我只是想知道是否有任何可以帮助我解决这个令人沮丧的问题

这是我的index.jsp的代码,我提交信息

<!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>

这是我的控制器代码

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);
    }

}

演员类代码是

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

最后这是控制器发送输出的代码

<!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>

2 个答案:

答案 0 :(得分:1)

缺少Controller类之上的WebServlet注释

 @WebServlet(name="Controller" urlPatterns={"/Controller"} ) 
 public class Controller extends HttpServlet
 {
    //To-DO
 }

答案 1 :(得分:1)

您还可以通过添加标记手动配置web.xml文件:

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

和....

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

<servlet-mapping>标记将映射匹配<url-pattern>标记与<servlet-name>.

中指定的servlet类之间指定的任何url模式

这是基于注释的解决方案的替代方案。希望它可以帮到某人。