javax.servlet.ServletException:通信链接失败

发布于 2024-11-03 18:31:11 字数 4517 浏览 0 评论 0原文

我尝试了以下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)

任何人都可以帮助我纠正问题。

I have tried the following jsp code for connecting the database.

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");
                }

The database get connected properly when i upload this file in one folder.

But i tried the same file with another test folder where,am getting the following error

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)

Can any help me in rectifying the issues.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So尛奶瓶 2024-11-10 18:31:11

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

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

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

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

So you are creating a database connection directly in the JSP, including hard coding your credentials in the view - you should never do that! Then you didn't wrap your code in any exception handling, this is why you got a ServletException as a general error message and the real cause is unknown - Servlet Container wrap JSP in try...catch block, catch whatever error happens and throw a ServletException.

I also cannot see where you close your connection, but you do close, don't you?

First it is very bad idea to put credentials in the view, second you never know how many people will access your page so you will likely overflow your database connections. So first of all put your database connection details in JNDI, use connection pool instead of creating connection yourself (still you will always have to close the pooled connections so that connections get back to the pool except when using a single shared connection, which makes not closing connections not so wrong) and take control over your errors, so that you can see what happens if things go wrong.

Tomcat (what you use as we can see) includes both connection pooling and JNDI, just use them. Here you'll find more: Tomcat JNDI HowTo

梦回梦里 2024-11-10 18:31:11

链接失败通常意味着尝试与服务通信时出现问题。也就是说,您的 jsp 似乎无法在连接级别连接到数据库。您的数据库实例是否在本地主机上侦听?您是否设置了防火墙来阻止 jsp 和数据库实例之间的通信?

Link Failure usually means that there was a problem trying to communicate with a service. That said, it seems like your jsp couldn't connect to your database on a connection level. Do you have your DB instance listening on localhost? Do you have firewalls in place that's preventing communication between your jsp and your DB instance?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文