Results 1 to 11 of 11
  1. #1
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default How to set retrieved values from arrayList in html on different places?

    Hey,

    how to set retrieved values from ArrayList in html on different places

    for e.g

    i have two values and i want to print it in a Div

    one value is on Left side other is Right how is it possible?

    Thanks...

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,960
    Rep Power
    31

    Default Re: How to set retrieved values from arrayList in html on different places?

    Is this a html question?
    Do you have the html code to do what you want and are asking how to write a java program to create that html?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: How to set retrieved values from arrayList in html on different places?

    Java Code:
    try{
                
               Class.forName("com.mysql.jdbc.Driver");
               Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false","root","password");
               PreparedStatement pst=con.prepareStatement("select name,age from irc_rough");
               ResultSet rs=pst.executeQuery();
               
            
            ArrayList<String> al=new ArrayList<String>();
            while(rs.next()){
            al.add(rs.getString("name"));
            al.add(rs.getString("age"));
            }
            int i=0;
            while(i<al.size()){    
                
              pw.println("<html>");
              pw.println("<head>");
              pw.println("</head>");
              pw.println("<style>");
              pw.println(".test{");
              pw.println("background-color:yellow;");  
              pw.println("width:300;");  
              pw.println("height:150;");
              
              
              
              
              pw.println("}");
              
              
              
              pw.println("</style>");
              pw.println("<body>");
              pw.println("<div class='test'>");
              
              pw.println("<p>"+al.get(i).toString()+"</p>");           
              i++;
              pw.println("<p>"+al.get(i).toString()+"</p>");
              i++;
              
             
              pw.println("</div>");
              pw.println("</body>"); 
              pw.println("</html>");
                
              
            }
            
        }
            catch(Exception e){}
    }

    Here it prints two values but i want to print them on different place Like first value is on left and second one is in right side
    Last edited by vknehra10; 01-25-2018 at 05:19 PM.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,977
    Rep Power
    8

    Default Re: How to set retrieved values from arrayList in html on different places?

    This is a HTML question. Use tables, divs, whatever. There's a million tutorials out there on HTML and CSS positioning, JSP, templates, the lot.

    On a side note, stop writing to the PrintWriter in a servlet and use JSP's.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: How to set retrieved values from arrayList in html on different places?

    what if i have to make this program in servlet and complete this POSITIONING TASK...
    Please help dont do this

    Thanks....

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: How to set retrieved values from arrayList in html on different places?

    When it comes to HTML layout work it's probably easiest to work from an HTML file and get that in the right layout.

    To do that I recommend loading the HTML dummy file in a browser, with your two divs or whatever, and open the developer tools (F12 in most browsers).
    In Chrome you can use the Elements tab to set style values until you have a layout that's what you need.

    Then use that to write your code around.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: How to set retrieved values from arrayList in html on different places?

    But how to set this on different position
    Java Code:
     pw.println("<div class='test'>");
               
              pw.println("<p>"+al.get(i).toString()+"</p>");           
              i++;
              pw.println("<p>"+al.get(i).toString()+"</p>");
              i++;
    this one array contains two values now i want to position both of these....

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: How to set retrieved values from arrayList in html on different places?

    Which is an HTML thing.
    Create an HTML page (a file) with dummy data and play around with that using the developer tools in something like Chrome.

    That's the way to learn how to do these things, and get the layout you want.

    One thing, since you're laying out two bits of text I would not use <p>'s.
    Use <div>'s or, if there's a lot and it makes sense, maybe <li>'s inside an ordered or unordered list.
    <div>'s will be simpler for only two items, and set the align style attribute.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,977
    Rep Power
    8

    Default Re: How to set retrieved values from arrayList in html on different places?

    Oh come on, can't you at least make ONE bit of effort?

    https://duckduckgo.com/?q=place+div+...&t=ffnt&ia=web

    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  10. #10
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: How to set retrieved values from arrayList in html on different places?

    I am not sure whether this is a html question. I think OP asks how he can display elements of the same array in two different divs in the jsp file.
    Answer is that you have to split up the collection into two lists and display the respective list in each <div>. Inside that <div> you loop over the collection for that specific <div>. How you loop over that collection depends on your framework again - if you are using jstl it will be a
    <c:forEach></c:forEach> loop.
    Inside your controller servlet you create the list to be displayed and pass it to the jsp page via request.setAttribute() method.
    So in your controller you would probably see something like this:
    Java Code:
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String page=null;
    List<String> showList = new ArrayList<String>();
    showList.add("First");
    showList.add("Second");
    showList.add("Third");
    request.setAttribute("showList", showList);
    page="/listshowcase.jsp";		
    getServletContext().getRequestDispatcher(page).forward(request, response);
    	}
    And in the jsp page you would find something like:
    Java Code:
    <c:forEach items="${showList}" var="myitem" >
    	<tr>
    		<td><div align="center"/><c:out value="${myitem}"/></div></td>
    	</tr>
    </c:forEach>
    Let us know whether this is what you had in mind.

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,977
    Rep Power
    8

    Default Re: How to set retrieved values from arrayList in html on different places?

    @vknehra10: People have askedf you questions, given you suggestions. The amount of feedback that you're giving is about zero. That is really demotivating for people to help you. People here are actually willing to help, believe it or not, but we expect some effort from you too. Benji here even posted what he thinks you're after. If that actually was the case, a simple "thanks, it works now" would be nice. If it was not, or you didn't understand it, please say so.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. simple page that pings an ip address retrieved from the database displays in HTML
    By msaikiranreddy in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 06-20-2013, 10:34 PM
  2. To display arraylist values in jsp
    By manideep.d132 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-09-2011, 03:59 AM
  3. ArrayList find values
    By ralf in forum New To Java
    Replies: 39
    Last Post: 07-07-2011, 12:08 AM
  4. Reading values between HTML tags.
    By bholzer in forum New To Java
    Replies: 6
    Last Post: 05-03-2011, 03:25 AM
  5. Getting HTML form values in Servlet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-17-2007, 09:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •