功能介绍:

登录界面包含用户、密码,输入已经设定好的用户名和密码,点击登录,然后判断是否输入正确,如果正确就会输出成功登录界面,否则就弹出用户名或密码错误的字样,然后可以点击注销按钮回到其实登录界面,由于在cookie中保存了用户名,会自动填入之前保存的用户名。同时界面还包含了显示页面的访问次数。

登录界面:

java点登录按钮跳转页面 java 登录页面_java

登录成功界面:

java点登录按钮跳转页面 java 登录页面_学习_02

密码或者用户名错误时报错

java点登录按钮跳转页面 java 登录页面_学习_03

点击注销自动回到原来的登录界面,并自动输入刚刚登录的用户名

java点登录按钮跳转页面 java 登录页面_java点登录按钮跳转页面_04

代码 如下

userLogin.jsp(包含登录界面和注销后的自动输入用户名)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>userLogin</title>
</head>
<body>
<%
	String msg = "";
	if(request.getAttribute("msg")!=null){
		msg=(String)request.getAttribute("msg");
	}
	
		
	Cookie[] cookies=request.getCookies();
	String username="";
	if(cookies != null){
	for(Cookie cookie : cookies){
		if(cookie.getName().equals("userName")){
			username = cookie.getValue();
			break;
		}	
	}
	}
%>
	<span style="color:red;"> <%= msg %> </span>
		
<form method ="post" action="loginSuccess.jsp">
	<p>
		<label>用户:</label>
		<input type ="text" name="userName" value ="<%=username %>">
	</p>
	<p>
		<label>密码:</label>
		<input type="password" name= "userPwd">
	</p>
	
	<input type="submit" value="登录">
</form>
<%
	Object count = application.getAttribute("count");
	if(count ==null){
		application.setAttribute("count",new Integer(1));
	}else{
		Integer i =(Integer)count;
			application.setAttribute("count",i.intValue()+1);
	}
	Integer icount =(Integer)application.getAttribute("count");
	out.println("页面被访问了"+icount.intValue()+"次");
%>
</body>
</html>

loginSuccess.jsp(判断密码是否正确)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loginSuccess</title>
</head>
<body>
<%
	request.setCharacterEncoding("UTF-8");
	String userName = null;
	String userPwd = null;
	userName = request.getParameter("userName");
	userPwd = request.getParameter("userPwd");
			
	if("lnc".equals(userName)&&"123".equals(userPwd))//设置密码
	{	
		Cookie cookie =new Cookie("userName",userName);
		response.addCookie(cookie);
	
		session.setAttribute("userName", userName);	//存储在session中
		session.setAttribute("userPwd", userPwd);
		response.sendRedirect("index.jsp");
			
	}
		else{
			String msg="用户名或密码错误";
			request.setAttribute("msg", msg);
			request.getRequestDispatcher("userLogin.jsp").forward(request,response);
		}
			
	%>
		
</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>
<%
	String userName=null;
	String userPwd=null;
	userName=(String)session.getAttribute("userName");
	userPwd=(String)session.getAttribute("userPwd");
	session.setMaxInactiveInterval(2*60);
%>
	<h1>登录成功</h1>
	<h2> 欢迎 <%= userName%></h2>
	<a href="logout.jsp">注销</a>
	
</body>

</html>

logout.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>

<title>用户注销</title>

</head>
<body>
 
<%
	session.invalidate();
	response.sendRedirect("userLogin.jsp");
%>
 
</body>
</html>