用JSP简单的写一个登录注册页面
编写页面时先创建一个Dynamic web project,所有的jsp文件都放在WebContent文件夹下,java文件放在java Resource文件的src文件1.创建一个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><a href="login.jsp">登录</a> <a href="register.jsp">注册</a></body></html>
2.创建User类用于对用户的参数进行封装
package web02;public class User {private String uaername;private String password;private int age;private String sex;public String getUaername() {return uaername;}public void setUaername(String uaername) {this.uaername = uaername;}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;}public User() {super();// TODO Auto-generated constructor stub}public User(String uaername, String password, int age, String sex) {super();this.uaername = uaername;this.password = password;this.age = age;this.sex = sex;}}
3.创建DBUtil类用来存储用户信息
因为主要用jsp因此这里用map数组暂替数据库对用户信息进行存储
package web02;import java.util.HashMap;import java.util.Map;public class DBUtil {private static Map<String,User> map=new HashMap<String, User>();//判断改用户名是否被注册过public Boolean addUser(String username,String password,int age,String sex) {if(map.containsKey(username)) {return false;}else {User user=new User(username, password, age, sex);map.put(username, user);return true;}}//判断改用户名和密码是否输入正确public static User LoginUser(String username,String password) {if(map.containsKey(username)) {User user=map.get(username);if (user.getPassword().equals(password)) {return user;}return null;}return null;}}
4.创建注册页面register.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><%--action 将输入的数据传入注册处理页面--%><form action="register_do.jsp" method="post">用户名:<input type="text" name="username"><br>用户名密码:<input type="password" name="password"><br>年龄:<input type="text" name="age"><br>性别: 男<input type="radio" value="男" name="sex"> 女<input type="radio" name="sex" value="女"><input type="submit" value="注册"></form></body></html>
5.创建注册处理页面register_do.jsp(对后续输出乱码问题进行处理)
<%@page import="web02.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"));//因为在运行是系统会用默认的编码格式对传入的数据进行编码而原本的iso-8859-1,而这编码格式没有中文因此会产生乱码问题//通过系统默认的编码格式进行解码,在通过设置utf-8的编码String sex=new String(request.getParameter("sex").getBytes("iso-8859-1"),"utf-8");//创建DBUtil类对注册的用户数据进行判断DBUtil util=new DBUtil();Boolean b=util.addUser(username, password, age, sex);if(b==true){//注册成功后用getRequestDispatcher将页面跳转到登录页面,setAttribute用来传递不同的参数//在登录界面接收后呈现出不同的数据用于区分跳转的页面和直接登录页面request.setAttribute("key", "注册成功,请登录!");request.getRequestDispatcher("login.jsp").forward(request, response);}%></body></html>
6.创建登录页面login.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 key=request.getAttribute("key");if(key!=null)out.println(key);%>登录<hr><form action="login_do.jsp" method="post">用户名:<input type="text" name="username"><br>用户名密码:<input type="password" name="password"><br><input type="submit" value="登录"></form></body></html>
7.创建登录处理页面login_do.jsp
<%@page import="web02.User"%><%@page import="web02.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");User user=DBUtil.LoginUser(username, password);if(user==null){//登录失败跳转回登录页面request.setAttribute("key", "<font color='red'>登录失败,用户名或密码错误!</font><br>");request.getRequestDispatcher("login.jsp").forward(request, response);//out.println("登录失败,用户名或密码错误!");}else{//out.println("登录成功!");//登录成功跳转到用户信息页面request.setAttribute("user", user);request.getRequestDispatcher("personCenter.jsp").forward(request, response);}%></body></html>
8.创建用户信息页面personCenter
<%@page import="web02.User"%><%@ 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><% User user=(User)request.getAttribute("user");%>用户名:<%=user.getUaername() %><br>年龄:<%=user.getAge() %><br>性别:<%=user.getSex() %><br></body></html>