2013-10-09 44 views
1

編輯:我能夠通過將index.jsp重命名爲hi.jsp來解決此問題。任何人都知道我爲什麼會遇到文件名問題?NullPointerException嘗試獲取index.jsp設置

問題: 我寫了使用Java /春web應用程序的後端組件,並通過Spring註解迷上它到Tomcat:

@Controller 
public class WorkerSubmitterController { 
    @RequestMapping(value = {"/start"}, method = RequestMethod.GET, produces = "text/plain") 
    @ResponseBody 
    public ResponseEntity<String> start() throws Exception { 
     return new ResponseEntity<String>("Started", HttpStatus.OK); 
    } 
} 

我配置Servlet是這樣的:

public class WebInitializer implements WebApplicationInitializer { 
    @Override 
    public void onStartup(ServletContext container) { 
     // Create the dispatcher servlet's Spring application context 
     AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); 
     dispatcherContext.scan("com.myProject.someName"); 

     // Manage the lifecycle of the root application context 
     container.addListener(new ContextLoaderListener(dispatcherContext)); 

     // Register and map the dispatcher servlet 
     ServletRegistration.Dynamic dispatcher = 
     container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); 

     dispatcher.setLoadOnStartup(1); 
     Set<String> mappingConflicts = dispatcher.addMapping("/"); 
    } 

} 

這工作正常當我去localhost:8080/start

但是,現在我試試通過獲得index.jsp工作來編寫前端。我創建了一個index.jsp文件/webapp/WEB-INF/下有以下情況:

<html> 
<head> 
    <title>Hello world</title> 
</head> 
<body> 

我加了web.xml文件:

<web-app id="WebApp_ID" 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>home</servlet-name> 
     <jsp-file>/index.jsp</jsp-file> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>home</servlet-name> 
     <url-pattern>/home</url-pattern> 
    </servlet-mapping> 

</web-app> 

當我訪問localhost:8080/home 我得到這個錯誤:

HTTP ERROR 404 

Problem accessing /home. Reason: 

    Not Found 
Powered by Jetty:// 

和服務器端顯示:

2013-10-08 20:27:31.652:WARN:oejs.ServletHandler:/home 
java.lang.NullPointerException 
    at java.net.URLEncoder.encode(URLEncoder.java:205) 
    at java.net.URLEncoder.encode(URLEncoder.java:171) 
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:474) 
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:378) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:848) 
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:669) 
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:457) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137) 
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557) 
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231) 
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1075) 
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:384) 
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193) 
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1009) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135) 
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255) 
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154) 
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116) 
    at org.eclipse.jetty.server.Server.handle(Server.java:368) 
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:489) 
    at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:942) 
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:1004) 
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:640) 
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235) 
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82) 
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628) 
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543) 
    at java.lang.Thread.run(Thread.java:724) 

我在做什麼錯?我錯過了讓jsp頁面工作的步驟嗎? index.jsp不正確?我試過修改index.jsp只是hello,彈出同樣的錯誤。另外,我已經試過移動index.jsp文件,找到問題的根源,並且錯誤消失,但後來我得到

Oct 08, 2013 9:58:38 PM org.apache.jasper.servlet.JspServlet serviceJspFile 
SEVERE: PWC6117: File "%2FUsers%2FuserName%2Fpersonal%2Friver%2Fsrc%2Fmain%2Fwebapp%2FWEB-INF%2Findex.jsp" not found 

所以我想這個問題是關係到我的JSP文件。

+0

它似乎是一個警告,而不是一個錯誤。你沒有聲明文件的編碼? – Floris

+0

我曾經在index.jsp的頂部有<%@ page contentType =「text/html; charset = UTF-8」language =「java」%>,但它仍然給我提供了相同的錯誤 – Popcorn

+0

我也是嘗試添加 Popcorn

回答

2

問題是您試圖訪問WEB-INF目錄中的JSP,該目錄明顯無法訪問。而你正試圖從根本上訪問它,這就是爲什麼你的錯誤HTTP ERROR 404

However, now I'm trying to write the frontend by getting index.jsp working. I created a index.jsp file under /webapp/WEB-INF/ with the following:...

嘗試將JSP的WEB-INF文件夾外到您的應用程序的根。

+0

是的,這是它!謝謝 – Popcorn

+1

@Popcorn作爲附加信息。它也**是一種廣泛的安全實踐(當然,如果你需要的話)有意將JSP文件放入WEB-INF中,以防止直接從客戶端獲取。在這種情況下,您應該使用某個servlet,並在請求發送到指定的URL時首先被訪問,並且在該servlet中使用[RequestDispatcher](http://docs.oracle.com/javaee/7/api /javax/servlet/RequestDispatcher.html)轉發到所需的JSP,比如'request.getRequestDispatcher(「/ WEB-INF/jsp/index.jsp」)。forward(request,response);' – informatik01

相關問題