首页 > 编程语言 >Javaweb-登录界面-filter配合案例-2023-04-11

Javaweb-登录界面-filter配合案例-2023-04-11

时间:2023-04-11 22:14:27浏览次数:42  
标签:11 Javaweb resp req HttpServletResponse filter import servlet javax

1、新建login.jsp 登录界面 响应的路径


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Login</title>
</head>
<body>

<h1>登录界面</h1>
<hr>
<form action="/servlet/login" method="post">
    <input type="text" name="username">
    <input type="submit">
</form>

</body>
</html>

2、写对应的loginServlet

package com.feijian.servlet;

import com.feijian.util.constant;

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

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        if (username.equals("admin")){
            req.getSession().setAttribute(constant.USER_SESSION,req.getSession().getId());
            resp.sendRedirect("/sys/success.jsp");
        }else {
            resp.sendRedirect("/sys/error.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

3、设置sucess.jsp,和error.jsp页面。

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023/4/11
  Time: 20:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>主页</title>
</head>
<body>

<%--<%--%>
<%--    Object userName = request.getSession().getAttribute("USER_NAME");--%>
<%--    if (userName==null){--%>
<%--        response.sendRedirect("/login.jsp");--%>
<%--    }--%>
<%--%>--%>
<hr>
<br>
<h1>登录成功!</h1> <br>
<p><a href="/servlet/logout">注销</a></p>
<hr>
<br>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023/4/11
  Time: 20:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>错误界面</title>
</head>
<body>
<h3><span style="color: red">错误页面,登录不成功!</span></h3>
<a href="/login.jsp">返回登录界面</a>
</body>
</html>

4、再写一个logoutServlet

package com.feijian.servlet;

import com.feijian.util.constant;

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

public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object user_session = req.getSession().getAttribute(constant.USER_SESSION);
        if (user_session!=null){
            req.getSession().removeAttribute(constant.USER_SESSION);
            resp.sendRedirect("/login.jsp");
        }else {
            resp.sendRedirect("/login.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

5、写一个Filter类

package com.feijian.filter;

import com.feijian.util.constant;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class sysFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        if (request.getSession().getAttribute(constant.USER_SESSION)==null){
            response.sendRedirect("/error.jsp");
        }
        chain.doFilter(req,resp);
    }

    @Override
    public void destroy() {

    }
}

6、在web.xml中注册 三个类,loginServlet, logoutServlet, filter 类 并在映射路径上和前端页面上的一致。

注意:传入的值建议写入Session,这样取值即使页面跳转,只要浏览器不关闭就一直存在,不会丢失,如果用request 在重定向的情况下打开新页面就无法页面间传值。

标签:11,Javaweb,resp,req,HttpServletResponse,filter,import,servlet,javax
From: https://www.cnblogs.com/RUI2022/p/17307948.html

相关文章

  • 「Solution Set」4.11 小记
    P3642[APIO2016]烟火表演我不太会证明凸性。像这道题就是列出DP方程,\(f_{u,x}\)表示以\(u\)为根的子树还有\(x\)分钟就全爆炸的最小代价。然后赌它是个凸函数((因为它有\(sum\),就是两个下凸函数相加,还是下凸的。然后求前缀的最小值再加一个函数一类的,所以考虑之后这......
  • 4.11每日总结
    昨天的河北省科技政策查询系统需求项目没有整出来总记录,老师没让通过。今天又修改了一下    packagecn.itcase.dao.impl;importcn.itcase.dao.UserDao;importcn.itcase.domain.User;importcn.itcase.util.JDBCUtils;importorg.springframework.jdbc.core.Be......
  • 4月11号总结
    --删除tb_user表DROPTABLEIFEXISTStb_user;--创建tb_user表CREATETABLEtb_user(idint,usernameVARCHAR(20),passwordVARCHAR(32));--添加数据INSERTINTOtb_userVALUES(1,'zhangsan','123'),(2,'lisi','234......
  • day42(2023.4.11)
    1.数据库基本概念2.数据库中,各个概念之间的关系3.数据库分类4.MySQL简介、特点、以及分类 5.下载MySQL   6.MySQL的安装与卸载 7.连接MySQL  8.Navicat工具由于MySQL自带的客户端工具(就是那个黑窗口),有点小小的简陋,也不怎么好看。我们可以......
  • 2023年4月11日(软件工程日报)
    深度学习进步之处由sigmid函数,转变为RELU函数,由于梯度下降原因,sigmid函数后期学习非常缓慢,但relu函数可以避免这种情况损失函数,用于衡量单一训练样例效果 逻辑回归中用到的损失函数是:......
  • 2023.4.11——软件工程日报
    所花时间(包括上课):8h代码量(行):0行博客量(篇):1篇今天,上午学习,下午学习。我了解到的知识点:1.学习了python,并了解了一些关于python的知识;2.了解了一些数据库的知识;......
  • 面试4.11
    #1tcp三次握手和四次挥手#2osi七层协议,哪七层,每层有哪些#3tcp和udp的区别? udp用在哪里了?tcp三次握手和四次挥手tcp的三次握手和四次挥手实质就是tcp通信的连接和断开三次握手:为了对每次发送的数据量进行跟踪与协商,确保数据段的发送和接收同步,根据所接收到的数据量......
  • 4/11小组个人工作总结
    今天对教学管理系统的学生页面进行了初步的绘制,样子比较难看,功能也不齐全,日后慢慢改进。<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtml><html><head><metacharset="UTF-8"......
  • 4.11每日总结
    今天做了什么:完成了Python的安装和配置,并且可以在pycharm运行代码,学习了sqlserver数据库的调试器,完成了记账app的总金额传参遇到了哪些问题:python导入外部拓展库受到网速制约导入失败,更换网络后成功明天打算做什么:对记账app的图片识别返回结果进行优化,自动获取有用的信息,并且尝......
  • 2023/4/11
    判断某年是否是闰年问题#include<iostream>usingnamespacestd;intmain(){ intyear;cout<<"Entertheyear:";cin>>year;boolisaleapyear;isaleapyear=((year%4==0&&year%100!=0)||(year%400==0));if(isaleapyear)cout<<year<<&quo......