80,349
社区成员
发帖
与我相关
我的任务
分享
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zf.entity.GoodsDetail;
public class WeiDaoServlet extends HttpServlet {
/**
* 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 {
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("name");
if (username.equals("zhangfan")) {
/*
StringBuilder builder = new StringBuilder();
builder.append('{');
builder.append("name:").append("zhangfan").append(",").append("age:").append(22);
builder.append('}');
System.out.println("builder" + builder);
request.setAttribute("json", builder.toString());
request.getRequestDispatcher("/MyJsp.jsp").forward(request, response);
*/
String str = "张三";
response.getOutputStream().write(str.getBytes("GBK"));
// str = URLEncoder.encode(str, "UTF-8");
// response.getWriter().println(str);
}
}
/**
* 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 {
doGet(request, response);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread()
{
public void run(){
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.191.1:8080/WeiDaoHouTai/servlet/WeiDaoServlet?name=zhangfan");
// post.getParams().setParameter("", "UTF-8");
try {
HttpResponse response = client.execute(post);
if(response.getStatusLine().getStatusCode()==200) {
// Toast.makeText(HomeActivity.this, "成功", Toast.LENGTH_SHORT).show();
// response.
HttpEntity entity = response.getEntity();
String msg = EntityUtils.toString(entity);
Log.d("zf", "entity = " + msg);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
// DefaultHttpClient client = new DefaultHttpClient();
// HttpPost post = new HttpPost("http://192.168.1.103:8080/WeiDaoHouTai/servlet/WeiDaoServlet?name=zhangfan");
// try {
// HttpResponse response = client.execute(post);
// if(response.getStatusLine().getStatusCode()==200) {
// Toast.makeText(HomeActivity.this, "成功", Toast.LENGTH_SHORT).show();
// }
// } catch (ClientProtocolException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}