Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

Array Display

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
princehyderabad
Asked:
princehyderabad
  • 16
  • 15
1 Solution
 
objectsCommented:
out.println(java.util.Arrays.asList(str));
0
 
princehyderabadAuthor Commented:
This the error:

incompatible types found   : java.lang.Object
required: java.lang.String[]
String str[] =  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);
                                                ^
1 error
0
 
sudhakar_koundinyaCommented:
>>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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sudhakar_koundinyaCommented:
Are u expecting to code something like this??

<%
String str =(String)  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);
MMUsers[] users=(MMUsers[])request.getAttribute(str);
%>
0
 
princehyderabadAuthor Commented:
Error
cannot resolve symbol symbol  : class MMUsers
0
 
princehyderabadAuthor Commented:
For your code this:
Object object =  getServletContext().getAttribute(MMServletConstants.PARAM_USER_LIST);
out.println(object);

Output is: null
0
 
sudhakar_koundinyaCommented:
Object object =  request.getAttribute(MMServletConstants.PARAM_USER_LIST);
out.println(object);
0
 
princehyderabadAuthor Commented:
Still Output is: null
0
 
sudhakar_koundinyaCommented:
let me know how you are working with servlet and jsp??
0
 
princehyderabadAuthor Commented:
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
 
sudhakar_koundinyaCommented:
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
 
sudhakar_koundinyaCommented:
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
 
sudhakar_koundinyaCommented:
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
 
princehyderabadAuthor Commented:
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
 
sudhakar_koundinyaCommented:
It's my mistake

Type cast it

Object []object = (Object[])session.getAttribute(MMServletConstants.PARAM_USER_LIST);
0
 
sudhakar_koundinyaCommented:
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
 
sudhakar_koundinyaCommented:
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
 
princehyderabadAuthor Commented:
I'm trying you solutions ...hold....

I'm from Hyderabad(India) ..
0
 
princehyderabadAuthor Commented:
Black Ouput, when I tried to put System.out.println(object);
I got  "null"
0
 
princehyderabadAuthor Commented:
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
 
princehyderabadAuthor Commented:
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
 
princehyderabadAuthor Commented:
what happen sudhakar ???
0
 
sudhakar_koundinyaCommented:
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
 
princehyderabadAuthor Commented:
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
 
sudhakar_koundinyaCommented:
can u post MMUsers class code??
0
 
princehyderabadAuthor Commented:
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
 
sudhakar_koundinyaCommented:
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
 
princehyderabadAuthor Commented:
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
 
sudhakar_koundinyaCommented:
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
 
sudhakar_koundinyaCommented:
no there is nothing to do with  that third party codes

0
 
sudhakar_koundinyaCommented:
and custom tags are much better than beans
0
 
princehyderabadAuthor Commented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 16
  • 15
Tackle projects and never again get stuck behind a technical roadblock.
Join Now