简体   繁体   English

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

[英]javax.servlet.ServletException: Communications link failure

I have tried the following jsp code for connecting the database. 我尝试了以下用于连接数据库的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");
                }

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. 有什么可以帮助我纠正问题的。

So you are creating a database connection directly in the JSP, including hard coding your credentials in the view - you should never do that! 因此,您将直接在JSP中创建数据库连接,包括在视图中对凭据进行硬编码-绝对不要这样做! 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. 然后,您没有在任何异常处理中包装代码,这就是为什么收到ServletException作为一般错误消息并且真正原因未知的原因-Servlet容器将JSP包装在try ... catch块中,捕获发生的任何错误并抛出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. 因此,首先将您的数据库连接详细信息放入JNDI中,请使用连接池而不是自己创建连接(仍然需要始终关闭池化的连接,以便连接返回池中,除非使用单个共享连接,否则关闭连接并不是很错误),并控制错误,以便您可以查看出现问题的情况。

Tomcat (what you use as we can see) includes both connection pooling and JNDI, just use them. Tomcat(您将看到的用法)包括连接池和JNDI,只需使用它们即可。 Here you'll find more: Tomcat JNDI HowTo 在这里您将找到更多: Tomcat JNDI HowTo

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. 就是说,您的jsp似乎无法在连接级别连接到数据库。 Do you have your DB instance listening on localhost? 您的数据库实例是否在localhost上侦听? Do you have firewalls in place that's preventing communication between your jsp and your DB instance? 您是否有防火墙阻止了jsp和数据库实例之间的通信?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM