1 登录页面的代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
/**
* 老紫竹JavaEE培训教程(3)- 登录表单和密码判断
*/
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" c>
<title>登录</title>
</head>
<body>
登录
<form method="post" name="LOGIN_FORM" id="LOGIN_FORM" action="logincheck.jsp"><br />
用户名:<input type="text" id="username" name="username" size="20" maxlength="20" /><br />
密码:<input type="password" id="password" name="password" size="20" maxlength="20" /><br />
<input type="submit" value="登录" /></form>
</body>
</html>
2 登录判断的代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
/**
* 老紫竹JavaEE培训教程(3)- 登录表单和密码判断
*/
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" c>
<title>登录检测</title>
</head>
<body>
<a href="login.jsp">返回登录</a>
<%
// 设置请求数据的编码方式
// 应尽可能放在其它代码的前面
request.setCharacterEncoding("UTF-8");
// 判断登录方式必须是POST
if ("POST".equals(request.getMethod())) {
// 从request对象里读取参数
String username = request.getParameter("username");
String password = request.getParameter("password");
// 判断用户名和密码必须填写
if (username == null || username.trim().length() == 0) {
out.println("请填写用户名");
} else if (password == null || password.trim().length() == 0) {
out.println("请填写密码");
} else {
// 去掉前后的空格等
username = username.trim();
password = password.trim();
// 判断用户名和密码
if ("admin".equals(username) && "1234".equals(password)) {
out.println("登录成功");
} else {
out.println("用户名或密码失败!");
}
}
} else {
out.print("请使用POST提交方式");
}
%>
</body>
</html>
3 运行
http://127.0.0.1:8080/j2ee/login.jsp
用户名为:admin
密码为:1234
4 测试
登录是表单操作的基础,请移动要掌握。