An error that has plagued me for a long time-it turned out to be a JSP connection to the MySQL database.

Source: Internet
Author: User

When running a JSP system, the following error is always reported:
HTTP status 500-

TypeException report

Message

Description The server encountered an internal error () that prevented it from fulfilling this request.

Exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:395)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Root Cause

java.lang.NullPointerException
org.apache.jsp.first_jsp._jspService(first_jsp.java:98)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.

However, after refresh several times, or after a period of time, the system runs normally. After finding a lot of information, we finally got the result. The solution benefited from the following prompt:
1.

Quote: Java. Lang. nullpointerexception
Note the full stack trace of the root cause is available in the Tomcat logs.

View log
Check your JSP page
2. java. Lang. nullpointerexception is a runtime exception. It indicates that your code is accessing an object with a null value during running.
Org. Apache. jsp. pass_jsp. _ jspservice (pass_jsp.java: 66) indicates the location of the error statement.
Find the file pass_jsp.java In the tomcat installation directory to see what line 66th is.
3. after reading the code, I think con is null during running, so statement stmt = con. createstatement: The exception occurs. The reason con is null is probably connection con = yy. getconn (); here. That is to say, the getconn method of YY returns a null value. Solution: Determine the con before using con, or catch exceptions using try catch.

Later, I checked Tomcat's logs and found a stdout_20070104.log file in logs, which reported the log of the current day, which contains the following information:
Java. SQL. sqlexception: communication failure during handshake. Is there a server running on localhost: 3306?
Unable to load driver
Java. SQL. sqlexception: communication failure during handshake. Is there a server running on localhost: 3306?
Unable to load driver
Java. SQL. sqlexception: communication failure during handshake. Is there a server running on localhost: 3306?
Unable to load driver
Java. SQL. sqlexception: communication failure during handshake. Is there a server running on localhost: 3306?
Unable to load driver

After thinking about the database connection file opendb. Java, rewrite this class and write the code as follows:
Import java. SQL .*;
Import java. Io .*;

Public class opendb
{
String host = "localhost ";
String database = "netshop ";
String username = "root ";
String userpassword = "*****"; // The password username must be consistent with the password and username of the database in use.
Private string drivername = "org. gjt. Mm. MySQL. Driver ";
Private string url = "JDBC: mysql: //" + host + "/" + database + "? Useunicode = true & characterencoding = gb2312 ";

Connection dbconn;
Public opendb ()
{

}
Public connection getconnection ()
{
Try
{
Class. forname (drivername). newinstance ();
Dbconn = drivermanager. getconnection (URL, username, userpassword );
}
Catch (exception ex)
{
System. Out. println (ex. tostring ());
Dbconn = NULL;
System. Out. println ("unable to load driver ");
}

Return dbconn;
}

}

After exploration, it is found that the system will run normally every time the opendb. Java file is re-compiled after Tomcat is started or started.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.