尝试设置index.jsp的NullPointerException

时间:2013-10-09 03:37:09

标签: java spring jsp servlets spring-mvc

编辑:我可以通过将index.jsp重命名为hi.jsp来解决问题。任何人都知道我为什么会遇到文件名问题?

问题: 我使用Java / Spring编写了webapp的后端组件,并通过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文件有关。

1 个答案:

答案 0 :(得分: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:...

尝试将WEB-INF文件夹外的JSP移动到应用程序的根目录中。