package com.neusoft.api;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DataTest5 extends HttpServlet {
/**
* Constructor of the object.
*/
public DataTest5() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String contentType=request.getHeader("Accept");
PrintWriter out = response.getWriter();
if(contentType.contains("application/json"))
{
response.setContentType("application/json;charset=utf-8");
out.println("{\"name\":\"huangbo\",\"age\":39}");
}
else if(contentType.contains("application/xml")||contentType.contains("text/xml"))
{
response.setContentType("text/xml;charset=utf-8");
out.println("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
out.println("<studnet>");
out.println("<name>黄波</name>");
out.println("<age>39</age>");
out.println("</studnet>");
}
//out.println(contentType);
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String contentType=request.getContentType();
PrintWriter out = response.getWriter();
if(contentType.contains("application/json"))
{
response.setContentType("application/json;charset=utf-8");
out.println("{\"name\":\"huangbo\",\"age\":39}");
}
else if(contentType.contains("application/xml")||contentType.contains("text/xml"))
{
response.setContentType("text/xml;charset=utf-8");
out.println("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
out.println("<studnet>");
out.println("<name>huangbo</name>");
out.println("<age>39</age>");
out.println("</studnet>");
}
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
1.以ajax POST方式请求DataTest5,可以得到xml或json格式数据(用户可选);2.把名字huangbo弹出来(用js代码写出来)
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DataTest5 extends HttpServlet {
/**
* Constructor of the object.
*/
public DataTest5() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String contentType=request.getHeader("Accept");
PrintWriter out = response.getWriter();
if(contentType.contains("application/json"))
{
response.setContentType("application/json;charset=utf-8");
out.println("{\"name\":\"huangbo\",\"age\":39}");
}
else if(contentType.contains("application/xml")||contentType.contains("text/xml"))
{
response.setContentType("text/xml;charset=utf-8");
out.println("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
out.println("<studnet>");
out.println("<name>黄波</name>");
out.println("<age>39</age>");
out.println("</studnet>");
}
//out.println(contentType);
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String contentType=request.getContentType();
PrintWriter out = response.getWriter();
if(contentType.contains("application/json"))
{
response.setContentType("application/json;charset=utf-8");
out.println("{\"name\":\"huangbo\",\"age\":39}");
}
else if(contentType.contains("application/xml")||contentType.contains("text/xml"))
{
response.setContentType("text/xml;charset=utf-8");
out.println("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
out.println("<studnet>");
out.println("<name>huangbo</name>");
out.println("<age>39</age>");
out.println("</studnet>");
}
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
1.以ajax POST方式请求DataTest5,可以得到xml或json格式数据(用户可选);2.把名字huangbo弹出来(用js代码写出来)