<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>"> <title>登陆失败</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 type="text/css" href="https://www.jb51.net/css/login.css"> </head> <body> <div> <div> <h1>输入用户名和密码不正确,请重新登陆!!!</h1> </div> <div> <button>返回</button> </div> </div> </body> </html>
2.6.2用户注册
注册首页register.jsp页面代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>"> <title>注册页面</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="content-type" content="text/html; charset=UTF-8"> <meta http-equiv="description" content="This is my page"> <link type="text/css" href="https://www.jb51.net/css/login.css"> <Script Language="JavaScript"> function check() { var tmp,str; str=document.myform.password1.value; tmp=document.myform.password2.value; if(str != tmp) alert("两次密码输入不一致,请重新确认密码!!!"); } </Script> </head> <body> <div> <div> <h1>欢迎来到简单购物车系统注册页面</h1> </div> <!-- 注册面板 --> <div> <form action="register_action.jsp" method="post"> <!-- 账号和密码组 --> <div> <label></label> <input type="text" placeholder="请输入注册账号"> </div> <div> <label></label> <input type="password" placeholder="请输入注册密码"> </div> <div> <label></label> <input type="password" placeholder="请确认注册密码"> </div> <!-- 注册按钮 --> <div> <button type="submit">注册</button> <button type="reset">重置</button> </div> </form> <div> <button>返回</button> </div> </div> </div> </body> </html>
注册处理脚本register_action.jsp代码如下;
<%@ page language="java" import="java.util.*,com.mongodb.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; String text_change = "等待注册"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>"> <title>My JSP 'register_action.jsp' starting page</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="Content-Type" content="text/html;charset=UTF-8"> <meta http-equiv="description" content="This is my page"> </head> <body> <% response.setContentType("text/html;charset=utf-8"); //确保显示的汉字信息以正确编码方式显示 request.setCharacterEncoding("utf-8"); //确保获取的汉字信息以正确编码方法获取 String userName1=(String)request.getParameter("username1"); //获取页面用户名 String passWord1=(String)request.getParameter("password1");//获取注册页面密码1 String passWord2=(String)request.getParameter("password2");//获取注册页面密码2 if(!passWord1.equals(passWord2)){ //如果用户两次输入密码不一致,则跳转到注册原页面register.jsp,即实现未跳转效果 response.sendRedirect("register.jsp"); } try{ // 连接到 mongodb 服务 MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); //此处采用无用户名和密码验证方式登陆 @SuppressWarnings("deprecation") DB db = mongoClient.getDB( "library" ); //连接到数据库library DBCollection coll = db.getCollection("userInfo"); //获取library数据库中集合userInfo System.out.println("Collection userInfo selected successfully"); DBObject user = new BasicDBObject();//定义一个Bson变量,用于存储注册的用户名和密码 user.put("username", userName1); user.put("password", passWord1); coll.insert(user); //向集合userInfo中插入注册用户信息 response.sendRedirect("register_success.jsp"); //注册成功后,自动跳转到注册成功提示页面 }catch(Exception e){ System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } %> </body> </html>
成功注册提示页面register_success.jsp页面代码如下: