我们可以将对象绑定到HttpSession实例上, 并使用setAttribute和getAttribute方法获取对象。
在上一页中, 我们了解了什么是HttpSession, 如何从会话对象存储和获取数据等。
在这里, 我们将创建一个不使用数据库代码的真实的登录和注销应用程序。我们假设密码为admin123。
访问此处以获取仅使用cookie的登录和注销应用程序使用cookie的servlet登录和注销示例
在此示例中, 我们将创建3个链接:登录, 注销和配置文件。用户必须先登录才能进入个人资料页面。如果用户已注销, 则需要再次登录才能访问个人资料。
在此应用程序中, 我们创建了以下文件。
- index.html
- link.html
- login.html
- LoginServlet.java
- LogoutServlet.java
- ProfileServlet.java
- web.xml
<
!DOCTYPE html>
<
html>
<
head>
<
meta charset="ISO-8859-1">
<
title>
Servlet Login Example<
/title>
<
/head>
<
body>
<
h1>
Login App using HttpSession<
/h1>
<
a href="http://www.srcmini.com/login.html">
Login<
/a>
|
<
a href="http://www.srcmini.com/LogoutServlet">
Logout<
/a>
|
<
a href="http://www.srcmini.com/ProfileServlet">
Profile<
/a>
<
/body>
<
/html>
档案:link.html
<
a href="http://www.srcmini.com/login.html">
Login<
/a>
|
<
a href="http://www.srcmini.com/LogoutServlet">
Logout<
/a>
|
<
a href="http://www.srcmini.com/ProfileServlet">
Profile<
/a>
<
hr>
档案:login.html
<
form action="LoginServlet" method="post">
Name:<
input type="text" name="name">
<
br>
Password:<
input type="password" name="password">
<
br>
<
input type="submit" value="http://www.srcmini.com/login">
<
/form>
文件:LoginServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
request.getRequestDispatcher("link.html").include(request, response);
String name=request.getParameter("name");
String password=request.getParameter("password");
if(password.equals("admin123")){
out.print("Welcome, "+name);
HttpSession session=request.getSession();
session.setAttribute("name", name);
}
else{
out.print("Sorry, username or password error!");
request.getRequestDispatcher("login.html").include(request, response);
}
out.close();
}
}
文件:LogoutServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LogoutServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
request.getRequestDispatcher("link.html").include(request, response);
HttpSession session=request.getSession();
session.invalidate();
out.print("You are successfully logged out!");
out.close();
}
}
【Servlet HttpSession登录和注销示例图解】文件:ProfileServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ProfileServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
request.getRequestDispatcher("link.html").include(request, response);
HttpSession session=request.getSession(false);
if(session!=null){
String name=(String)session.getAttribute("name");
out.print("Hello, "+name+" Welcome to Profile");
}
else{
out.print("Please login first");
request.getRequestDispatcher("login.html").include(request, response);
}
out.close();
}
}
文件:web.xml
<
?xml version="1.0" encoding="UTF-8"?>
<
web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<
servlet>
<
description>
<
/description>
<
display-name>
LoginServlet<
/display-name>
<
servlet-name>
LoginServlet<
/servlet-name>
<
servlet-class>
LoginServlet<
/servlet-class>
<
/servlet>
<
servlet-mapping>
<
servlet-name>
LoginServlet<
/servlet-name>
<
url-pattern>
/LoginServlet<
/url-pattern>
<
/servlet-mapping>
<
servlet>
<
description>
<
/description>
<
display-name>
ProfileServlet<
/display-name>
<
servlet-name>
ProfileServlet<
/servlet-name>
<
servlet-class>
ProfileServlet<
/servlet-class>
<
/servlet>
<
servlet-mapping>
<
servlet-name>
ProfileServlet<
/servlet-name>
<
url-pattern>
/ProfileServlet<
/url-pattern>
<
/servlet-mapping>
<
servlet>
<
description>
<
/description>
<
display-name>
LogoutServlet<
/display-name>
<
servlet-name>
LogoutServlet<
/servlet-name>
<
servlet-class>
LogoutServlet<
/servlet-class>
<
/servlet>
<
servlet-mapping>
<
servlet-name>
LogoutServlet<
/servlet-name>
<
url-pattern>
/LogoutServlet<
/url-pattern>
<
/servlet-mapping>
<
/web-app>
下载此示例(使用Eclipse IDE开发)
输出
文章图片
文章图片
文章图片
文章图片
文章图片
文章图片
如果再次单击配置文件链接, 则需要先登录。
推荐阅读
- 使用Cookies的Servlet登录和注销示例图解
- 带有注解的Servlet(Servlet3的功能)
- Servlet过滤器基本介绍和用法
- Servlet中的RequestDispatcher示例图解
- 服务器端包含(SSI)介绍
- Servlet中的SendRedirect用法示例
- Servlet中的分页示例图解
- Servlet使用JavaMail API发送电子邮件示例
- AndroidSDK的默认安装路径