[英]Simple servlet project (HTTP Status 404 error)
我想創建一個servlet項目。
我的Java類叫做AzpanaServlet.java
,它包含一個內部類。 (當我編譯它時,我有2個類文件)。
我的項目是一個簡單的應用程序,它接收一個字符串輸入並用它做一些事情(不相關)。
當我按下"submit"
按鈕時,我收到以下錯誤:
HTTP Status 404 - /AzpanaServlet
Type Status report
Message /AzpanaServlet
Description The requested resource (/AzpanaServlet) is not available.
Apache Tomcat/6.0.18
請盡可能幫助我,我無法解決這么多時間。 這是我的Java代碼:
public class AzpanaServlet extends HttpServlet {
//
//Some functions
//
//Inner class: public class oneChar{...}
//
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* Get the value of form parameter
*/
String name = request.getParameter("name");
/*
* Set the content type(MIME Type) of the response.
*/
response.setContentType("text/html");
String str = "";
PrintWriter out = response.getWriter();
ArrayList<Integer> list = new ArrayList<Integer>();
try {
list = mainmanu(name); //not relevant function.
} catch (Exception e) {
str = e.toString();
e.printStackTrace();
}
/*
* Write the HTML to the response
*/
out.println("<html>");
out.println("<head>");
out.println("<title> this is your answers</title>");
out.println("</head>");
out.println("<body>");
if(str != ""){
out.println(str);
}
else{
for(int i = 0;i<=40;i++){
out.println(list.get(i));
out.println("<br>");
}
}
out.println("<a href='form.html'>"+"Click here to go back to input page "+"</a>");
out.println("</body>");
out.println("</html>");
out.close();
}
public void destroy() {
}
}
我的web.xml
代碼:
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>AzpanaServlet</servlet-name>
<servlet-class>com.example.AzpanaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AzpanaServlet</servlet-name>
<url-pattern>/AzpanaServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/form.html</welcome-file>
</welcome-file-list>
</web-app>
我的form.html
代碼:
<html>
<head>
<title>Zeev's Project</title>
</head>
<body>
<h1>Just Enter String</h1>
<form method="POST" action="AzpanaServlet">
<label for="name">Enter String</label>
<input type="text" id="name" name="name"/><br><br>
<input type="submit" value="Submit Form"/>
<input type="reset" value="Reset Form"/>
</form>
</body>
</html>
文件夾的層次結構如下:
ROOT[
WEB-INF[
web.xml
classes[
com[
example[
AzpanaServlet.class
AzpanaServlet$oneChar.class
]
]
]
lib[
AzpanaServlet.java
]
]
META-INF[
MANIFEST.MF
]
form.html
]
復制了您的確切代碼,並注釋掉了Servlet代碼
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* Get the value of form parameter
*/
String name = request.getParameter("name");
System.out.println("Parameter name : "+name);
奇跡般有效。
在我的服務器控制台中
Parameter name : helloo
您的配置沒有問題。 您可能希望清理瀏覽器緩存並重試。
問題可能是您使用IDE運行Tomcat服務器並部署到根應用程序上下文。 嘗試在部署中為應用程序使用某些應用程序上下文。 例如/myapp
。
看看您的IDE控制台或您的tomcat日志,您的Web應用程序是否已成功啟動?
它可能由於某種原因而無法啟動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.