You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ashish Kulkarni <as...@gmail.com> on 2009/07/07 18:06:10 UTC

[OT] Return Hashmap java object from servlet

HiIs it possible to return Hashmap object from java Servlet,  What kind of
ContentType can i set to the response?

The other option is to set data from HashMap into XML and return it as XML
file.

for example.

I have servlet like below

public class ReturnObjectServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{

processResponse(request, response);
}

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

processResponse(request, response);
}

private void processResponse(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{

           Map map1 = new HashMap();
   map1.put("ABC", "XYZ");
   map1.put("ABC2", "XYZ");
   map1.put("ABC3", "XYZ");

        // What kind of response type can i set??
         response.setContentType("text/xml; charset=UTF-8");
         PrintWriter out = response.getWriter();
         out.print(map1);
         out.close();

        }


}

Re: [OT] Return Hashmap java object from servlet

Posted by Nils-Helge Garli Hegvik <ni...@gmail.com>.
What exactly are you trying to do? Do you want to serialize the
hashmap to the client as a Java object, or do you want it converted to
xml? What does the consuming end expect? If you really want to produce
xml, take a look at one of the many java <-> xml libraries that exists
(although, something as simple as your example, you could probably
construct the xml "by hand"). A google search should give you more
than enough information.

Nils-H

On Tue, Jul 7, 2009 at 6:06 PM, Ashish
Kulkarni<as...@gmail.com> wrote:
> HiIs it possible to return Hashmap object from java Servlet,  What kind of
> ContentType can i set to the response?
>
> The other option is to set data from HashMap into XML and return it as XML
> file.
>
> for example.
>
> I have servlet like below
>
> public class ReturnObjectServlet extends HttpServlet
> {
> public void doGet(HttpServletRequest request, HttpServletResponse response)
> throws ServletException, IOException
> {
>
> processResponse(request, response);
> }
>
> public void doPost(HttpServletRequest request, HttpServletResponse response)
> throws ServletException, IOException
> {
>
> processResponse(request, response);
> }
>
> private void processResponse(HttpServletRequest request,
> HttpServletResponse response) throws ServletException, IOException
> {
>
>           Map map1 = new HashMap();
>   map1.put("ABC", "XYZ");
>   map1.put("ABC2", "XYZ");
>   map1.put("ABC3", "XYZ");
>
>        // What kind of response type can i set??
>         response.setContentType("text/xml; charset=UTF-8");
>         PrintWriter out = response.getWriter();
>         out.print(map1);
>         out.close();
>
>        }
>
>
> }
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [OT] Return Hashmap java object from servlet

Posted by Musachy Barroso <mu...@gmail.com>.
was this a question?

musachy

On Tue, Jul 7, 2009 at 9:06 AM, Ashish
Kulkarni<as...@gmail.com> wrote:
> HiIs it possible to return Hashmap object from java Servlet,  What kind of
> ContentType can i set to the response?
>
> The other option is to set data from HashMap into XML and return it as XML
> file.
>
> for example.
>
> I have servlet like below
>
> public class ReturnObjectServlet extends HttpServlet
> {
> public void doGet(HttpServletRequest request, HttpServletResponse response)
> throws ServletException, IOException
> {
>
> processResponse(request, response);
> }
>
> public void doPost(HttpServletRequest request, HttpServletResponse response)
> throws ServletException, IOException
> {
>
> processResponse(request, response);
> }
>
> private void processResponse(HttpServletRequest request,
> HttpServletResponse response) throws ServletException, IOException
> {
>
>           Map map1 = new HashMap();
>   map1.put("ABC", "XYZ");
>   map1.put("ABC2", "XYZ");
>   map1.put("ABC3", "XYZ");
>
>        // What kind of response type can i set??
>         response.setContentType("text/xml; charset=UTF-8");
>         PrintWriter out = response.getWriter();
>         out.print(map1);
>         out.close();
>
>        }
>
>
> }
>



-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org