Client.java 發送字符串從客戶端的servlet成servlet如何發送字符串從我的java類
String url = "http://localhost:8084/Lab/url";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
con.setDoInput(true);
con.setDoOutput(true);
dos = new DataOutputStream(con.getOutputStream());
dos.writeBytes(resultJson.toString());
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
dos.flush();
dos.close();
in.close();
的Servlet 獲取值並返回
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
我不知道下一步該怎麼做。
你想傳遞哪些字符串參數? – SpringLearner
使用'GET'在URL中傳遞參數,也許您需要使用'POST'? –
@JqueryLearner like this {「lol」:5,「lolid」:[1,2,3,4,5]} – olle