我正在尝试开发一个显示表单的servlet页面,用户将填写表单并发布到servlet本身(问题)。我粘贴了下面的所有代码。当我调用servlet时,页面显示正常,我可以填写表单但是当我单击提交并发布它时,程序进入一个循环,其中mysql insert命令无限期运行,我被迫停止程序。
问题似乎来自这条线:
request.getRequestDispatcher("/problem").include(request, response);
然而,这是我发布页面后我看到再次调用servlet的唯一方法,但不知何故,这是创建一个程序循环。知道为什么会这样吗?
下面是我的servlet:
@WebServlet(description = "Problem Page", urlPatterns = { "/problem" })
public class ProblemPage extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ProblemPage() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String problemID = request.getParameter("pbid");
String option = request.getParameter("opt");
String productID = request.getParameter("pdid");
if(problemID==null && option==null && productID==null) {
PrintWriter out = response.getWriter();
try {
for(String printLine:(new Problem()).printProblemAdd()) {
out.print(printLine);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if(problemID!=null && option!=null && productID==null) {
if(option.equals("edit")) {
request.getRequestDispatcher("problem").include(request, response);
} else if(option.equals("del")) {
} else {
PrintWriter out = response.getWriter();
out.print("<p>Page is invalid.</p>");
}
} else if(problemID==null && option==null && productID!=null) {
//Lists problems for product
Problem problem = new Problem(productID);
PrintWriter out = response.getWriter();
try {
for(String printLine:problem.print()) {
out.print(printLine);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
//Page is invalid
PrintWriter out = response.getWriter();
out.print("<p>Page is invalid.</p>");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String Url = "jdbc:mysql://localhost:3306/";
String DbName = "knowledge";
String Driver = "com.mysql.jdbc.Driver";
String UserName = "root";
String Password = "";
Connection Conn = null;
try {
Class.forName(Driver).newInstance();
Conn = (Connection) DriverManager.getConnection(Url + DbName,UserName,Password);
//St = (Statement) Conn.createStatement();
String mysqlQuery = "INSERT INTO problems (ID,productID,description) VALUES (?,?,?);";
PreparedStatement updateStatement = (PreparedStatement) Conn.prepareStatement(mysqlQuery);
updateStatement.setInt(1,0);
updateStatement.setString(2, request.getParameter("productid"));
updateStatement.setString(3, request.getParameter("description"));
int Res = updateStatement.executeUpdate();
request.setAttribute("message", Res>0?"Record added: " + request.getParameter("description"):"Record failed");
Conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
Conn.close();
} catch(Exception e) {
e.printStackTrace();
}
}
request.getRequestDispatcher("/problem").include(request, response);
}
}