regist.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>Regist</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <center> <form action="doregist.jsp" method="post"> 用户名:<input type="text" name="uname" value=""><br> 密 码: <input type="password" name="upwd" value=""><br> email: <input type="text" name="email" value=""><br> <input type="submit" value="注册"> <a href="login.jsp">返回</a> </form> </center> </body> </html>
doregist.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="com.ql.dao.*" %> <%@page import="com.ql.entity.Users" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <% request.setCharacterEncoding("utf-8"); String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); String email = request.getParameter("email"); Users user = new Users(); user.setUsername(uname); user.setPassword(upwd); user.setEmail(email); MsgDao md = new MsgDao(); md.addUser(user); request.getRequestDispatcher("login.jsp").forward(request, response); %>
//注册方法insert public boolean addUser(Users user){ Connection con=null; try{ //获取连接 con = getConnection(); //书写SQL语句 String sql = "insert into Usersd(username,PASSWORD,email) values(?,?,?)"; PreparedStatement st =con.prepareStatement(sql); st.setString(1,user.getUsername()); st.setString(2, user.getPassword()); st.setString(3, user.getEmail()); int row = st.executeUpdate(); if(row>0){ //发送成功 return true; } closeAll(con, st, null); }catch(SQLException e){ e.printStackTrace(); } return false; }