测试时出现下载框的各种Debug

情况是这样的,我要做一个从服务器数据库将emp表的数据发送到Android客户端显示的测试,用hibernate搭配tamcat做服务器,

数据库用的mysql,有3个表分别是user,dep,emp,我把这3个表都添加到hibernate,然后写service,

package com.service;

import java.util.List;

import com.dao.EmpDAO;
import com.pojo.Emp;

public class EmpService {
	/**
	 * 
	 * 1,查出来所有的员工
	 * 2将查出来的List转为JSON
	 */
	
	private EmpDAO empdao =new EmpDAO();
	
	//查
	public List GetAll(){
		return empdao.findAll();
	}
	//转为JSON字符串 格式为:[{id:1,name:'王二'},{id:2,name:''}]
	
	public String Emp2JSON(){
		//要拼接字符串的话,想到StringBuffer
		StringBuffer sb= new StringBuffer();
		//接收Emp对象
		List<Emp> list =GetAll();
		//判断是否有内容,hibernate查出来的数据不会为null,要么就是0
		if(list.size()>0){
			sb.append("[");
			//foreach
			for (Emp emp : list) {
				sb.append("{id:");
				sb.append(emp.getEid());
				sb.append(",name:'");
				sb.append(emp.getEname());
				sb.append("'},");
				//这样的话,sb最后会是‘},而不是'}]
				
			}
			//所以将最后的,换成]
			sb.setCharAt(sb.length()-1, ']');
		}
		else{
			return "[]";//否则返回空的JSON
		}
		return sb.toString();
	}

}

EMpserverlet
package com.serverlet;


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;

import com.service.EmpService;

public class EmpServerlet1 extends HttpServlet {
	private EmpService empservice=new EmpService();
		
	@Override
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("txt/html;charset=utf-8");
		String p=request.getParameter("p");
		System.out.println(p);
		if("GetAll".equals(p)){
			GetEmp2(request,response);
		}
	}



	public void GetEmp2(HttpServletRequest request,
			HttpServletResponse response) throws IOException  {
		//Print   向文本输出流打印对象的格式化表示形式
		PrintWriter out = response.getWriter();

		//将JSON取出来
		String json=empservice.Emp2JSON();

		out.printf(json);
		
		
	}
	

之后发布工程,在地址栏输入地址访问,http://127.0.0.1:8080/JSONserver/emp.do?p=GetAll,
然而在这时候就出问题了,在Myeclipse 的控制台下可以看到输出了GetAll,但是在浏览器却出现了下载对话框,<img src="https://img-blog.csdn.net/20151108230517245?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
这时候,我很是纠结 ,为什么会出现这样的情况呢?
想想感觉有这么几种可能:
1,浏览器的问题;
2,java代码的问题;
3,配置出问题了;
4,Myeclipse的问题;
</pre><pre name="code" class="java">
既然叫我下载,那我就下载看看(反正我平时也不浏览什么。。。),下载下来,发现可以用记事本打开,而且里面的内容与我预期要在浏览器上显示的一致<img src="https://img-blog.csdn.net/20151108231649023?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
</pre><pre name="code" class="java">这是什么鬼?我就蒙了,为啥会这样呢,?目前以我的水平搞不懂
</pre><pre name="code" class="java">,反正还会出现下载框,于是排除一个个预想的问题,
1,换个全新的火狐浏览器,发现问题依旧,表明不是浏览器的问题,
2,我把其他的去掉,只留了serverlet,在浏览器输出‘hello’结果发现是可以的,<img src="https://img-blog.csdn.net/20151108232315126?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
</pre><pre name="code" class="java">先写到这,,后面的还没调试,调试之后再说,第一次写,很多地方写的不好,我要努力
 
后来发现原来是服务器端设置编码的问题,
<span style="color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(252, 211, 221);">response.setContentType("text/html; charset=utf-8"); html</span><br style="color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(252, 211, 221);" /><span style="color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(252, 211, 221);">.setContentType("text/plain; charset=utf-8"); 文本</span><br style="color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(252, 211, 221);" /><span style="color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(252, 211, 221);">text/javascript json数据</span><br style="color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(252, 211, 221);" /><span style="color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(252, 211, 221);">application/xml </span><wbr style="color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(252, 211, 221);"><span style="color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(252, 211, 221);"> xml数据</span>
</wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值