Solved

Array Display

Posted on 2004-08-19
34
228 Views
Last Modified: 2010-04-01
This the servlet which I hv to use and get all values displayed in JSP. This servlet give you the Array of UserList.

.....
HttpSession session = request.getSession();
            String name = (String) session.getAttribute(mm_userName);
            try {
                  MMUser [] users = getUsers(name);
                  request.setAttribute(mm_userList, users);
            } catch (MMFailedUserActionException e) {
                  throw new ServletException(e);
            }
....

I'm trying with this code in JSP: Can anyone help to get retriver and display.
<%
String str[] =  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);
out.println(str); %>
0
Comment
Question by:princehyderabad
  • 16
  • 15
34 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11849050
out.println(java.util.Arrays.asList(str));
0
 

Author Comment

by:princehyderabad
ID: 11853622
This the error:

incompatible types found   : java.lang.Object
required: java.lang.String[]
String str[] =  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);
                                                ^
1 error
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11854492
>>String str[] =  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);

Should be

Object object =  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);

                                   OTHERWISE

if it is a String
String str =(String)  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);

0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11854533
Are u expecting to code something like this??

<%
String str =(String)  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);
MMUsers[] users=(MMUsers[])request.getAttribute(str);
%>
0
 

Author Comment

by:princehyderabad
ID: 11854664
Error
cannot resolve symbol symbol  : class MMUsers
0
 

Author Comment

by:princehyderabad
ID: 11854682
For your code this:
Object object =  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);
out.println(object);

Output is: null
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11855095
Object object =  request.getAttribute(MMServletConstants.PARAM_USER_LIST);
out.println(object);
0
 

Author Comment

by:princehyderabad
ID: 11855111
Still Output is: null
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11855411
let me know how you are working with servlet and jsp??
0
 

Author Comment

by:princehyderabad
ID: 11856431
This is my servlet which is talking to DB and getting userlist, now I want o get the userlist displays in JSP page. Plz send me jsp code for that.

      public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

            // extract request parameters
            HttpSession session = request.getSession();
            String name = (String) session.getAttribute(MMServletConstants.PARAM_USER_NAME);
            try {
                  MMUser [] users = getUsers(name);
                  request.setAttribute(MMServletConstants.PARAM_USER_LIST, users);
            } catch (MMFailedUserActionException e) {
                  throw new ServletException(e);
            }
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11856483
OK you need to do this


 public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

          // extract request parameters
          HttpSession session = request.getSession();
          String name = (String) session.getAttribute(MMServletConstants.PARAM_USER_NAME);
          try {
               MMUser [] users = getUsers(name);
               session.setAttribute(MMServletConstants.PARAM_USER_LIST, users);
               
          } catch (MMFailedUserActionException e) {
               throw new ServletException(e);
          }



<%
 Object []object = session.getAttribute(MMServletConstants.PARAM_USER_LIST);

if(object!=null)
{
          for(int i=0;i<object.length;i++)
          {
                   out.println(object[i]);
           }
}

%>


0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11856501
or

Servlet
========

 public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

          // extract request parameters
          HttpSession session = request.getSession();
          String name = (String) session.getAttribute(MMServletConstants.PARAM_USER_NAME);
          try {
               MMUser [] users = getUsers(name);
               request.setAttribute(MMServletConstants.PARAM_USER_LIST, users);
                RequestDispatcher dispatcher;
            dispatcher = context.getRequestDispatcher(YourJSPPage);
            dispatcher.forward(req, res);
               
          } catch (MMFailedUserActionException e) {
               throw new ServletException(e);
          }


JSP
=====
      
Comment from objects
Date: 08/20/2004 09:07AM IST
      Comment       

out.println(java.util.Arrays.asList(str));

Comment from princehyderabad
Date: 08/20/2004 10:52PM IST
      Author Comment       

This the error:

incompatible types found   : java.lang.Object
required: java.lang.String[]
String str[] =  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);
                                                ^
1 error

Comment from sudhakar_koundinya
Date: 08/21/2004 12:05AM IST
      Your Comment       

>>String str[] =  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);

Should be

Object object =  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);

                                   OTHERWISE

if it is a String
String str =(String)  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);


Comment from sudhakar_koundinya
Date: 08/21/2004 12:08AM IST
      Your Comment       

Are u expecting to code something like this??

<%
String str =(String)  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);
MMUsers[] users=(MMUsers[])request.getAttribute(str);
%>

Comment from princehyderabad
Date: 08/21/2004 12:18AM IST
      Author Comment       

Error
cannot resolve symbol symbol  : class MMUsers

Comment from princehyderabad
Date: 08/21/2004 12:19AM IST
      Author Comment       

For your code this:
Object object =  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);
out.println(object);

Output is: null

Comment from sudhakar_koundinya
Date: 08/21/2004 01:05AM IST
      Your Comment       

Object object =  request.getAttribute(MMServletConstants.PARAM_USER_LIST);
out.println(object);

Comment from princehyderabad
Date: 08/21/2004 01:06AM IST
      Author Comment       

Still Output is: null

Comment from sudhakar_koundinya
Date: 08/21/2004 01:38AM IST
      Your Comment       

let me know how you are working with servlet and jsp??

Comment from princehyderabad
Date: 08/21/2004 03:34AM IST
      Author Comment       

This is my servlet which is talking to DB and getting userlist, now I want o get the userlist displays in JSP page. Plz send me jsp code for that.

     public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

          // extract request parameters
          HttpSession session = request.getSession();
          String name = (String) session.getAttribute(MMServletConstants.PARAM_USER_NAME);
          try {
               MMUser [] users = getUsers(name);
               request.setAttribute(MMServletConstants.PARAM_USER_LIST, users);
          } catch (MMFailedUserActionException e) {
               throw new ServletException(e);
          }

Comment from sudhakar_koundinya
Date: 08/21/2004 03:44AM IST
      Your Comment       

OK you need to do this


 public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

          // extract request parameters
          HttpSession session = request.getSession();
          String name = (String) session.getAttribute(MMServletConstants.PARAM_USER_NAME);
          try {
               MMUser [] users = getUsers(name);
               session.setAttribute(MMServletConstants.PARAM_USER_LIST, users);
               
          } catch (MMFailedUserActionException e) {
               throw new ServletException(e);
          }



<%
 Object []object = session.getAttribute(MMServletConstants.PARAM_USER_LIST);

if(object!=null)
{
          for(int i=0;i<object.length;i++)
          {
                   out.println(object[i]);
           }
}

%>
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11856512
OOPS

Ignore previous one

Solution 1


Servlet
========

 public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

          // extract request parameters
          HttpSession session = request.getSession();
          String name = (String) session.getAttribute(MMServletConstants.PARAM_USER_NAME);
          try {
               MMUser [] users = getUsers(name);
               request.setAttribute(MMServletConstants.PARAM_USER_LIST, users);
                RequestDispatcher dispatcher;
          dispatcher = context.getRequestDispatcher(YourJSPPage);
          dispatcher.forward(req, res);
               
          } catch (MMFailedUserActionException e) {
               throw new ServletException(e);
          }


JSP
=====
     <%
 Object []object = request.getAttribute(MMServletConstants.PARAM_USER_LIST);

if(object!=null)
{
          for(int i=0;i<object.length;i++)
          {
                   out.println(object[i]);
           }
}

%>



Solution 2



Servlet
========

 public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

          // extract request parameters
          HttpSession session = request.getSession();
          String name = (String) session.getAttribute(MMServletConstants.PARAM_USER_NAME);
          try {
               MMUser [] users = getUsers(name);
               session.setAttribute(MMServletConstants.PARAM_USER_LIST, users);

               
          } catch (MMFailedUserActionException e) {
               throw new ServletException(e);
          }


JSP
=====
     <%
 Object []object = session.getAttribute(MMServletConstants.PARAM_USER_LIST);

if(object!=null)
{
          for(int i=0;i<object.length;i++)
          {
                   out.println(object[i]);
           }
}

%>
0
 

Author Comment

by:princehyderabad
ID: 11856572
I did for Solution 2:

incompatible types found   : java.lang.Object
required: java.lang.Object[] Object []object = session.getAttribute(MMServletConstants.PARAM_USER_LIST);
                                                                                               ^
1 error
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11856633
It's my mistake

Type cast it

Object []object = (Object[])session.getAttribute(MMServletConstants.PARAM_USER_LIST);
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11856650
Even for

first solution

Object []object = (Object[])request.getAttribute(MMServletConstants.PARAM_USER_LIST);


BTW

Are u from India Hyderabad or Pakistan Hyderabad?? Just curious
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11856735
if that does not work

Here is a modified solution

Servlet
========

 public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

          // extract request parameters
          HttpSession session = request.getSession();
          String name = (String) session.getAttribute(MMServletConstants.PARAM_USER_NAME);
          try {
               MMUser [] users = getUsers(name);
               session.setAttribute(MMServletConstants.PARAM_USER_LIST,java.util.Arrays.asList( users));

               
          } catch (MMFailedUserActionException e) {
               throw new ServletException(e);
          }


JSP
=====
     <%
 Object []object =((java.util.List) session.getAttribute(MMServletConstants.PARAM_USER_LIST)).toArray();

if(object!=null)
{
          for(int i=0;i<object.length;i++)
          {
                   out.println(object[i]);
           }
}

%>
0
 

Author Comment

by:princehyderabad
ID: 11856979
I'm trying you solutions ...hold....

I'm from Hyderabad(India) ..
0
 

Author Comment

by:princehyderabad
ID: 11857007
Black Ouput, when I tried to put System.out.println(object);
I got  "null"
0
 

Author Comment

by:princehyderabad
ID: 11857057
Oops I guess why its null, because its bascially not at all going calling servlet. Please help to solve here:

<%@ page import="com.mid.user.MMUser, com.mid.servlet.*" %> [will this not call the serlvet or include or make servlet run and get ouput]
<%@ page import="java.net.*, java.io.*"%>
<%@ page session="true" %>
<%
Object []object = (Object[])session.getAttribute(MMServletConstants.PARAM_USER_LIST);
System.out.println("Testing on Console: " + object);
if(object!=null)
{
          for(int i=0;i<object.length;i++)
          {
                   out.println(object[i]);
           }
}
%>
0
 

Author Comment

by:princehyderabad
ID: 11857087
I included the serlvet in the JSP
<jsp:include page="/admin/GetUsers" />

Outputs:
JSP Output:  "com.mid.mm.user.MMUser@ffd553 "
Console Output: User List: [Lcom.midstream.mm.user.MMUser;@1685e30
0
 

Author Comment

by:princehyderabad
ID: 11908266
what happen sudhakar ???
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 125 total points
ID: 11909875
I was busy with some other works

Try this

Servlet
========

 public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

          // extract request parameters
          HttpSession session = request.getSession(true);
          String name = (String) session.getAttribute(MMServletConstants.PARAM_USER_NAME);
          try {
               MMUser [] users = getUsers(name);
               session.setAttribute(MMServletConstants.PARAM_USER_LIST,java.util.Arrays.asList( users));

               
          } catch (MMFailedUserActionException e) {
               throw new ServletException(e);
          }


JSP
=====
     <%
 Object []object =((java.util.List) session.getAttribute(MMServletConstants.PARAM_USER_LIST)).toArray();

if(object!=null)
{
          for(int i=0;i<object.length;i++)
          {
                   out.println(object[i]);
           }
}

%>
0
 

Author Comment

by:princehyderabad
ID: 11915416
This is the OUTPUT:

com.mid.mm.user.MMUser@1f01b29
com.mid.mm.user.MMUser@8721bd
com.mid.mm.user.MMUser@1b81d4f

Its showing object.length =3 which is correct, but not displaying the proper values...
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11915873
can u post MMUsers class code??
0
 

Author Comment

by:princehyderabad
ID: 11915961
Hey Sudhakar I got it, but now you help me how to do same in beans. I think using bean it would be more easy and small code.
Idea is here to display the ALL:  username, password, fullname, role  in the table format.

<jsp:include page="/admin/GetUsers" />
<%
   MMUser []userlist = (MMUser[])session.getAttribute(MMServletConstants.PARAM_USER_LIST);
if(userlist!=null)
{
          for(int i=0;i<userlist.length;i++)
          {
                   out.println(userlist[i].getName());
                   out.println("<br>");
           }
}
%>

rEGARDS
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11916306
Do you know custom tags??


if you know this becomes much easy for you

//import com.mid.mm.user.*;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import javax.servlet.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class MMCustomTag extends SimpleTagSupport {
  private String html = null;
 
  public MMCustomTag() {
    super();
  }
 
  public void setHtml(String name) {
    this.html = name;
  }
  public void doTag() {
    try {
 
     
      PageContext context = (PageContext)getJspContext();
      ServletRequest request=context.getRequest();
      ServletResponse response=context.getResponse();
      PrintWriter out=response.getWriter();
      HttpSession session=context.getSession();
     
        MMUser []userlist = (MMUser[])session.getAttribute(MMServletConstants.PARAM_USER_LIST);
        if(userlist!=null)
        {
          for(int i=0;i<userlist.length;i++)
          {
                String name=userlist[i].getName();
                String password=userlist[i].getPassword();
                String role=userlist[i].getRole();
                String _html=""+html;
                html=html.replaceAll("MM::USER",name);
                html=html.replaceAll("MM::PASS",password);
                html=html.replaceAll("MM::ROLE",role);
                out.println(html);
                html=new String(_html);
                   
           }
        }
     
    } catch (Exception ex) {
        ex.printStackTrace();
    }
  }
}
0
 

Author Comment

by:princehyderabad
ID: 11916357
oops sudhakar, no just tell me how to do it in JSP using jsp:bean.

Reason is I have this biz logic class (servlet) which I have to make use. I didnt do that big logic class, somebody else worte I hv to use jsp and use that biz logic class.
Please just tell me hwo to do it using JSP bean:


Right now this code is working fine with correct output:
<jsp:include page="/admin/GetUsers" />
<%
   MMUser []userlist = (MMUser[])session.getAttribute(MMServletConstants.PARAM_USER_LIST);
if(userlist!=null)
{
          for(int i=0;i<userlist.length;i++)
          {
                   out.println(userlist[i].getName());
                   out.println("<br>");
           }
}
%>

getName(), getfullName, getPassword(), getRole() are the methods already defined just used them as I have used getName() in above code.

regards
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11916367
And in jsp code you must call that tag simply like this

<jsp:include page="/admin/GetUsers" />
<MMCustomTag  html="<YOUR HTML TEMPLATE>" />

that's all
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11916445
no there is nothing to do with  that third party codes

0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11916450
and custom tags are much better than beans
0
 

Author Comment

by:princehyderabad
ID: 11916520
Okay 2 questions:

1: <MMCustomTag  html="<YOUR HTML TEMPLATE>" />

what should be "Your HTML Template". Please tell in deatil


2: If there is a servlet (business logic) which is there already which talk to DB and gets all values and store in MMUser so is it good idea again to write servlet(your custom tag) and again in JSP pass 2 lines code ? Also I wanna display in nice row & colomns that isnt a pain in servlet to code html, in JSP much easy correct ?
If I'm wrong plz explain me ? Why and When

Regards
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
HOW TO: Upload an ISO image to a VMware datastore for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere Host Client, and checking its MD5 checksum signature is correct.  It's a good idea to compare checksums, because many installat…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now