我在 JDK1.8 上的 Netbeans 11 中创建了一个简单的 Web 应用程序,并运行 Glassfish 5.1.0,只有一个重定向到一个简单 JSP 页面的 servlet。
在构建和运行应用程序时,当我访问http://localhost:8080/MyNews/news时出现上述错误。堆栈跟踪在这里
visiting unvisited references|#]
visiting unvisited references|#]
visiting unvisited references|#]
Loading application [MyNews] at [/MyNews]|#]
Loading application MyNews done in 161 ms|#]
GlassFish Server Open Source Edition 5.1.0 (default-private) startup time : Felix (3,830ms), startup services(6,567ms), total(10,397ms)|#]
visiting unvisited references|#]
Grizzly Framework 2.4.4 started in: 62ms - bound to [/0.0.0.0:7676]|#]
Registered com.sun.enterprise.glassfish.bootstrap.osgi.EmbeddedOSGiGlassFishImpl@77ee25f1 as OSGi service registration: org.apache.felix.framework.ServiceRegistrationImpl@6dd93a21.|#]
Created HTTP listener http-listener-2 on host/port 0.0.0.0:8181|#]
JMXStartupService has started JMXConnector on JMXService URL service:jmx:rmi://DESKTOP-QQAVGAD:8686/jndi/rmi://DESKTOP-QQAVGAD:8686/jmxrmi|#]
Grizzly Framework 2.4.4 started in: 13ms - bound to [/0.0.0.0:8181]|#]
Created HTTP listener http-listener-1 on host/port 0.0.0.0:8080|#]
Grizzly Framework 2.4.4 started in: 12ms - bound to [/0.0.0.0:8080]|#]
visiting unvisited references|#]
visiting unvisited references|#]
visiting unvisited references|#]
visiting unvisited references|#]
visiting unvisited references|#]
visiting unvisited references|#]
visiting unvisited references|#]
visiting unvisited references|#]
Loading application [MyNews] at [/MyNews]|#]
MyNews was successfully deployed in 404 milliseconds.|#]
Initializing Mojarra 2.3.9 for context ''|#]
Loading application [__admingui] at [/]|#]
Loading application __admingui done in 2,712 ms|#]
Context path from ServletContext: differs from path from bundle: /|#]
WebModule[/MyNews] ServletContext.log():Marking servlet ViewNews as unavailable|#]
StandardWrapperValve[ViewNews]: Allocate exception for servlet ViewNews
java.lang.RuntimeException:
at view.ViewNews.<init>(ViewNews.java:1)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:320)
at com.sun.enterprise.web.WebContainer.createServletInstance(WebContainer.java:725)
at com.sun.enterprise.web.WebModule.createServletInstance(WebModule.java:1955)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1262)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1069)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:119)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:611)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:550)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:75)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:114)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:199)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:439)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:144)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:182)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:156)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:218)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:95)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:260)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:177)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:109)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:88)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:53)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:515)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:89)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:94)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:33)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:114)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:569)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:549)
at java.lang.Thread.run(Thread.java:748)
|#]
这是小服务程序
package view;
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;
/**
*
* @author korla
*/
@WebServlet(name = "ViewNews", urlPatterns = {"/news"})
public class ViewNews extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("news.jsp");
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
这是JSP
<%--
Document : news
Created on : 03-May-2020, 4:54:15 PM
Author : korla
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
添加的库:javax.servlet.jsp-2.2.0.jar javax.servlet-api-4.0.1.jar
我在运行一个成熟的应用程序时遇到了同样的错误,我最近在 JDK-11 上用 Tomcat9.0 重建了这个应用程序(没有上面的 jars,因为 tomcat 带有它们)。将该应用程序也转移到 JDK-8 和 Glassfish(添加了这些 jar),但仍然出现相同的错误。