session实现用户登陆功能

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登陆页面</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

  </head>

  <body>

    <form action="/Session/servlet/LoginServlet" method="post">
        用户名:<input type="text" name="username"><br/>
        密码:<input type="password" name="password"><br/>
        <input type="submit" value="登陆">
    </form>


  </body>
</html>

package login;

public class User
{
    private String username;
    private String password;

    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
    public User(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }


}

package login;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = DB.find(username, password);  //html
        if(user==null){
            out.write("用户名或密码有误!!");
            return;
        }

        request.getSession().setAttribute("user", user);  //向用户session中存一个登陆标记,也就是让用户登陆成功

        response.sendRedirect("/Session/index1.jsp");


    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }
}

class DB
{
    private static List<User> list = new ArrayList();
    static{
        list.add(new User("aaa","123"));
        list.add(new User("bbb","123"));
        list.add(new User("ccc","123"));
    }


    public static User find(String username ,String password){
        for(User user : list){
            if(user.getUsername().equals(username) && user.getPassword().equals(password)){
                return user;
            }
        }
        return null;
    }
}

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="login.User"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>

  <body>

    欢迎您:
    <% 
        User user = (User)session.getAttribute("user");
        if(user!=null) out.write(user.getUsername());
    %>

    <br/><br/>
    <a href="/Session/login.html">登陆</a>

  </body>
</html>

浏览器输入:http://localhost:8080/Session/login.html
对用户名和密码进行输入:
输入错误用户名和密码::
这里写图片描述
登陆:
这里写图片描述


输入正确的用户名和密码:
这里写图片描述
登陆:
这里写图片描述

在JavaWeb中,Session是一种保存在服务器端的数据对象,用于保存用户会话信息。可以通过Session实现用户登录功能,具体步骤如下: 1. 用户输入用户名和密码,提交表单到服务器端。 2. 服务器端接收到表单数据后,验证用户名和密码是否正确。 3. 如果用户名和密码正确,则在服务器端创建一个Session对象,并将用户信息保存到Session中,如用户ID、用户名等。 4. 将Session ID返回给客户端浏览器,通常是通过cookie方式保存Session ID。 5. 用户进行其他操作时,浏览器会将保存的Session ID发送给服务器端,服务器端可以通过Session ID获取到对应的Session对象,从而获取到用户信息。 6. 当用户退出登录时,服务器端可以通过invalidate方法将Session对象销毁,从而清除用户信息。 在实现用户登录时,需要注意以下几点: 1. 用户密码不能明文存储,应该加密存储或使用哈希函数存储。 2. Session ID应该设置为随机字符串,以避免被猜测。 3. Session对象中保存的用户信息应该尽量少,以减少网络流量和服务器内存占用。 4. 应该设置Session过期时间,以避免Session长时间存在导致安全风险和服务器资源浪费。 示例代码如下: ```java // 用户登录验证 if (checkUser(username, password)) { // 登录成功,创建Session对象 HttpSession session = request.getSession(true); // 设置Session过期时间为30分钟 session.setMaxInactiveInterval(30 * 60); // 将用户信息保存到Sessionsession.setAttribute("userId", userId); session.setAttribute("username", username); // 将Session ID保存到cookie中 Cookie cookie = new Cookie("JSESSIONID", session.getId()); cookie.setMaxAge(30 * 60); response.addCookie(cookie); // 跳转到主页 response.sendRedirect("index.jsp"); } else { // 登录失败 response.sendRedirect("login.jsp?error=1"); } ``` 在上面的示例代码中,如果用户登录成功,则创建一个Session对象,并将用户ID和用户名保存到Session中,同时将Session ID保存到cookie中。如果用户登录失败,则重定向到登录页面,并在URL中添加error参数,以提示用户登录失败。在JSP页面中,可以通过EL表达式和Session对象获取用户信息,如下所示: ```jsp <%-- 获取Session中的用户信息 --%> <% String userId = (String) session.getAttribute("userId"); String username = (String) session.getAttribute("username"); %> <%-- 显示用户信息 --%> <p>欢迎您,<c:out value="${username}" /></p> ``` 在上面的示例代码中,使用了JSP页面中的EL表达式和<c:out>标签,从Session中获取用户信息,并将其显示在页面上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值