Java --> Server Response (Servlet -- doGet & doPost), assumervletdoget
--> Servelet: used to receive requests (client, browser), respond, server, and java
--> ServletLogin -- Java Implementation of Web Project Server Response
Package com. dragon. java. servlet; import java. io. IOException; import java. io. printWriter; import java. io. unsupportedEncodingException; import javax. servlet. servletException; import javax. servlet. annotation. webServlet; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse;/*** Servlet implementation class ServletLogin */@ WebServlet ("/ServletLogin") public class ServletLogin extends HttpServlet {private static final long serialVersionUID = 1L; protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String user = parse (request. getParameter ("user"); String pwd = parse (request. getParameter ("pwd"); System. out. println (user + ":" + pwd); response. setContentType ("text/ Html; charset = UTF-8 "); PrintWriter writer = response. getWriter (); writer. println (user +" Login successful! "); // The server will automatically shut down the stream} protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request. setCharacterEncoding ("UTF-8"); // only address post request garbled doGet (request, response);} public String parse (String msg) throws UnsupportedEncodingException {return new String (msg. getBytes ("iso8859-1"), "UTF-8 ");}}
1. doGet method:
--> HttpURLConnectionUtil tool class
Package com. dragon. java. urlbyget; import java. io. IOException; import java. io. inputStream; import java.net. httpURLConnection; import java.net. URL; public class HttpURLConnectionUtil {public static InputStream getInputStreamByGet (String url) {try {HttpURLConnection conn = (HttpURLConnection) new URL (url ). openConnection (); conn. setRequestMethod ("GET"); conn. setreadtimeouts (10000); conn. setConnectTimeout (10000); // you can use this method to set the http Request Header // conn. addRequestProperty ("Accept ","...... "); if (conn. getResponseCode () = HttpURLConnection. HTTP_ OK) {// get the encoding // String contentType = conn. getContentType (); // String string = contentType. split ("=") [1]; InputStream inputStream = conn. getInputStream (); return inputStream;} catch (IOException e) {e. printStackTrace ();} return null ;}}
--> Test class
package com.dragon.java.urlbyget;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class Test { public static void main(String[] args) { InputStream inputStreamByGet = HttpURLConnectionUtil .getInputStreamByGet("http://www.baidu.com"); try { BufferedReader br = new BufferedReader(new InputStreamReader( inputStreamByGet, "gb2312")); String line = ""; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } }}
-------------------------------- Evil split line ------------------------------------
2. doPost Method
--> HttpURLConnectionUtil tool class
package com.dragon.java.urlbypost;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;public class HttpURLConnectionUtil { private static String charset; public static InputStream getInputStreamByPost(String url, String parms) { try { HttpURLConnection conn = (HttpURLConnection) new URL(url) .openConnection(); conn.setReadTimeout(5000); conn.setConnectTimeout(5000); conn.setRequestMethod("POST"); conn.setDoOutput(true); OutputStream outputStream = conn.getOutputStream(); outputStream.write(parms.getBytes()); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { String contentType = conn.getContentType(); charset = contentType.split("=")[1]; InputStream inputStream = conn.getInputStream(); return inputStream; } } catch (IOException e) { e.printStackTrace(); } return null; } public static String getCharset() { return charset; }}
--> Conversion class of InputStreamUtil stream to String
package com.dragon.java.urlbypost;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class InputStreamUtil { public static String inputStreamToString(InputStream is, String charset) { StringBuffer sb = new StringBuffer(); try (BufferedReader br = new BufferedReader(new InputStreamReader(is, charset));) { char[] buffer = new char[1024]; int len = -1; while ((len = br.read(buffer)) != -1) { sb.append(buffer, 0, len); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return null; }}
--> Test class
package com.dragon.java.urlbypost;import java.io.InputStream;public class Test { public static void main(String args[]) {
// Encode the Chinese parameters in the URL.
String user = URLEncoder. encode ("Zhang San", "UTF-8 ");
String pwd = URLEncoder. encode ("hello", "UTF-8 ");
InputStream inputStreamByPost = HttpURLConnectionUtil
. GetInputStreamByPost (
"Http: // 192.168.2.11: 8080/08-23/ServletLogin", "user ="
+ User + "& pwd =" + pwd );
System.out.println(InputStreamUtil.inputStreamToString( inputStreamByPost, HttpURLConnectionUtil.getCharset())); }}
---> Note: first import the Web Project to the server and run it...