2016-04-14 93 views
0

我創建了一個示例項目來測試jspProject looks like thistomcat無法顯示JSP,錯誤:請求的資源不可用

我正在使用tomcat 9,Java SE 8_73。該項目的網頁模塊版本爲3.1,未生成web.xml。該

Java代碼看起來像這樣

package pac; 

import java.io.IOException; 
import java.io.PrintWriter; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

/** 
* Servlet implementation class JSPProject 
*/ 
@WebServlet("/jspproject") 
public class JSPProject extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     PrintWriter cut = response.getWriter(); 
     cut.println("hello world"); 
    } 
} 

,但在服務器上運行的JSPProject.java時,下面顯示錯誤消息。

HTTP Status 404 - /DynamicJSP/jspproject 
type Status report` 
message /DynamicJSP/jspproject 
description The requested resource is not available. 
The requested resource is not available. 
+0

而不是'service'方法使用'保護無效的doGet(HttpServletRequest的請求,HttpServletResponse的響應)拋出ServletException異常,IOException {}' – Naman

+0

無法正常工作。即時消息相同的錯誤 – CoderS

+0

嘗試清理項目並重新啓動tomcat。 – Naman

回答

-1

您應該重寫從要擴展這樣的HttpServletdoGet方法:

@Override 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
      throws ServletException, IOException { 
    try (ServletOutputStream cut = resp.getOutputStream()) { 
     cut.println("hello world"); 
     cut.flush(); 
    } 
0
if you have changed your method to doGet and after that also it doesn't works then you need to make changes in your web.xml file. Pls look at below example : 

<servlet> 
<servlet-name>HelloServlet</servlet-name> (this is any name of your choice) 
<servlet-class>examples.Hello</servlet-class>(This is Java class implements the servlet) 
</servlet> 
<servlet-mapping> 
<servlet-name>HelloServlet</servlet-name>(this name should exactly the same name as above , used for mapping) 
<url-pattern>/hello</url-pattern> (This is URL pattern that invokes the servlet in your browser) 
</servlet-mapping> 
相關問題