刚开始学习web编程,真诚求教各位大虾。
比如我在java里面有个Map,里面有user=‘admin’,password=‘12345’。
我想把user和password的值显示在jsp页面上。该用什么方法。
求具体代码 我用的tomcat

解决方案 »

  1.   

    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>
      

  2.   

    楼上那位map那样存进去吗?
    Map map=new HashMap();
     map.put("user","admin");
     map.put("password","12345");
      

  3.   

    楼上那样能存进去吗?
    Map map=new HashMap();
    map.put("user","admin");
    map.put("password","12345");
      

  4.   

    楼上那样能存进去吗?
    Map map=new HashMap();
    map.put("user","admin");
    map.put("password","12345");
      

  5.   

    request.setAttribute("ddd",map);用的时候${map.XX}
      

  6.   

    在web中传值,应该是jsp和servlet之间进行传值吧
      

  7.   

    楼主Java里面是指什么,servlet?action?还是普通的类?如果是普通类就像1楼实现就可以了,如果是servlet或者action,可以在类里面把map储存到request里面:request.setAttribute("map",map);在jsp页面里面取出来Map map=(Map)request.getAttribute("map"),展示仿照1楼搞定
      

  8.   

    楼主使用什么框架没?
    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表达式
    很多方式都可以
      

  9.   

    <%@ 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>
      

  10.   

    楼上都说差不多了,我觉得其实就是把你要的数据放到web容器里面,你可以看看web容器的几个作用域,然后怎么取就随便了,很多方法,就一个一个web数据传输了。
      

  11.   

    LS 说了好多,我补充下,跳转方式要注意,得把request带到你的页面上去。 各个框架跳转方法不同, 自个查查吧。