码迷,mamicode.com
首页 > 编程语言 > 详细

JavaWeb_用户登录注册模板

时间:2019-01-20 00:51:57      阅读:600      评论:0      收藏:0      [点我收藏+]

标签:页面   int   com   信息   static   false   key   提示   HERE   

 

 

用户注册_01版

  当用户点击主页面注册时跳转到register.jsp页面,用户输入完表单后由register_do.jsp页面进行处理,户数据模型驱动User,java和DBUtild.java中对数据进行本地处理

  DBUtild.java对用户ID进行同名检测,用户注册成功显示绿色提示信息用户注册失败显示红色提示信息

技术分享图片

技术分享图片
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<a>登录</a>
<a href="register.jsp">注册</a>

</body>
</html>
index.jsp

 

技术分享图片
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<form action="register_do.jsp" methon="post">
用户名:<input type="text" name="username" /><br/>
密码:    <input type="password" name="password" /><br/>
年龄:    <input type="text" name="age" /><br/>
性别:男<input type="radio" name="sex" value="男" /><input type="radio" name="sex" value="女"/><br/>

<input type="submit" value="注册"/>
</form>

</body>
</html>
register.jsp

 

技术分享图片
<%@ page import="com.Gary.util.DBUtil" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%

String username = request.getParameter("username");
String password = request.getParameter("password"); 
int age = Integer.parseInt(request.getParameter("age"));
String sex = request.getParameter("sex");

boolean isSuccess = DBUtil.addUser(username,password,age,sex);
if(isSuccess){
    out.println(username);
     out.println("<font color=‘green‘>注册成功</font>");
}else{
    out.println(username);
    out.println("<font color=‘red‘>注册失败</font>");
}
%>
</body>
</html>
register_do.jsp

 

技术分享图片
package com.Gary.model;

public class User {
    private String username;
    private String password;
    private int age;
    private String sex;
    public User(String username, String password, int age, String sex) {
        super();
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
    }
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    
    
}
User.java

 

技术分享图片
package com.Gary.util;

import java.util.HashMap;
import java.util.Map;

import com.Gary.model.User;


public class DBUtil {
    
    private static Map<String ,User>db = new HashMap<String,User>();
    
    public static boolean addUser(String username,String password,int age,String sex) {
        //TODO
        
        if(db.containsKey(username)) {
            return false;
        }else {
            User user=new User(username,password,age,sex);
            db.put(username, user);
            return true;
        }
    }
}
DBUtil.java

  开发注册界面

  表单

  技术分享图片

  用户表单信息注册完毕后提交表单给register_do.jsp

<form action="register_do.jsp" methon="post">
用户名:<input type="text" name="username" /><br/>
密码:    <input type="password" name="password" /><br/>
年龄:    <input type="text" name="age" /><br/>
性别:男<input type="radio" name="sex" value="男" /><input type="radio" name="sex" value="女"/><br/>

<input type="submit" value="注册"/>
</form>

 

  户数据模型驱动User,java存储用户信息,DBUtil.java运用字典集对数据进行本地存储

    private static Map<String ,User>db = new HashMap<String,User>();

 

  通过DBUtil.java中的addUser()方法对用户进行本地注册,返回bool值,当用户注册ID相同时返回false,否则返回true

        if(db.containsKey(username)) {
            return false;
        }else {
            User user=new User(username,password,age,sex);
            db.put(username, user);
            return true;
        }

 

  由register_do.java对用户信息进行校验

boolean isSuccess = DBUtil.addUser(username,password,age,sex);
if(isSuccess){
    out.println(username);
     out.println("<font color=‘green‘>注册成功</font>");
}else{
    out.println(username);
    out.println("<font color=‘red‘>注册失败</font>");
}

 

 

用户注册_02版

  当用户通过首页直接进入login.jsp时,login.jsp页面显示登录字样,用户通过register.jsp进入login.jsp页面时,提示用户注册成功

技术分享图片

 

技术分享图片
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<a href="login.jsp">登录</a>
<a href="register.jsp">注册</a>

</body>
</html>
index.jsp

 

技术分享图片
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<%
Object msg = request.getAttribute("message");
if(msg!=null)
    out.println(msg);
%>

登录<hr>
<form action="" methon="post">
用户名:<input type="text" name="username" /><br/>
密码    :<input type="password" name="password" /><br/>

<input type="submit" value="登录"/>
</form>
</body>
</html>
login.jsp

 

技术分享图片
<%@ page import="com.Gary.util.DBUtil" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%

String username = request.getParameter("username");
String password = request.getParameter("password"); 
int age = Integer.parseInt(request.getParameter("age"));
String sex = request.getParameter("sex");

boolean isSuccess = DBUtil.addUser(username,password,age,sex);

//使用请求转发
if(isSuccess){
    request.setAttribute("message", "注册成功,请登录");
    //通过getRequestDispatcher传递路径将请求转发给login.jsp
    request.getRequestDispatcher("login.jsp").forward(request,response);
}

// if(isSuccess){
//     out.println(username);
//      out.println("<font color=‘green‘>注册成功</font>");
// }else{
//     out.println(username);
//     out.println("<font color=‘red‘>注册失败</font>");
// }
%>
</body>
</html>
register_do.jsp

 

技术分享图片
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<form action="register_do.jsp" methon="post">
用户名:<input type="text" name="username" /><br/>
密码:    <input type="password" name="password" /><br/>
年龄:    <input type="text" name="age" /><br/>
性别:男<input type="radio" name="sex" value="男" /><input type="radio" name="sex" value="女"/><br/>

<input type="submit" value="注册"/>
</form>

</body>
</html>
register.jsp

 

技术分享图片
package com.Gary.model;

public class User {
    private String username;
    private String password;
    private int age;
    private String sex;
    public User(String username, String password, int age, String sex) {
        super();
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
    }
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    
    
}
User.java

 

技术分享图片
package com.Gary.util;

import java.util.HashMap;
import java.util.Map;

import com.Gary.model.User;


public class DBUtil {
    
    private static Map<String ,User>db = new HashMap<String,User>();
    
    public static boolean addUser(String username,String password,int age,String sex) {
        //TODO
        
        if(db.containsKey(username)) {
            return false;
        }else {
            User user=new User(username,password,age,sex);
            db.put(username, user);
            return true;
        }
    }
}
DBUtil.Java

 

  当登录成功时,通过在register_do.jsp页面中表单传递时进行转发给login,jsp页面【跳转到login.jsp页面】

boolean isSuccess = DBUtil.addUser(username,password,age,sex);

//使用请求转发
if(isSuccess){
    request.setAttribute("message", "注册成功,请登录");
    //通过getRequestDispatcher传递路径将请求转发给login.jsp
    request.getRequestDispatcher("login.jsp").forward(request,response);
}

  

  login,jsp页面最开始时进行判断是否由register.jsp传递过来信息

<%
Object msg = request.getAttribute("message");
if(msg!=null)
    out.println(msg);
%>

 

JavaWeb_用户登录注册模板

标签:页面   int   com   信息   static   false   key   提示   HERE   

原文地址:https://www.cnblogs.com/1138720556Gary/p/10293791.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!