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();
}
}
}
显示例外情况 一切都在项目..连接器,数据库,表如果用户相同的代码,它运行简单的Java代码但在servlet中显示ClassNotFoundException 帮我。 谢谢!
以下是例外
java.lang.ClassNotFoundException:com.mysql.jdbc.driver at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333) 在 org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167) at java.lang.Class.forName0(Native Method)at reg.doPost中的java.lang.Class.forName(未知来源)(reg.java:47) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:648)at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)at at 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) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) 在 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) 在 org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1456) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source) 在 org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在java.lang.Thread.run(未知来源)
答案 0 :(得分:0)
如果它在Eclipse中运行,则表示您在项目的类路径中有MySql jdbc dirver jar,但它不适用于您的Web应用程序。
项目类路径(构建路径)与部署时Web应用程序的类路径不同。并非所有项目类路径上的jar都会自动导出。
通常WEB-INF/lib
中的jars可用于Web应用程序,但不包含其中的jar。
您应该将MySql jdbc驱动程序jar放在WEB-INF/lib
中,或者将其添加到Servlet容器的lib文件夹中(在您的情况下通过异常堆栈跟踪判断Tomcat)。