jsp和servlet实现注册时写入不了数据库
我采用的是mvc模式Servlet接收reg.jap表单传来的参数,然后把该参数交给UserDaoImpl.java去执行具体的插入操作(我已经把insert业务逻辑写入了其中数据库连接也在里面,而且如果是单独测试这个java的话可以将数据写入数据库的),Servlet只把结果转向不同的页面即可,我怀疑是自己的servlet写错了,代码如下:
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import xhy.bbs.dao.IUserDao;
import xhy.bbs.dao.impl.UserDaoImpl;
import xhy.bbs.entity.UserEntity;
public class regServlet extends HttpServlet {
public regServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String uname=request.getParameter("uname");
String upass=request.getParameter("upass");
String gender=request.getParameter("gender");
String head=request.getParameter("head");
UserEntity user=new UserEntity();
user.setUid(1);
user.setUname(uname);
user.setUpass(upass);
user.setHead("image/head/"+head);
user.setGender(Integer.parseInt(gender));
String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
user.setRegTime(time);
IUserDao dao=new UserDaoImpl();
int i=dao.newOneUser(user);
if(i>0)request.getRequestDispatcher("login.jsp").forward(request,response);
else System.out.println("注册失败");
}
public void init() throws ServletException {
// Put your code here
}
}
PS:用tomcat运行的结果是运行到servlet时是一片空白,而数据没有被写入数据库
web.xml如下:<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>logionServlet</servlet-name>
<servlet-class>logionServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>regServlet</servlet-name>
<servlet-class>regServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logionServlet</servlet-name>
<url-pattern>/logionServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>regServlet</servlet-name>
<url-pattern>/regServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
请大家帮帮忙!