用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 ( ) ;
}
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" ) ) ;
String sex= new String ( request. getParameter ( "sex" ) . getBytes ( "iso-8859-1" ) , "utf-8" ) ;
DBUtil util= new DBUtil ( ) ;
Boolean b= util. addUser ( username, password, age, sex) ;
if ( b== true ) {
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) ;
} else {
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>