gpt4 book ai didi

java - NullPointerException 尝试设置index.jsp

转载 作者:行者123 更新时间:2023-11-30 04:12:37 25 4
gpt4 key购买 nike

编辑:我能够通过将 index.jsp 重命名为 hi.jsp 来解决该问题。有人知道为什么我遇到文件名问题吗?

问题:我使用 Java/Spring 编写了 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 工作来编写前端。我在 /webapp/WEB-INF/ 下创建了一个 index.jsp 文件,内容如下:

<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 文件有关。

最佳答案

问题是您正在尝试访问位于WEB-INF目录中的JSP,该目录显然无法访问。并且您尝试从根访问它,这就是为什么您收到错误 HTTP ERROR 404

但是,现在我正在尝试通过使index.jsp 工作来编写前端。我在/webapp/WEB-INF/下创建了一个 index.jsp 文件,其中包含以下内容:...

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

关于java - NullPointerException 尝试设置index.jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19262646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com