1.session.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<jsp:directive.page import="com.test.bean.Person" />
<jsp:directive.page import="java.text.SimpleDateFormat" />
<jsp:directive.page import="java.text.DateFormat" />
<%!
DateFormat dataFormate = new SimpleDateFormat("yyyy-MM-dd");
%>
<%
response.setCharacterEncoding("UTF-8");
Person[] persons = {
new Person("java","password1",dataFormate.parse("1982-03-11")),
new Person("Hello","password2",dataFormate.parse("1988-01-11")),
new Person("Hello2","password3",dataFormate.parse("1988-01-11"))
};
String message = "";
if(request.getMethod().equals("POST")){
for(Person person : persons){
if(person.getName().equalsIgnoreCase(request.getParameter("username"))
&& person.getPassword().equals(request.getParameter("password"))
){
session.setAttribute("person",person);
session.setAttribute("loginTime",new Date());
response.sendRedirect(request.getContextPath() +"/welcome.jsp");
return;
}
}
message = "用户名密码不正确";
}
%>
<%
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 'session.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>
<div align="center" style="margin:10px; ">
<fieldset>
<legend>登录</legend>
<form action="session.jsp" method="post">
<table>
<%-- <tr>
<td></td>
<td><span></span>
<span style="color:red;"><%= exception.getMessage() %></span>
</td>
</tr>
--%>
<tr>
<td>账号:</td>
<td><input type="text" name="username" style="width:200px;"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" style="width:200px;">
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="登 录"></td>
</tr>
</table>
</form>
</fieldset>
</body>
</html>
2.Person.java
package com.test.bean;
import java.util.Date;
public class Person
{
String name;
String password;
Date date;
public Person(String name,String password,Date date){
this.name = name;
this.password = password;
this.date = date;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
}
3.welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<jsp:directive.page import="com.test.bean.Person" />
<jsp:directive.page import="java.text.SimpleDateFormat" />
<jsp:directive.page import="java.text.DateFormat" />
<%!
DateFormat dataFormate = new SimpleDateFormat("yyyy-MM-dd");
%>
<%
Person person = (Person)session.getAttribute("person");
Date loginTime = (Date)session.getAttribute("loginTime");
%>
<%
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 'welcome.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>
<table>
<tr>
<td>您的名字:</td>
<td><%=person.getName() %></td>
</tr>
<tr>
<td>登录时间:</td>
<td><%= loginTime %></td>
</tr>
<tr>
<td>您生日:</td>
<td><%= person.getDate() %></td>
</tr>
</table>
</body>
</html>