【发布时间】:2016-12-26 05:10:36
【问题描述】:
我在 jsp 中创建了一个表单,并尝试使用 Spring JDBC 将表单详细信息发送到数据库。我创建了一个 JSP 表单、一个 servlet 文件 db_Servlet.java 以从表单和 db_Service.java 中获取数据,后者将数据发送到数据库。
当我使用 tomcat 运行项目时,它在创建 db_Service 类的对象时卡住了。 下面的代码是 db_Servlet.java 的 servlet doPost 方法。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("entered the servlet");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String ph = request.getParameter("phone");
long phone = Long.parseLong(ph);
String emailid = request.getParameter("emailid");
customer c= new customer(name,phone,emailid);
try {
System.out.println("entered the try block");
db_Service service = new db_Service();
int result = service.addtodb(c);
System.out.println(result);
String title = "Thank you";
String doctype = "<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(doctype +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body>" +
"Thank you for wasting your precious time"
+ " </body></html>"
);
}
finally {
out.close();
}
}
下面是db_Service类
public class db_Service {
public void addtodb(customer c){
System.out.println("Entered the service");
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
JDBCtemplate customerJDBCtemplate =
(JDBCtemplate)context.getBean("JDBCtemplate");
customerJDBCtemplate.create(c.getName(),c.getPhone(),c.getEmailid());
}
}
评论“进入try block”后不显示评论“进入服务”
【问题讨论】:
-
尝试捕获异常并打印出堆栈跟踪。
-
我尝试打印堆栈跟踪,但没有打印任何内容
-
不如使用调试器找出发生了什么
-
请告诉我你不是真的在使用这个代码...这会导致内存问题,奇怪的调试数据库问题等。所以除非你想使用你拥有的代码,否则正确使用依赖注入。
-
@M.Deinum 我是新手,你能告诉我代码的哪一部分会出现内存问题吗?
标签: java spring jsp servlets jdbc