package com.cyq7on.servlet;

import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cyq7on.entity.User;

/**
 * Servlet implementation class Servlet1
 */
@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
//		response.sendRedirect("https://www.taobao.com");
//		ServletOutputStream outputStream = response.getOutputStream();
//		outputStream.write(new String("heihei").getBytes());
//		PrintWriter writer = response.getWriter();
//		writer.write(new String("heihei"));
		ServletInputStream inputStream = request.getInputStream();
		PrintWriter writer = response.getWriter();
		byte[] b = new byte[1024];
		int len = 0;
		int offset = 0;
	    int numRead = 0;
	    while (numRead >= 0) {
	      numRead = inputStream.read(b, offset, b.length - offset);
	      offset += numRead;
	      String info = new String(b);
	      writer.write(info);
	      System.out.println(info);
	    }

//		while((len = inputStream.readLine(b, 0, len))!= -1) {
////			writer.write(new String(b));
//			System.out.println(new String(b));
//		}
//		while(inputStream.read(b) != -1) {
//			writer.write(new String(b));
//		}
		writer.close();
		inputStream.close();
	}

	private void request2(HttpServletRequest request) {
		Map<String, String[]> map = request.getParameterMap();
		User user = new User();
		try {
			for (Entry<String, String[]> entry : map.entrySet()) {
				String name = entry.getKey();
				String[] values = entry.getValue();
				PropertyDescriptor descriptor = new PropertyDescriptor(name, User.class);
				Method method = descriptor.getWriteMethod();
				if (values.length == 1) {
					method.invoke(user, values[0]);
				} else {
					method.invoke(user, (Object)values);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(user);
	}

	private void request1(HttpServletRequest request) {
		System.out.println(request.getMethod());
		System.out.println(request.getRequestURI());
		System.out.println(request.getRequestURL());
		System.out.println(request.getContextPath());
		String query = request.getQueryString();
		if(query == null) {
			System.out.println("query is null");
		}else {
			System.out.println("query is " + query);
		}
		Enumeration<String> headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String header = headerNames.nextElement();
			System.out.println(header + ":" + request.getHeader(header));
		}
	}
}

@@ -149,16 +149,44 @@
 Method;%0A
+import java.net.URLDecoder;%0A
 import j
@@ -1361,210 +1361,138 @@
  0;%0A
-%09%09int offset = 0;%0A%09    int numRead = 0;%0A%09    while (numRead %3E= 0) %7B%0A%09      numRead = inputStream.read(b, offset, b.length - offset);%0A%09      offset += numRead;%0A%09      String info = new String(b);%0A%09      
+%0A%09%09while((len = inputStream.readLine(b, 0, b.length))!= -1) %7B%0A%09%09%09String info = URLDecoder.decode(new String(b,0,len),%22UTF-8%22);%0A%09%09%09
 writ
@@ -1512,14 +1512,10 @@
 );%0A%09
-      
+%09%09
 Syst
@@ -1541,19 +1541,15 @@
 );%0A%09
-    %7D%0A%0A//%09%09
+%09%7D%0A%09%09/*
 whil
@@ -1577,53 +1577,38 @@
 read
-Line(b, 0, len))!= -1) %7B%0A////%09%09%09writer.write(
+(b))!= -1) %7B%0A%09%09%09String info = 
 new 
@@ -1619,136 +1619,74 @@
 ng(b
-)
+,0,len
 );%0A
-//
 %09%09%09
-System.out.println(new String(b));%0A//%09%09%7D%0A//%09%09while(inputStream.read(b) != -1) %7B%0A//%09%09%09writer.write(new String(b)
+writer.write(info);%0A%09%09%09System.out.println(info
 );%0A
-//
 %09%09%7D
+*/
 %0A%09%09w

read and write by stream