Session案例 用户登录

一.Session案例思维导图

Session案例 用户登录_第1张图片

二.编写登录页面:login.jsp

实现登录功能和显示登录错误信息

1.登录页面
2.显示登录失败信息
3.默认账户:mahaun 密码:123456

<%--
  Created by IntelliJ IDEA.
  User: pc
  Date: 17-4-4
  Time: 下午6:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录页面title>
head>
<body>
<%
    if(null!=request.getAttribute("Ext")){
        out.print("

"+request.getAttribute("Ext")+"

"
); } %>
<h3 >登录页面<h3> <form action="/LoginServlet" method="post"> 用户名:<input type="text" name="username"/> 密 码:<input type="password" name="password"> <input type="submit" value="登录">- form> body> html>

Session案例 用户登录_第2张图片

Session案例 用户登录_第3张图片

三.编写登录成功和再次访问页面:index.jsp index2.jsp

1.登录成功显示用户名

2.再次访问显示用户名

index.jsp:

1.显示登录成功
2.显示登录的用户名密码
3.判断session中是否含有username

<%--
  Created by IntelliJ IDEA.
  User: pc
  Date: 17-4-4
  Time: 下午6:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$title>
  head>
  <body>
  <%
    String username=(String)session.getAttribute("username")
    if(null==username){
        response.sendError(404);
    }else {
  %>

  <h1>主页面1h1>
  <h3>用户名:<%=request.getSession().getAttribute("username") %>h3>
  恭喜登录成功!
  body>
<%
  }
%>
html>

Session案例 用户登录_第4张图片

index2.jsp:

1.通过Session显示用户名
2.判断session中是否含有username
3.显示用户再次访问信息从 session中访问到username

<%--
  Created by IntelliJ IDEA.
  User: pc
  Date: 17-4-4
  Time: 下午6:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>主页2title>
head>
<body>
<%
    String username=(String)session.getAttribute("username")
    if(null==username){
        response.sendError(404);
    }else {
%>
<h1>主页面2h1>
<h3>用户名:<%=request.getSession().getAttribute("username") %>h3>
欢迎<%=request.getSession().getAttribute("username") %>再次访问!
body>
<%
    }
%>
body>
html>

Session案例 用户登录_第5张图片

三.写Servlet类

里面包含:接受用户名和密码

1.校验是否登录成功
2.重定向到index.jsp
3.创建Session:setAttribute:保存username
4.转发到login页面,request保存失败信息
5.创建reques:setAttribute保存要输出的错误信息

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by pc on 17-4-4.
 */
@WebServlet(name = "Login_Servlet",urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
       throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if("mahuan".equals(username)&&"123456".equals(password)){
            //登录成功
            request.getSession().setAttribute("username",username);
            response.sendRedirect(request.getContextPath()+"/index.jsp");
        }else {
            //登录失败
            request.setAttribute("Ext","您的账户或密码有误,请重新登录!");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }
}

Session案例 用户登录_第6张图片

文章文集:JavaEE–学习笔记

你可能感兴趣的:(Java,EE学习)