小椰奶冻 2022-10-27 19:57 采纳率: 0%
浏览 21

application.getAttribute值为空

想要实现一个在线聊天室
使用application与session
每个用户聊天信息显示用户名并且application保存所有的聊天信息
但是一直用户名和输出的话都是null

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loopy聊天室</title>
</head>
<style>
body {background-color:white;  } 
h1 {color:#fbcccf;
text-align:center;
font-size:40px;
}

h2{text-align:center;}
    
</style>
<body>
<form action="homework6.jsp" method="post">

<h2>
<img src=" D:\photo\3.jpg" width="120" height="120"></img>
</h2>
<h1>
聊天室
<br>
用户名:<input id="usernameID" type="text" name="username"/>
<br>
密码:<input type="text" name="password"/>
<br>
<input type="submit" value="提交"/> 
</h1>

</form>
</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
    String message="聊天区域:";
    %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>聊天室</title>
<style>
.div3{
    background-color:pink;
    border:solid 1px black;
    width:300px;
    height:300px;
    

    line-height:150%
}
img{
    width:15px;
    height:20px;
}
</style>
</head>
<body>
 <div class="div3">


<form action="homework7.jsp" method="post">

<%
 String cy=(String)application.getAttribute("cy");
 if(cy==null){
     out.print("聊天内容:");
 }else{
     out.print("聊天内容:"+cy);
 }
 %>


 <%
 String username=(String)application.getAttribute("username");
out.print("你的用户名是:"+username);
%>
<br>
聊天区域:<input type="text" name="cy"/>
<br>
<input type="submit"value="提交"/></input>

</form>
</div>
</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
     <%
    String message="聊天区域:";
    %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>聊天室</title>
<style>
.div3{
    background-color:pink;
    border:solid 1px black;
    width:300px;
    height:300px;
    

    line-height:150%
}
img{
    width:15px;
    height:20px;
}
</style>
</head>
<body>
 <div class="div3">

<%
request.setCharacterEncoding("UTF-8");
String cy=(String)application.getAttribute("cy");
message=message+cy+".";
out.print("
"
); application.setAttribute("cy",cy); out.print(cy); out.println("聊天内容输入成功,20s后自动跳回聊天页面"); response.setHeader("refresh","20;url='homework6.jsp'"); %>
</div> </body> </html>
  • 写回答

1条回答 默认 最新

  • 夜郎king 2022博客之星IT其它领域TOP 12 2022-10-29 07:58
    关注

    没看到homework6.jsp,也没看到application.setAttribute("username")这步操作;需要把用户名设置到application中吧,没有设置的话,取出来肯定是空的。

    评论

报告相同问题?

问题事件

  • 创建了问题 10月27日

悬赏问题

  • ¥15 pic16F877a单片机的外部触发中断程序仿真失效
  • ¥15 Matlab插值拟合差分微分规划图论
  • ¥15 keil5 target not created
  • ¥15 C/C++数据与算法请教
  • ¥15 怎么找志同道合的伙伴
  • ¥20 如何让程序ab.eXe自已删除干净硬盘里的本文件自己的ab.eXe文件
  • ¥50 爬虫预算充足,跪巨佬
  • ¥15 滑块验证码拖动问题悬赏
  • ¥15 Wanted but not invoked:Actually, there were zero interactions with this moc
  • ¥20 怎么驱动tb6612