2015-12-11 41 views
0
index.jsp 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>REGISTRATION</title> 
</head> 
<body> 
<form action="reg" method="post"> 
Name: <input type="text" name="user_name"><br> 
EMAIL: <input type="text" name="user_email"><br> 
PASSWORD: <input type="text" name="user_pass"><br> 
<input type="submit"><br> 
</form> 
</body> 
</html> 



reg.java 


import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.sql.Statement; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

/** 
* Servlet implementation class reg 
*/ 
@WebServlet("/reg") 
public class reg extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 
    *  response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     // TODO Auto-generated method stub 
     response.getWriter().append("Served at: ").append(request.getContextPath()); 
    } 

    /** 
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 
    *  response) 
    */ 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html"); 
     PrintWriter out = response.getWriter(); 

     String user_name = request.getParameter("user_name").toString(); 
     String user_email = request.getParameter("user_email").toString(); 
     String user_pass = request.getParameter("user_pass").toString(); 

      Connection con=null; 
      try { 
       Class.forName("com.mysql.jdbc.driver"); 
       con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root","root123"); 
       Statement st=con.createStatement(); 
       int c=st.executeUpdate("insert into test values('"+user_name+"','"+user_email+"','"+user_pass+"')"); 
       if(c>0){ 
        out.println("REGISTERD"); 
       } 
      } catch (ClassNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
    } 

} 

顯示異常 一切都在項目..Connector,數據庫,表,如果簡單的Java代碼的用戶相同的代碼運行它,但它的servlet顯示ClassNotFoundException的 幫我。 謝謝!JSP JDBC SERVLET顯示ClassNotFound的例外....一切是正確的

以下是例外

拋出java.lang.ClassNotFoundException:com.mysql.jdbc.driver在 org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333) 在 組織.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167) at java.lang.Class.forName0(Native Method)at java.lang.Class.forName(Unknown Source)at reg.doPost(reg。 java:47) at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)at javax.servlet.http.Htt pServlet.service(HttpServlet.java:729)在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206 ) 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 在 有機apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 在 org.apache.catalina.core .StandardHostValve.invoke(StandardHostValve.java:141) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 在 org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java :616) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521) 在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096) 在 org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:674) 在 org.apache.tomcat .util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1500) at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1456) at java.util.concurrent。 ThreadPoolExecutor.runWorker(來源不明) 在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(來源不明) 在 org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)

+0

您需要mysql連接器。下載mysql jdbc driver .jar並添加項目庫文件夾。 –

回答

0

如果它從Eclipse運行,這意味着您的項目的類路徑中有MySql jdbc dirver jar,但它不可用於您的Web應用。

項目類路徑(構建路徑)與部署時的Web應用程序的類路徑不同。並非您的項目類路徑上的所有jar都會自動導出。

通常,WEB-INF/lib中的罐子將可用於Web應用程序,但其外的罐子不會。

您應該將MySQL JDBC驅動程序的jar在WEB-INF/lib,或它(在你的情況下,由異常堆棧跟蹤判斷的Tomcat)添加到您的Servlet容器的lib文件夾。