Link to home
Start Free TrialLog in
Avatar of princehyderabad
princehyderabad

asked on

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); %>
Avatar of Mick Barry
Mick Barry
Flag of Australia image

out.println(java.util.Arrays.asList(str));
Avatar of princehyderabad
princehyderabad

ASKER

This the error:

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

Are u expecting to code something like this??

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

Output is: null
Object object =  request.getAttribute(MMServletConstants.PARAM_USER_LIST);
out.println(object);
Still Output is: null
let me know how you are working with servlet and jsp??
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);
            }
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]);
           }
}

%>


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]);
           }
}

%>
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]);
           }
}

%>
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
It's my mistake

Type cast it

Object []object = (Object[])session.getAttribute(MMServletConstants.PARAM_USER_LIST);
Even for

first solution

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


BTW

Are u from India Hyderabad or Pakistan Hyderabad?? Just curious
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]);
           }
}

%>
I'm trying you solutions ...hold....

I'm from Hyderabad(India) ..
Black Ouput, when I tried to put System.out.println(object);
I got  "null"
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]);
           }
}
%>
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
what happen sudhakar ???
ASKER CERTIFIED SOLUTION
Avatar of sudhakar_koundinya
sudhakar_koundinya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...
can u post MMUsers class code??
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
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();
    }
  }
}
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
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
no there is nothing to do with  that third party codes

and custom tags are much better than beans
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