新手关于jsp和java之间数据传递的问题 刚开始学习web编程,真诚求教各位大虾。比如我在java里面有个Map,里面有user=‘admin’,password=‘12345’。我想把user和password的值显示在jsp页面上。该用什么方法。求具体代码 我用的tomcat 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java代码:package test;import java.util.HashMap;import java.util.Map;import java.util.Set;public class Test2 { public Map test(){ Map map=new HashMap(); map.put("admin", "12345"); return map; } }jsp代码:<body> <% Map map=new Test2().test(); Set set=map.keySet(); Iterator it=set.iterator(); while(it.hasNext()){ %> <%=it.next() %> <%} Collection co=map.values(); it=co.iterator(); while(it.hasNext()){ %> <%=it.next()%> <% } %> </body> 楼上那位map那样存进去吗?Map map=new HashMap(); map.put("user","admin"); map.put("password","12345"); 楼上那样能存进去吗?Map map=new HashMap();map.put("user","admin");map.put("password","12345"); 楼上那样能存进去吗?Map map=new HashMap();map.put("user","admin");map.put("password","12345"); request.setAttribute("ddd",map);用的时候${map.XX} 在web中传值,应该是jsp和servlet之间进行传值吧 楼主Java里面是指什么,servlet?action?还是普通的类?如果是普通类就像1楼实现就可以了,如果是servlet或者action,可以在类里面把map储存到request里面:request.setAttribute("map",map);在jsp页面里面取出来Map map=(Map)request.getAttribute("map"),展示仿照1楼搞定 楼主使用什么框架没?1. Struts2java后台:request.setAttribute("user",user);request.setAttribute("password",password);jsp页面 :<s:property value="#request.user">;<s:property value="#request.password">;2. Struts1java后台,将用户名保存在相应表中的实体对象中jsp页面:获取实体bean,然后获取对应的属性其他<%request.setgetAttribute("user","user");%><%request.setgetAttribute("password","password");%>或者el表达式很多方式都可以 <%@ page import="test.Test2,java.util.Map,java.util.Iterator" %><body> <% Test2 t = new Test2() ; Map map = t.test() ; Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); Object val = entry.getValue(); %> key:<%=key%> , value:<%=val%> <br /><% } %></body> 楼上都说差不多了,我觉得其实就是把你要的数据放到web容器里面,你可以看看web容器的几个作用域,然后怎么取就随便了,很多方法,就一个一个web数据传输了。 LS 说了好多,我补充下,跳转方式要注意,得把request带到你的页面上去。 各个框架跳转方法不同, 自个查查吧。 User.hbm.xml not found java.lang.ClassCastException: java.math.BigDecimal问题 如何将弹出窗口做成排它的还可以调用父窗口的方法? ---!大家好,用poi生成excel,怎样才能变成点击按钮出现下载的页呢?? 【急求】哪有较好的开源网站下载??【急求】 读取图片问题 在JSP页面读取Sql Server中Text字段 大家测试一下我开发的多用户blog系统(java) 系统业务数据自动导入NC的用到的XMLUtil类在哪,希望有人帮忙解答 useBean问题 ssh框架整合问题(急) 求助struts1简单例子
package test;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;public class Test2 {
public Map test(){
Map map=new HashMap();
map.put("admin", "12345");
return map;
}
}
jsp代码:
<body>
<% Map map=new Test2().test();
Set set=map.keySet();
Iterator it=set.iterator();
while(it.hasNext()){
%>
<%=it.next() %>
<%}
Collection co=map.values();
it=co.iterator();
while(it.hasNext()){
%>
<%=it.next()%>
<% } %>
</body>
Map map=new HashMap();
map.put("user","admin");
map.put("password","12345");
Map map=new HashMap();
map.put("user","admin");
map.put("password","12345");
Map map=new HashMap();
map.put("user","admin");
map.put("password","12345");
1. Struts2
java后台:request.setAttribute("user",user);request.setAttribute("password",password);
jsp页面 :<s:property value="#request.user">;<s:property value="#request.password">;
2. Struts1
java后台,将用户名保存在相应表中的实体对象中
jsp页面:获取实体bean,然后获取对应的属性
其他
<%request.setgetAttribute("user","user");%>
<%request.setgetAttribute("password","password");%>
或者
el表达式
很多方式都可以
<%
Test2 t = new Test2() ; Map map = t.test() ;
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
%>
key:<%=key%> , value:<%=val%> <br />
<%
}
%>
</body>