JSP内置对象简单应用
登录页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String message = "";
// 获取刚刚那个页面设置message的值
message = (String) request.getAttribute("message");
if (message == null) {
message = "";
}
%>
<form action="reg.jsp" method="post">
用户名:
<input type="text" name="username">
<br />
密 码:
<input type="password" name="pwd" />
<%=message%>
<br />
<input type="submit" values="登录" />
</form>
</body>
</html>
账号或密码错误页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<%
// 中文编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//获取请求值
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
if("admin".equals(username)&&"123456".equals(pwd)){
// 设置属性值 以便跳转到下个页面获取
session.setAttribute("username",username);
session.setAttribute("pwd",pwd);
//跳转页面 直接跳转
response.sendRedirect("success.jsp");
}else{
//跳转页面 间接跳转
request.setAttribute("message","用户名或密码错误");
request.getRequestDispatcher("Login.jsp").forward(request,response);
}
%>
</center>
</body>
</html>
登录成功页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<p style="color:red; font-size:20px">登陆成功</p>
<%
// 中文编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//获取属性值 (String)强行转换数据类型
String username = (String)session.getAttribute("username");
String pwd = (String)session.getAttribute("pwd");
out.println("用户名:" + username);%>
<br/>
<%
out.println("密 码:" + pwd);
%>
</center>
</body>
</html>

本文详细介绍了如何在JSP中使用内置对象如request进行登录页面、错误提示及登录成功的页面交互,包括获取参数、设置会话属性和页面跳转。
2689

被折叠的 条评论
为什么被折叠?



