简体   繁体   中英

javax.servlet.ServletException: Communications link failure

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.

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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