JavaWeb-利用Session登录成功后跳转

login.jsp
 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>






登录页面











	
用户名(只能由字母组成,3~12位)
密码(6~12位)
${requestScope["kong"] }${requestScope["YWLJ"] } ${requestScope["BPP"] }

login-action.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*"%>




业务逻辑判断





	<%
		//判断是否为空
		if (request.getParameter("username").equals("")
				&& request.getParameter("password").equals("")) {
			//为空就将请求转发到登录页面报错
			request.setAttribute("kong", "您输入的内容为空");
			request.getRequestDispatcher("/Task/Task07/login.jsp").forward(
					request, response);
		} else {
			//不是空就创建
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			//判断用户名密码是否符合业务逻辑
			if (username.matches("[a-zA-Z]{3,12}")
					&& password.matches("[a-zA-Z0-9]{6,12}")) {

				//判断用户名密码是否正确,正确就跳转到欢迎页面
				if (username.equals("tom") && password.equals("123456")) {
					out.print("welcome!" + username);
					//使用一个指定的名称绑定一个对象到session会话,request也有此方法
					session.setAttribute("username", username);
					//跳转到欢迎页面
					response.sendRedirect("/WebTask/index.jsp");
				} else {
					//用户名密码不正确时
					request.setAttribute("BPP", "您输入的用户名密码不匹配");
					request.getRequestDispatcher("/Task/Task07/login.jsp")
							.forward(request, response);
				}
			} else {
				//不符合业务逻辑时
				request.setAttribute("YWLJ", "请按要求输入用户名和密码");
				request.getRequestDispatcher("/Task/Task07/login.jsp")
						.forward(request, response);

			}
		}
	%>



index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%!int fontSize = 0;%>





欢迎页面









	现在的时间是:
	<%=(new java.util.Date()).toLocaleString()%>
<% out.print("

Welcome!" + session.getAttribute("username")+"

"); %>

演示

JavaWeb-利用Session登录成功后跳转_第1张图片

你可能感兴趣的:(JavaWeb)