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

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代码写出来)
...全文
71 2 点赞 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
街头小贩 2015-10-15
自已下个jquery对比chm手册不难写出来
  • 打赏
  • 举报
回复
qq_20099705 2015-10-15
名字是huangbo
  • 打赏
  • 举报
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-10-15 03:01
社区公告
暂无公告