web.xml中的register部分如下:RegisterServlet servlet.RegisterServlet RegisterServlet /registerServlet Register.java代码段如下publicclassRegisterServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterEncoding("gb2312");response.setContentType("gb2312");Stringusername=request.getParameter("username");Stringpwd=request.getParameter("pwd");if(newDB().insertUser(username,pwd)){response.sendRedirect("login.jsp");}}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doGet(request,response);}运行之后,浏览器没啥反应,我估计是Register.java中的if判断语句出现了问题,于是我添加了如下代码:if(newDB().insertUser(username,pwd)){response.sendRedirect("login.jsp");}else{response.sendRedirect("success.jsp");}果然返回了success.jsp界面。其中register.jsp代码段如下:
register name: Password: 其中DB.java的数据库连接代码段如下:Connectioncon;PreparedStatementpstmt;publicDB(){StringdriverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";Stringurl="jdbc:sqlserver://localhost:1433;DatabaseName=JSP";Stringusername="load";Stringpassword="hww";//加载驱动程序以连接数据库try{Class.forName(driverName);con=DriverManager.getConnection(url,username,password);}//捕获加载驱动程序异常catch(ClassNotFoundExceptione){System.err.println("加载JDBC/ODBC驱动程序失败。");e.printStackTrace();System.exit(1);}//捕获连接数据库异常catch(SQLExceptione){System.err.println("无法连接数据库");e.printStackTrace();System.exit(1);}}判断是否添加注册用户的代码段如下:publicbooleaninsertUser(Stringusername,Stringpwd){try{pstmt=con.prepareStatement("insertinto[userTable]values(?,?)");pstmt.setString(1,username);pstmt.setString(2,pwd);pstmt.executeUpdate();returntrue;}catch(Exceptione){e.printStackTrace();returnfalse;}}请问在哪里出错了,搞不明白。谢谢。
相关分类