Servlet1
import java.sql.*;
import java.io.*;
import javax.servlet.*;
public class Servlet1 extends GenericServlet{
Connection con=null;
Statement st=null;
public void init()
{
try{
ServletConfig scfg=getServletConfig();
Class.forName(scfg.getInitParameter("driver"));
String url=scfg.getInitParameter("url");
String un=scfg.getInitParameter("user");
String psd=scfg.getInitParameter("password");
con=DriverManager.getConnection(url,un,psd);
}
catch(Exception e)
{
System.out.println(e);
}
}
public void service(ServletRequest req,ServletResponse res)
{
try{
String name=req.getParameter("na");
String uname=req.getParameter("un");
String password=req.getParameter("pa");
st=con.createStatement();
st.executeUpdate("insert into general values('"+name+"','"+uname+"','"+password+"')");
RequestDispatcher rd=req.getRequestDispatcher("Servlet2");
rd.forward(req,res);
}
catch(Exception e)
{
System.out.println(e);
}
}
public void destroy()
{
try{
con.close();
st.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Servlet2
import java.io.*;
import java.sql.*;
import javax.servlet.*;
public class Servlet2 extends GenericServlet
{
ServletContext scxt=getServletContext();
Statement st=null;
Connection con=null;
public void init()
{
try{
Class.forName(scxt.getInitParameter("driver"));
String url=scxt.getInitParameter("url");
String un=scxt.getInitParameter("user");
String psd=scxt.getInitParameter("password");
con=DriverManager.getConnection(url,un,psd);
}
catch(Exception e)
{
System.out.println(e);
}
}
public void service(ServletRequest req,ServletResponse res)
{
try{
String mobileno=req.getParameter("mo");
String email=req.getParameter("em");
String address=req.getParameter("ad");
Statement st=con.createStatement();
int i=st.executeUpdate("insert into contacts values('"+mobileno+"','"+email+"','"+address+"')");
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
if(i>0)
{
pw.println("<html><body><h1>Succesfully Registered</h1></body></html>");
}
else
{
pw.println("<html><body><h1>Registration Failed</h1></body></html>");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public void destroy()
{
try{
con.close();
st.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
的web.xml
<web-app>
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-class>Servlet1</servlet-class>
<init-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost/chakri</param-value>
</init-param>
<init-param>
<param-name>user</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>chakri123</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/srv1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Servlet2</servlet-name>
<servlet-class>Servlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet2</servlet-name>
<url-pattern>/Servlet2</url-pattern>
</servlet-mapping>
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost/chakri</param-value>
</context-param>
<context-param>
<param-name>user</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>chakri123</param-value>
</context-param>
</web-app>
我的文件夹结构也正确错误的原因是什么,因为我的Servlet1工作正常并将值插入数据库(javax.servlet.ServletException:实例化servlet类Servlet2时出错)。