jsp关于request.setAttribue还有response.addCookie()的两个问题

刚才使用request.getAttribute();时候无法获取到值,一直为null,经过分析,得到下面的

index1.jsp

<%--
  Created by IntelliJ IDEA.
  User: 亦清
  Date: 2019/7/16
  Time: 12:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户登录索引</title>
</head>
<body>
<%
    try{
        int money=100;
        int b=0;
        request.setAttribute("result",money/0);
    }catch (Exception e){
        request.setAttribute("result","页面出现错误");
    }
%>
    <jsp:forward page="login.jsp"/>
<%--<a href="login.jsp?name=321&password=qqqq">跳转界面</a>--%>
</body>
</html>

login.jsp

<%--
  Created by IntelliJ IDEA.
  User: 亦清
  Date: 2019/7/16
  Time: 12:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String name = request.getParameter("name");
    String password = request.getParameter("password");
%>
<html>
<head>
    <title>登陆</title>
</head>
<body>
<%    String result = (String)request.getAttribute("result");
%>
<%=result%>
<form name="form1" action="" method="post">
    用户名 <input type="text" name="name" style="width:120px" value="<%=name%>"><br>
    密&nbsp;&nbsp;&nbsp;码 <input type="password" name="password" value="<%=password%>" style="width:120px"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

1.第一个request.setAttribute();的jsp片段所放的位置必须和login.jsp当中一样,就是要放在body标签就都放在body里面,不然会得到Nul,

2.index.jsp一旦点击a标签跳转到login.jsp,也会获取到null,

很奇怪,暂时放着

第二个,使用response.addCookie(); 的时候,传入的cookie的值必须合法,不能有空格什么的,刚才使用这个方法一直报错,最后发现是java的Date()方法创建的时间转换为字符串之后,中间会有一个空格,就导致了报错,所以用replace(" ",""); 将字符串合法化就可以了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值