Java 请求的资源不可用,Glassfish
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21782791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The requested resource is not available, Glassfish
提问by dannyb1071
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
我试图在这个网站上的其他地方寻找答案,但我找不到任何对我有帮助的东西。当按下创建演员按钮将信息发送到控制器时,我总是得到“HTTP 404 状态 - 未找到:请求的资源不可用”我只是想知道是否有任何可以帮助我解决这个令人沮丧的问题
This is the code for my index.jsp where i submit the information
这是我提交信息的 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>
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>
采纳答案by Manjeet Brar
WebServlet annotation above Controller class is missing
缺少控制器类上方的 WebServlet 注释
@WebServlet(name="Controller" urlPatterns={"/Controller"} )
public class Controller extends HttpServlet
{
//To-DO
}
回答by Kelli
You can also manually configure the web.xml file by adding the tags:
您还可以通过添加标签来手动配置 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>
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>.
该<servlet-mapping>
标记将匹配<url-pattern>
标记之间指定的任何 url 模式映射到指定的 servlet 类<servlet-name>.
This is an alternative to the annotation based solution. Hope it helps someone.
这是基于注释的解决方案的替代方案。希望它可以帮助某人。