首页 > 代码库 > 微博小程序 版本一
微博小程序 版本一
先进行演示再看代码
登录网站首页
点击还不是会员
输入合法的地址 名称 密码
返回再次输入
注册成功实际上是在电脑的盘符中建立文件夹 文件夹里面有个profile文件 这里暂时没有用到数据库
如果注册过程中存在错误 就会打印错误信息
登陆
登陆成功
程序组成如下
静态网页 两个
登录页面
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Gossip 微网志</title> <link rel="stylesheet" href="css/index.css" type="text/css"> </head> <body> <div id="loginPanel"> <div> <img src=‘images/caterpillar.jpg‘ alt=‘Gossip 微网志‘/> </div> <a href=‘register.html‘>还不是会员?</a> <p> <form method=‘post‘ action=‘login.do‘> <table bgcolor=‘#cccccc‘> <tr> <td colspan=‘2‘>会员登录</td> <tr> <td>名称:</td> <td><input type=‘text‘ name=‘username‘></td> </tr> <tr> <td>密码:</td> <td><input type=‘password‘ name=‘password‘></td> </tr> <tr> <td colspan=‘2‘ align=‘center‘><input type=‘submit‘ value=‘登入‘></td> </tr> <tr> <td colspan=‘2‘><a href=‘forgot.html‘>忘记密码?</a> </td> </tr> </table> </form> </div> <div> <h1>Gossip ... XD</h1> <ul> <li>谈天说地不奇怪 <li>分享讯息也可以 <li>随意写写表心情 </ul> </div> </body> </html>
注册页面
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Gossip 微网志</title> </head> <body> <h1>会员注册</h1> <form method=‘post‘ action=‘register.do‘> <table bgcolor=#cccccc> <tr> <td>邮件地址:</td> <td><input type=‘text‘ name=‘email‘ size=‘25‘ maxlength=‘100‘> </td> </tr> <tr> <td>名称(最大16字符):</td> <td><input type=‘text‘ name=‘username‘ size=‘25‘ maxlength=‘16‘> </td> </tr> <tr> <td>密码(6到16字符):</td> <td><input type=‘password‘ name=‘password‘ size=‘25‘ maxlength=‘16‘> </td> </tr> <tr> <td>确认密码:</td> <td><input type=‘password‘ name=‘confirmedPasswd‘ size=‘25‘ maxlength=‘16‘> </td> </tr> <tr> <td colspan=‘2‘ align=‘center‘><input type=‘submit‘ value=‘注册‘> </td> </tr> </table> </form> </body> </html>
index的css 图片就不给出来了
body { font-family: Arial, Helvetica, sans-serif; font-size: 13px } li,td {Arial , Helvetica, sans-serif; font-size: 13px } hr { color: #3333cc; width: 300; text-align: left } a { color: #000000 } #loginPanel { width : 250px; float : left; }
控制器两个
登陆控制器
package cc.openhome.controller; import java.io.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/login.do") public class Login extends HttpServlet { private final String USERS = "F:/USERDATABASE"; private final String SUCCESS_VIEW = "member.view"; private final String ERROR_VIEW = "index.html"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String userName=request.getParameter("username"); String passWord=request.getParameter("password"); if(checkLogin(userName,passWord)) { request.getRequestDispatcher(SUCCESS_VIEW).forward(request, response);; } else //如果用户名密码不正确 重定向回到首页 response.sendRedirect(ERROR_VIEW); } /** * 通过检索文件判断是否存在账号密码 * @param username * @param password * @return * @throws IOException */ private boolean checkLogin(String username, String password) throws IOException { if(username != null && password != null) { for (String file : new File(USERS).list()) { if (file.equals(username)) { BufferedReader reader = new BufferedReader( new FileReader(USERS + "/" + file + "/profile")); String passwd = reader.readLine().split("\t")[1]; if(passwd.equals(password)) { return true; } } } } return false; } }
注册控制器
package cc.openhome.controller; import java.io.*; import java.util.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/register.do") public class Register extends HttpServlet { private final String USERS = "F:/USERDATABASE"; private final String SUCCESS_VIEW = "success.view"; private final String ERROR_VIEW = "error.view"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String email=request.getParameter("email"); String username=request.getParameter("username"); String password=request.getParameter("password"); String confirmedPasswd=request.getParameter("confirmedPasswd"); List<String> errors=new ArrayList<String>(); //判断邮件是否正确 if(isInvalidEmail(email)) { errors.add("未填写邮件或邮件格式不正确"); } //判断用户名是否正确 if(isInvalidUsername(username)) { errors.add("用户名称为空或已存在"); } //判断密码是否正确 if(isInvalidPassword(password, confirmedPasswd)) { errors.add("请确认密码符合格式并再次确认密码"); } String resultType=ERROR_VIEW; //如果有任何错误 if(!errors.isEmpty()) { request.setAttribute("errors", errors); } //如果没有任何错误 else { resultType=SUCCESS_VIEW; createUserData(email, username, password); } request.getRequestDispatcher(resultType).forward(request, response); } /** * 判断是否是非法邮箱 * @param email * @return */ private boolean isInvalidEmail(String email) { return email == null || !email.matches("^[_a-z0-9-]+([.]" + "[_a-z0-9-]+)*@[a-z0-9-]+([.][a-z0-9-]+)*$"); } /** * 通过文件名来判断是否有用户存在 * @param username * @return */ private boolean isInvalidUsername(String username) { //如果没有输入任何字符 得到的其实是空串而不是Null if(username=="") return true; //列出所有目录 如果有该目录 则说明用户存在 for (String file : new File(USERS).list()) { //判断是否存在文件(用户)且文件名称和我们输入的是否一样 if (file.equals(username)) { return true; } } return false; } /** * 判断密码是否合法且两次输入是否相同 * @param password * @param confirmedPasswd * @return */ private boolean isInvalidPassword(String password, String confirmedPasswd) { return password == null || password.length() < 6 || password.length() > 16 || !password.equals(confirmedPasswd); } /** * 创建用户数据并写入文件 * @param email * @param username * @param password * @throws IOException */ private void createUserData(String email, String username, String password) throws IOException { File userhome = new File(USERS + "/" + username); //以用户名的形式创建目录 userhome.mkdir(); //在目录中再创建一个profile存储邮箱和密码 BufferedWriter writer = new BufferedWriter( new FileWriter(userhome + "/profile")); writer.write(email + "\t" + password); writer.close(); } }
视图三个 这里先不用jsp 直接在Servlet中打印输出
注册失败页面
package cc.openhome.view; import java.io.*; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/error.view") public class Error extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置返回编码 response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE html PUBLIC " + "‘-//W3C//DTD HTML 4.01 Transitional//EN‘>"); out.println("<html>"); out.println("<head>"); out.println(" <meta content=‘text/html; charset=UTF-8‘" + "http-equiv=‘content-type‘>"); out.println(" <title>新增会员失败</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>新增会员失败</h1>"); out.println("<ul style=‘color: rgb(255, 0, 0);‘>"); //取得放在请求域中的错误信息 List<String> errors=(List<String>) request.getAttribute("errors"); for(String error:errors) { out.println("<li>"+error+"<li>"); } out.println("</ul>"); out.println("<a href=http://www.mamicode.com/‘register.html‘>返回注册页面"); out.println("</body>"); out.println("</html>"); out.close(); } }
登陆成功页面
package cc.openhome.view; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/member.view") public class Member extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE html PUBLIC ‘-//W3C//DTD HTML 4.01 Transitional//EN‘>"); out.println("<html>"); out.println("<head>"); out.println(" <meta content=‘text/html; charset=UTF-8‘ http-equiv=‘content-type‘>"); out.println(" <title>会员登入成功</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>会员 " + request.getParameter("username") + " 你好</h1>"); out.println("</body>"); out.println("</html>"); out.close(); } }
注册成功输出页面
package cc.openhome.view; import java.io.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/success.view") public class Success extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE html PUBLIC ‘-//W3C//DTD HTML 4.01 Transitional//EN‘>"); out.println("<html>"); out.println("<head>"); out.println(" <meta content=‘text/html; charset=UTF-8‘ http-equiv=‘content-type‘>"); out.println(" <title>会员注册成功</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>会员 " + request.getParameter("username") + " 注册成功</h1>"); out.println("<a href=http://www.mamicode.com/‘index.html‘>回首页登入"); out.println("</body>"); out.println("</html>"); out.close(); } }
微博小程序 版本一
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。