1 package main; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class Save extends HttpServlet {11 12 @Override13 protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {14 req.getRequestDispatcher("WEB-INF/save.html").forward(req, res);15 }16 17 18 19 }
package main;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ToSave extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { System.out.println(req.getParameter("t")); req.getRequestDispatcher("WEB-INF/save.html").forward(req, res); } }
1 2 3 4 513 14WebTest login 6 7 8保存
9
简单的提交页面。
通过分析,可以知道保存时候发生的请求。
这里也可以知道传递了那些参数。
这里简单的demo只需要穿文本区的内容就可以了。
1 package httpclient.demo; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.apache.http.Header; 8 import org.apache.http.HttpEntity; 9 import org.apache.http.NameValuePair;10 import org.apache.http.client.ClientProtocolException;11 import org.apache.http.client.entity.UrlEncodedFormEntity;12 import org.apache.http.client.methods.CloseableHttpResponse;13 import org.apache.http.client.methods.HttpGet;14 import org.apache.http.client.methods.HttpPost;15 import org.apache.http.impl.client.CloseableHttpClient;16 import org.apache.http.impl.client.HttpClients;17 import org.apache.http.message.BasicNameValuePair;18 import org.apache.http.protocol.HTTP;19 import org.apache.http.util.EntityUtils;20 21 public class LoginDemo4 {22 23 public static void main(String[] args) {24 // HttpClientBuilder.create().build()25 CloseableHttpClient httpClient = null;26 HttpPost post = null;27 try {28 httpClient = HttpClients.createDefault();29 //登录30 post = new HttpPost("http://localhost:8080/WEB_TEST/toSave.do");31 post.setHeader("User-Agent",32 "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");33 34 //传递所需参数35 Listparams=new ArrayList (); 36 params.add(new BasicNameValuePair("t","Hello World!!")); 37 38 post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));39 CloseableHttpResponse response = httpClient.execute(post);40 HttpEntity entity = response.getEntity();41 if (entity != null) {42 String str = EntityUtils.toString(entity, "utf-8");43 System.out.println(str);44 }45 46 } catch (ClientProtocolException e) {47 e.printStackTrace();48 } catch (IOException e) {49 e.printStackTrace();50 } finally {51 try {52 if (post != null) {53 post.releaseConnection();54 }55 if (httpClient != null) {56 httpClient.close();57 }58 } catch (IOException e) {59 // TODO Auto-generated catch block60 e.printStackTrace();61 }62 }63 }64 65 }
运行结果可知,httpclient的demo成功发出了保存请求,并获取到了传递的参数,即文本区域内容。