request.getAttribute( "result");和request.setAttribute("result",username);

本文详细介绍了如何在request对象中设置属性以及如何在后续的JSP页面中获取这些属性值。通过实例说明了request.setAttribute方法的用法,并对比了与session的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

request.setAttribute("result",username);
在request对象中加入名为result的属性并附值为username,因为request对象是可以更改的,你可以
在同一个请求中象这样访问这个属性。

虽然类似session,但与session是有所区别的,request.setAttribute设置的属性只能在当前
request只使用,比如你在Action中设置result属性,需要到jsp页面中读取:
request.setAttribute("result",username);
requests.getRequestDispatcher("result.jsp").forward(request, response);
jsp页面获取该值:
request.getAttribute( "result");

因为一同将当前action的request与response对象都发送过来,相当于直接操作自身页面。


request.setAttribute("result",username);
request.getRequestDispatcher("result.jsp").forward(request,response);
requset.getAttribute("result");

转载于:https://www.cnblogs.com/zzzzw/p/4849731.html

package sfw.xmut.guomao.aspect; import sfw.xmut.guomao.entity.OperationLog; import sfw.xmut.guomao.mapper.OperationLogMapper; import sfw.xmut.guomao.utils.ServletUtil; import jakarta.servlet.http.HttpServletRequest; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import java.time.LocalDateTime; @Aspect @Component public class OperationLogAspect { private final OperationLogMapper logMapper; public OperationLogAspect(OperationLogMapper logMapper) { this.logMapper = logMapper; } @Around("execution(* sfw.xmut.guomao.controller.*.*(..))") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); HttpServletRequest request = attributes.getRequest(); if (request.getRequestURI().endsWith("/operationLog/page")) { return joinPoint.proceed(); } long startTime = System.currentTimeMillis(); Object result = joinPoint.proceed(); long spendTime = System.currentTimeMillis() - startTime; OperationLog log = new OperationLog(); log.setUserName("admin"); // TODO: Get from JWT token log.setOperationType(request.getRequestURI()); log.setOperationMethod(request.getMethod()); log.setRequestUrl(request.getRequestURL().toString()); log.setOperationIp(ServletUtil.getClientIP(request)); log.setOperationStatus("成功"); log.setLastOperationTime(LocalDateTime.now()); logMapper.insert(log); return result; } }log.setUserName("admin"); // TODO: Get from JWT token 这一行代码怎么把他改成不管哪个用户登录这个管理系统日志上面都可以更新,不要像这个admin锁住固定住不管谁登录都是显示他的名称
06-19
package com.cissst.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.cissst.entity.Admin; import com.cissst.entity.CustomAccount; import com.cissst.service.IAdminService; import com.cissst.service.ICustomAccountService; import com.cissst.service.impl.AdminServiceImpl; import com.cissst.service.impl.CustomAccountServiceImpl; import com.cissst.util.MD5Util; public class UserServlet extends HttpServlet{ @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); String action = request.getParameter("action"); IAdminService as = new AdminServiceImpl(); ICustomAccountService cs = new CustomAccountServiceImpl(); HttpSession session = request.getSession(); if("login".equals(action)){ String name = request.getParameter("username"); String password = MD5Util.encode(request.getParameter("password")); String usertype = request.getParameter("usertype"); Admin a = as.findBynp(name, password); CustomAccount c = cs.findBynp(name, password); if(a != null){ String n = a.getName(); String p = a.getPassword(); if(n.equals(name) && p.equals(password)&&"admin".equals(usertype)){ session.setAttribute("admin", a); response.sendRedirect("index.jsp"); }else{ response.getWriter().write("<script charset='UTF-8'>alert(\"用户名或密码错误!\");" + "location.href='index.jsp';</script>"); } }else if(c != null){ String n = c.getUsername(); String p = c.getPassword(); if(n.equals(name) && p.equals(password)&&"user".equals(usertype)){ session.setAttribute("custom_Account", c); response.sendRedirect("index2.jsp"); }else{ response.getWriter().write("<script charset='UTF-8'>alert(\"用户名或密码错误!\");" + "location.href='index.jsp';</script>"); } }else{ response.getWriter().write("<script charset='UTF-8'>alert(\"用户名或密码错误!\");" + "location.href='index.jsp';</script>"); } }else if("logout".equals(action)){ session.invalidate(); //if (request.getSession(false)==null) System.out.println(123); response.sendRedirect("login.jsp"); }else if("relogin".equals(action)){ session.invalidate(); response.sendRedirect("login.jsp"); } } }
最新发布
08-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值