编写3个页面,实现登录页面login.jsp,在其中输入用户名,密码,发送到登录验证页面loginCL.jsp,验证登录成功,在success.jsp页面显示成功提示、打印出相关信息,验证登录失败,返回login.jsp页面,在login.jsp页面提示“用户名不存在”或“用户密码错误”的提示信息。用户如果直接访问loginCL.jsp或success.jsp,均转向login.jsp页面。
注意:用request属性
1.新建文件login.jsp、loginCL.jsp、success.jsp
2.login.jsp文件代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>login.jsp页面</title>
</head>
<body>
<form action="loginCL.jsp" method="post" name=form >
<font size="5">登录界面</font><br>
用户名:<input type="text" value=""name="username"><br>
密 码:<input type="text"value="" name="password"><br>
<input type="submit"value="提交"name="submit">
<input type="reset"value="重置">
</form>
<%=(String)request.getAttribute("usertxt") %>
</body>
</html>
3.loginCL.jsp文件代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>loginCL.jsp页面</title>
</head>
<body>
<%
String referer = request.getHeader("Referer");
if(referer==null||referer.trim().length()==0||
!referer.equals("http://localhost:8081/two/login.jsp")){
response.sendRedirect("login.jsp");
return;
}
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name=request.getParameter("username");
String password=request.getParameter("password");
//判断登陆界面的用户名和密码,根据输入的不同情况进行不同的处理
if(name.trim().equals("admin")&&password.trim().equals("1234"))
{
request.setAttribute("usertxt", "正常登录!!!");
request.getRequestDispatcher("success.jsp").forward(request, response);
}
else{
request.setAttribute("usertxt", "用户名或密码错误!!!");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
%>
</body>
</html>
4.success.jsp文件代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>success.jsp页面</title>
</head>
<body>
<%
String referer = request.getHeader("Referer");
if(referer==null||referer.trim().length()==0||
!referer.equals("http://localhost:8081/two/login.jsp")){
response.sendRedirect("login.jsp");
return;
}
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
%>
<%=(String)request.getAttribute("usertxt") %>
</body>
</html>
5.浏览器显示效果