急求大神帮忙!!!!这个要怎么弄?

qq_20099705 2015-10-15 03:01:46
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.把我的名字弹出来(用js代码写出来)
...全文
114 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
街头小贩 2015-10-15
  • 打赏
  • 举报
回复
自已下个jquery对比chm手册不难写出来
qq_20099705 2015-10-15
  • 打赏
  • 举报
回复
名字是huangbo

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧