6、DAO工厂类:
package com.song.DAOFactory; import com.song.DAO.RegisterDAO; import com.song.Impl.RegisterImpl; public class RegisterFactory{ public static RegisterDAO getRegisterDAOInstance(){ return new RegisterImpl(); } }
7、一个servlet实现控制:
package com.song.servlet; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import com.song.VO.RegisterBean; import com.song.DAOFactory.RegisterFactory; public class RegisterServlet extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{ String username = req.getParameter("username"); String password = req.getParameter("password"); String email = req.getParameter("email"); RegisterBean rb = new RegisterBean(); rb.setUsername(username); rb.setPassword(password); rb.setEmail(email); try{ RegisterFactory.getRegisterDAOInstance().insert(rb); }catch(Exception e){ e.printStackTrace(); } res.setContentType("text/html;charset=GBK"); RequestDispatcher rd = req.getRequestDispatcher("register/register.jsp"); rd.forward(req,res); } }
8、JSP显示:
<%@ page contentType="text/html; charset=gbk"%> <html> <head> <title>DAO模式实现简单注册页面</title> </head> <body> <form action="/my/register" method="GET"> <center> 用户名:<input type="text" name="username" /><br> 密 码:<input type="password" name="password" /><br> 邮 箱:<input type="text" name="email" /><br> <input type="submit" value="注册" /> <input type="reset" value="重置" /> </center> </form> </body> </html>
时间: 2024-09-23 09:22:19