jsp使用getAttribute方法获得application信息



jsp使用getAttribute方法获得application信息实例源码介绍,设置全局变量、获得登陆的用户名,登陆密码等使用方法的jsp实例。

<%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8″%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>My JSP ‘JSP_application_attribute.jsp’ starting page</title>
</head>
<body>
<h1>使用getAttribute方法获得application信息</h1>
<%
//获得登陆的用户名
String username = (String)request.getParameter(“username”);
//获得登陆密码
String password = (String)request.getParameter(“password”);
//判断用户名和密码是否为空
if(username != null &&!username.equals(“”)&&password != null&&!password.equals(“”)){
//判断用户名是aa
if(username.equals(“aa”)){
//创建一个HashMap对象
HashMap hashMap = new HashMap();
//把获取的全局变量username和登录时间放入HashMap集合中
hashMap.put(username,Calendar.getInstance().getTime());
//设置全局变量
application.setAttribute(“username”,hashMap);
//
if(username.equals(“aa”)){
response.sendRedirect(“application_access.jsp”);
}
}else{
response.sendRedirect(“error.jsp”);
}
}else{
response.sendRedirect(“error.jsp”);
}
%>
</body>
</html>