servlet+myeclipse+tomcat+JSON的一个小服务器

经过了一段时间的努力,查找了不少资料,现在把相关知识列出来:

代码:

这个是服务端代码:

这是一个person类

package com.servelet;


public class Person {
//属性(字段)
private int legs;
private String name;
//构造方法(函数)
Person(){

}
Person(int legs, String name) {
this.legs = legs;
this.name = name;
}
//方法
public String getName() {
return this.name;
}


public void setName(String n) {
this.name = n;
}
public int getLegs() {
return legs;
}
public void setLegs(int legs) {
this.legs = legs;
}

}




这里是关键:

package com.servelet;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONObject;


public class ServeletTest extends HttpServlet {


/**
* Constructor of the object.
*/
public ServeletTest() {
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 {


response.setContentType("text/html");
//response.setContentType("application/x-json");
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
/*out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.print("    This is ");
out.print(this.getClass());
out.println(", using the GET method");*/
Map<String,Person> m2 = new HashMap<String,Person>();
m2.put("usa",new Person(2,"jack"));
m2.put("china", new Person(2,"zhoutao"));
m2.put("japan", new Person(2,"中田"));
String json ="{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
JSONObject json3 = JSONObject.fromObject(json);
out.write(json3.toString());
/*out.println("  </BODY>");
out.println("</HTML>");*/

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 {


response.setContentType("text/html");
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.print("    This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}


/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}


}


步骤:

第一步:

把代码贴上去,会有很多XXXXXX,这时候要导入包。包的地址:http://download.csdn.net/detail/xie389124248/5234992

同时些包也要导入到tomcat的相应服务下的lib里面去,否者会报错。

一个服务的发布,我这里指的不是发布一个web 服务,而仅仅是一个servlet与tomcat连接起来。

1、在myeclipse里面创建一个web procject而不是一个web service project

2、新建一个servlet类,在里面填写相关内容

3、配置tomcat,这些资料网上都有很多,google一下就出来了,我的是先前已经配置好了的

4、点击这个按钮,把tomcat和当前的servlet连接起来,选择自己要连接的project

       点击最左边的图标(注意不是myeclipse里面最左边的图标)



      弹出了这个窗口

              选择自己要连接的project。下面有”successfully deployed“说明已经成功了。



最后开启服务器,在浏览器器中输入http://localhost:8080/ServeletSever/servlet/ServeletTest就可以了

解释一下这字符串:

 localhost是自己本地。。。。。

8080是自己的端口号,配置的时候可以看到的

ServeletSever这个是工程名字

servlet/ServeletTest是web.xml里面的URL映射路径。


这样就可以了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值