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
}
}
能否做到这一点?
如何转换对xml
或json
的答复 是否有活力?