Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7940983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:31:42+00:00 2026-06-03T23:31:42+00:00

In my project, for inserting, updating and deleting the data I am requesting to

  • 0

In my project, for inserting, updating and deleting the data I am requesting to servlet class and then it process and get back the response.

It all happen via jQuery ajax.

Right now it response only success or failure as follows

PrintWriter out =   response.getWriter();
out.println("<custom message>");

now i want to make that message more meaningful

By default i will accept 1 parameter called "format", if the format is null then by default that particular servlet class will respond in json format else only 2 options are going to be there json and xml.

then i need to set response.setContentType("application/json"); and etc.

So i will make one servlet class as follows

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class myservlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,java.io.IOException
    {
        String format   =   req.getParameter("format");

        if(format == null)
        {
            format  =   "json";
        }
        else
        {
            if(format.equals("json"))
            {
                resp.setContentType("application/json");
            }
            else if(format.equals("xml"))
            {
                resp.setContentType("application/rss+xml");
            }
            else
            {
                //error
            }
        }
    }

    public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,java.io.IOException
    {
        String format   =   req.getParameter("format");

        if(format == null)
        {
            format  =   "json";
        }
        else
        {
            if(format.equals("json"))
            {
                resp.setContentType("application/json");
            }
            else if(format.equals("xml"))
            {
                resp.setContentType("application/rss+xml");
            }
            else
            {
                //error
            }
        }
    }

}

and this above class is extended as follows

import java.io.PrintWriter;

import javax.servlet.ServletException;

public class abc extends myservlet
{
    private static final long serialVersionUID = 1L;

    public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,java.io.IOException
    {
        PrintWriter out     =   resp.getWriter();
            out.println("{/"id/": /"file/"}");
        //response must be converted to either json or to xml
    }
}

will it possible..??

how can i convert response to either xml or json dynamically…??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:31:43+00:00Added an answer on June 3, 2026 at 11:31 pm

    Don’t do this in servlet. This is good occasion to create a filter which will do your converting job.

    1. Filter uses ServletResponseWrapper and pass it to the servlet
    2. Servlet saves a response
    3. Filter checks parameter and delegate to appropriate converter (json, xml) – postprocessing.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My normal process for inserting into one table and then getting the ID back
I'm working on a project that requires a fair bit of inserting/updating rows in
I have all my views in a project inheriting from a ViewBase class that
I have an issue with a datagrid inserting/updating rows twice. The datagrid is bound
When inserting an object into a database with SQLAlchemy, all it's properties that correspond
My project uses struts,hibernate and spring,oracle db ,WebLogic server. I have a transaction inserting
Have created a ATL COM project through which I am inserting Menu Items to
I am currently working on a project for traversing an excel document and inserting
Background: I have a project that is not at all large scale (2 ASP.NET
Updating someone else's old PHP project and I'm unfamiliar with regular expressions. Question one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.