English 中文(简体)
将服务器类别扩大到定制类别,以便进行更加定制的反应
原标题:Extending the servlet class to custom class for more customised response

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.

这一切都是通过 j子发生的。

现在,它只应对以下成功或不足:

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

我现在想使这一信息更有意义

如果格式为<代码>null,则一经违约即接受1个参数,即:<编码>条形>。 否则,只有2种备选办法,即<代码>json <>/code>和xml

然后需要制定<代码>response.setContentType(“application/json”);等。

因此,一将按以下方式开设一个服务器级:

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
            }
        }
    }

}

以上职等如下:

进口java.io。 打印机;

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
    }
}

能否做到这一点?

如何转换对xmljson的答复 是否有活力?

最佳回答

页: 1 这是一个很好的机会,可以建立一个能够转换工作的过滤器。

  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.
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签