javax.servlet.ServletException : Communications link failure

标签 java mysql jsp tomcat jdbc

我试过以下连接数据库的jsp代码。

Connection conn=null;
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                conn = DriverManager.getConnection("jdbc:mysql://localhost/TestDB?user=root&password=123");

                if (conn != null) {
                    out.println("connected");
                }
                else
                    {
                    out.println("connection failed");
                }

当我将此文件上传到一个文件夹中时,数据库连接正常。

但我在另一个测试文件夹中尝试了相同的文件,其中出现以下错误

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
sun.reflect.GeneratedMethodAccessor62.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
java.security.AccessController.doPrivileged(Native Method)
javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
java.security.AccessController.doPrivileged(Native Method)
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:738)
sun.reflect.GeneratedMethodAccessor152.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
java.security.AccessController.doPrivileged(Native Method)
javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:218)

root cause

javax.servlet.ServletException: Communications link failure

Last packet sent to the server was 0 ms ago.
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:841)
    org.apache.jasper.runtime.PageContextImpl.access$1100(PageContextImpl.java:64)
    org.apache.jasper.runtime.PageContextImpl$12.run(PageContextImpl.java:761)
    java.security.AccessController.doPrivileged(Native Method)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:759)
    org.apache.jsp.test_jsp._jspService(test_jsp.java:71)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    sun.reflect.GeneratedMethodAccessor62.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
    java.security.AccessController.doPrivileged(Native Method)
    javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
    org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
    org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
    java.security.AccessController.doPrivileged(Native Method)
    org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:738)
    sun.reflect.GeneratedMethodAccessor152.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
    java.security.AccessController.doPrivileged(Native Method)
    javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
    org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
    org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:218)

任何人都可以帮助我解决问题。

最佳答案

因此,您将直接在 JSP 中创建数据库连接,包括在 View 中对您的凭据进行硬编码 - 您永远不应该这样做!然后你没有在任何异常处理中包装你的代码,这就是为什么你得到一个 ServletException 作为一般错误消息,真正的原因是未知的 - Servlet Container 在 try...catch block 中包装 JSP,捕获发生的任何错误并抛出一个 ServletException。

我也看不到你在哪里关闭了你的连接,但你确实关闭了,不是吗?

首先,将凭据放在 View 中是非常糟糕的主意,其次,您永远不知道有多少人会访问您的页面,因此您很可能会溢出数据库连接。所以首先将你的数据库连接细节放在 JNDI 中,使用连接池而不是自己创建连接(你仍然必须关闭池连接,以便连接返回池,除非使用单个共享连接,这使得不closing connections not so wrong)并控制你的错误,这样你就可以看到如果出现问题会发生什么。

Tomcat(如我们所见,您使用的)包括连接池和 JNDI,只需使用它们即可。您可以在这里找到更多信息:Tomcat JNDI HowTo

关于javax.servlet.ServletException : Communications link failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5786251/

相关文章:

java - 如何在 DAO 中传递对象字段?

mysql - 使用主键分块从大表中删除时仍然看到锁定等待超时

java - Spring 的安全。授权不适用于jsp

java - JAX-RS 2.0 API 支持调用其客户端

java - API 需要 2 分钟加载数据。想要使用缓存

java - 如何将Spring Boot应用程序指向外部jar的logback.xml

MySQL选择最高评价

php - Mysql 找到 result_id where name = "x"然后删除共享结果 id 和名称 ="y"的其他条目

java - 在注销 Servlet 中使 session 无效

javascript - 如何从 iframe 打开弹出窗口