DoGet和DoPost
2015-08-12 14:35
288 查看
给serverlet传输数据
serverlet的编写
使用get方法传输
使用post的方法传输
serverlet的编写
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String username=request.getParameter("username");//得到的网页的uername, String password=request.getParameter("password"); username=Encoding.encoding(username);//通过编写的方法,进行转换 System.out.println("用户名"+username+"\n"+"密码"+password); /** * 然后在给浏览器数据 */ response.setHeader("content-type","text/html;charset=utf-8");//让浏览器以utf-8格式解析 try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //返回给消息提供者的消息 response.getWriter().append("用户名:"+username); response.getWriter().println("<br>"); response.getWriter().append("密码 :"+password); }
public class Encoding { public static String encoding(String s) { try { byte[] array = s.getBytes("ISO-8859-1"); s = new String(array, "utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; } }
使用get方法传输
String urlTemp = "http://localhost:8080/TestWeb/TestServerlet?username=zhangsan&password=123"; try { URL url = new URL(urlTemp);// 建立一个新连接 URLConnection con = url.openConnection();// 打开连接 HttpURLConnection urlcon = (HttpURLConnection) con;// 连接转化为Http urlcon.setRequestMethod("GET");// 设置服务器的方法 urlcon.setReadTimeout(3000);//设置读取超时 urlcon.setConnectTimeout(3000);//连接超时 urlcon.setRequestProperty("Accept-Charset", "utf-8"); // 设置可以接受序列化的java对象 urlcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); int code = urlcon.getResponseCode(); System.out.println("状态码:" + code); if (code == HttpURLConnection.HTTP_OK) { InputStream is = urlcon.getInputStream(); BufferedReader bRead = new BufferedReader(new InputStreamReader(is)); String s = bRead.readLine(); while (s != null) { System.out.println(s); s = bRead.readLine(); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SocketTimeoutException e) { System.out.println("连接超时"); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
使用post的方法传输
public void actionPerformed(ActionEvent arg0) { String urlTemp = "http://localhost:8080/TestWeb/TestServerlet"; try { URL url = new URL(urlTemp); URLConnection con = url.openConnection(); HttpURLConnection httpCon = (HttpURLConnection) con; httpCon.setRequestMethod("POST");// 设置服务器的方法 httpCon.setReadTimeout(30000);// 设置读取超时 httpCon.setConnectTimeout(3000);// 连接超时 httpCon.setRequestProperty("Accept-Charset", "utf-8"); // 设置可以接受序列化的java对象 httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpCon.setDoOutput(true);//设置post输出为真post方法必须设置 httpCon.setUseCaches(false);//设置缓存为false,post方法必须设置的 String tempSend="username=zhangsan&password=123"; httpCon.getOutputStream().write(tempSend.getBytes()); int code =httpCon.getResponseCode(); System.out.println(code); if (code == HttpURLConnection.HTTP_OK) { InputStream is = httpCon.getInputStream(); BufferedReader bRead = new BufferedReader(new InputStreamReader(is)); String s = bRead.readLine(); while (s != null) { 4000 System.out.println(s); s = bRead.readLine(); } }
相关文章推荐
- serverlet配置文件
- eclipse建立Dynamic Web Project 时没有web.xml
- 从零开始写javaweb框架笔记8-细节完善与代码优化
- Servlet 工程 web.xml 中的 servlet 和 servlet-mapping 标签
- Tomcat组件初解
- servlet初识之创建servlet与tomcat配置
- Request ---域--取值---response的流程
- 浅谈JSP serverlet的区别与联系
- [转]linux /proc/cpuinfo 文件分析
- Nginx双机高可用
- centos配置nfs服务详细步骤(centos开启nfs服务)
- tomcat部署maven项目过程
- 免费样片申请网站集合
- class中的field,property,method,constructor
- centos-apache-tomcat-domain配置
- 迅为4412开发板Linux驱动教程——总线_设备_驱动注册流程详解
- linux 安装sysstat使用iostat、mpstat、sar、sa
- 14 个 Linux grep 命令的例子
- OpenCV3.0.0 + VS2013 + Win8x64配置
- 网站变灰