JSP实现用户登录

登录界面 login.jsp:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
   String path = request.getContextPath();
   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
	<head>
		<!-- Page title -->
		<title>imooc - Login</title>
		<!-- End of Page title -->
		<!-- Libraries -->
		<link type="text/css" href="css/login.css" rel="stylesheet" />	
		<link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" />	
		<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
		<script type="text/javascript" src="js/easyTooltip.js"></script>
		<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
		<!-- End of Libraries -->	
	</head>
	<body>
	<div id="container">
		<div class="logo">
			<a href="#"><img src="logo.png" alt="" /></a>
		</div>
		<div id="box">
			<form action="dologin.jsp" method="post">
			<p class="main">
				<label>用户名: </label>
				<input name="username" value="" /> 
				<label>密码: </label>
				<input type="password" name="password" value="">	
			</p>
			<p class="space">
				<input type="submit" value="登录" class="login" style="cursor: pointer;"/>
			</p>
			</form>
		</div>
	</div>
	</body>
</html>

对登录的响应 dologin.jsp:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  String username ="";
  String password ="";
  request.setCharacterEncoding("utf-8");//防止中文乱码
  
  username = request.getParameter("username");
  password = request.getParameter("password");
  
  //如果用户和密码都等于admin,则登录成功
  if("admin".equals(username)&&"admin".equals(password))
  {
     session.setAttribute("loginUser", username);
     request.getRequestDispatcher("login_success.jsp").forward(request, response);
     
  }
  else
  {
     response.sendRedirect("login_failure.jsp");
  }
%>

这里注意的是:

   request传送信息需要设置编码方式,request.setCharacterEncoding("utf-8");

	request.getRequestDispatcher("login_success.jsp")取得一个RequestDispatcher,一个请求分发器对象,它的方法forward(request,response)作用是将请求转发到login_success.jsp,request封装了请求信息,response封装了响应信息。注意是forward方式,这种方式只转发请求,不产生新的request对象。如下图:
	
	成功界面:login_success.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
   String path = request.getContextPath();
   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
	<head>
		<!-- Page title -->
		<title>imooc - Login</title>
		<!-- End of Page title -->
		<!-- Libraries -->
		<link type="text/css" href="css/login.css" rel="stylesheet" />	
		<link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" />	
		<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
		<script type="text/javascript" src="js/easyTooltip.js"></script>
		<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
		<!-- End of Libraries -->	
	</head>
	<body>
	<div id="container">
		<div class="logo">
			<a href="#"><img src="logo.png" alt="" /></a>
		</div>
		<div id="box">
		<%
			String loginUser = "";
			if(session.getAttribute("loginUser") != null) {
				loginUser = session.getAttribute("loginUser").toString();
			}
		%>
			
			欢迎您<font color = "red"> <%=loginUser %></font>,登陆成功!
		</div>
	</div>
	</body>
</html>

login_failure.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
   String path = request.getContextPath();
   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
	<head>
		<!-- Page title -->
		<title>imooc - Login</title>
		<!-- End of Page title -->
		<!-- Libraries -->
		<link type="text/css" href="css/login.css" rel="stylesheet" />	
		<link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" />	
		<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
		<script type="text/javascript" src="js/easyTooltip.js"></script>
		<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
		<!-- End of Libraries -->	
	</head>
	<body>
	<div id="container">
		<div class="logo">
			<a href="#"><img src="logo.png" alt="" /></a>
		</div>
		<div id="box">
			登录失败!检查用户或者密码
			<a href="login.jsp">返回登录界面</a>
		</div>
	</div>
	</body>
</html>



已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页