本学期学习JSP和Servlet后,老师要求使用MVC模式下编写一个网页购物车,以下是一些主要代码及开发心得
这个是包的截图
先是注册页面,编写一个表单,表单方法调用另一个JSP页面(因为我这个页面是写的JSP页面链接数据库,你也可以直接使用DBDao连接数据库)
<form action="checkRegister.jsp" method="post" onsubmit="return validate1()">
<table width="300" height="180" border="5" bordercolor="#A0A0A0">
<tr>
<th>用户名:</th>
<td><input type="text" name="username" value="输入16个字符以内" maxlength="16" onfocus="if(this.value == '输入16个字符以内') this.value =''"></td>
</tr>
<tr>
<th>输入密码:</th>
<td><input type="text" name="password" value="输入20个字符以内" maxlength="20" onfocus="if(this.value == '输入20个字符以内') this.value =''"></td>
</tr>
<tr>
<th>确认密码:</th>
<td><input type="text" name="newword" value="请再次输入密码" maxlength="20" onfocus="if(this.value == '请再次输入密码') this.value =''"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="注 册"> <input type="reset" value="重 置"></td>
</tr>
</table>
</form>
checkregister.jsp
String username = new String(request.getParameter("username").getBytes("ISO8859_1"),"utf-8");
String password = new String(request.getParameter("password").getBytes("ISO8859_1"),"utf-8");
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/userdate?characterEncoding=utf-8";
String usename = "root";
String psw= "123456";
Connection conn = DriverManager.getConnection(url,usename,psw);//得到连接
PreparedStatement pStmt = conn.prepareStatement("select * from servlet where username = '" + username + "'");
ResultSet rs = pStmt.executeQuery();
if(rs.next()){
out.println("<script language='javascript'>alert('该用户已存在,请重新注册!');window.location.href='register.jsp';</script>");
}else{
PreparedStatement tmt = conn.prepareStatement("Insert into servlet values('" + username + "','" + password + "')");
int rst = tmt.executeUpdate();
if (rst != 0){
out