HttpUrlConnection访问Servlet进行数据传输
建立一个URL url = new URL("location");
建立 httpurlconnection :HttpUrlConnection httpConn = (HttpUrlConnection)url.openConnection();//强制类型转换
String requestString = "什么gui";
设置连接属性:
httpConn.setDoOutPut(true); //使用URL进行输出
httpConn.setDoInPut(true); //使用URL进行输入
httpConn.setUseCaches(false); //忽略使用缓存
httpConn.setRequestMethod("Post"); //默认情况是Get方法
设置请求属性:
byte[] requestStringBytes = requestString.getBytes("UTF-8");
httpConn.setRequestProperty("Content-length", "" + requestStringBytes.length);
httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
httpConn.setRequestProperty("Charset", "UTF-8");
建立输出流,写入数据:
httpConn.connect();
DataOutputStream outputStream = new DataOutputStream(httpConn.getOutputStream());
String content = "name="+ URLEncoder.encode(requestString,"utf-8");
outputStream.writeBytes(content);
outputStream.flush();
outputStream.close();
那么在Servlet端 获取数据的方式,
String name = request.getParameter("name"); //这样就能获取到requestString 的字符串
HttpUrlConnection访问Servlet进行数据传输的更多相关文章
- 浏览器访问Servlet
浏览器访问Servlet1.指定一个Servlet路径( 在web.xml : <servlet> ...
- 自建目录中jsp页面访问servlet路径出错404
---恢复内容开始--- 自建目录中jsp页面访问servlet路径出错404 使用eclipse建立的项目,总是会遇到路径问题,比如jsp页面访问servlet,jsp在默认的路径.jsp在自建目录 ...
- struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...
- Action访问Servlet API
访问Servlet API 1.通过ActionContent类访问Servlet API ActionContext中访问Servlet API的几个常用的方法: (1)Map getApplica ...
- struts2访问servlet API
搭建环境: 引入jar包,src下建立struts.xml文件 项目配置文件web.xml. web.xml: <?xml version="1.0" encoding=&q ...
- struts2中访问servlet API
Struts2中的Action没有与任何Servlet API耦合,,但对于WEB应用的控制器而言,不访问Servlet API几乎是不可能的,例如需要跟踪HTTP Session状态等.Struts ...
- struts2的action访问servlet API的三种方法
学IT技术,就是要学习... 今天无聊看看struts2,发现struts2的action访问servlet API的三种方法: 1.Struts2提供的ActionContext类 Object g ...
- Struts 2读书笔记-----Action访问Servlet API
Action访问Servlet API Struts2中的Action并没有和任何Servlet API耦合,这样框架更具灵活性,更易测试. 对于Web应用的控制器而言,不访问ServletAPI是几 ...
- Struts2系列笔记(4)---Ation类访问servlet
Ation类访问servlet Ation类有三种方式servlet: (1)间接的方式访问Servlet API ---使用ActionContext对象 (2) 实现接口,访问Action时完 ...
随机推荐
- js传递参数中包含+号时的处理方法
encodeURI(url).replace(/\+/g, '%2B') 例子: $scope.getAnesthesiawaystatistical = function (annual, anes ...
- php 读取文件readfile
<?php //读取文件 //echo readfile('aa.txt'); //打开文件更好的方法是fopen $f = fopen('aa.txt' , 'r') or die('unab ...
- (转)AVI文件格式解析+AVI文件解析工具
AVI文件解析工具下载地址:http://download.csdn.net/detail/zjq634359531/7556659 AVI(Audio Video Interleaved的缩写)是一 ...
- POJ C Looooops
Description A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; ...
- pH 值与曝气对硝化细菌硝化作用的影响
http://wenku.baidu.com/view/c2723434eefdc8d376ee325d.html 摘要: 目的 探讨硝化细菌最佳工作条件,为应用和生产提供依据. 方法 通过人工调节液 ...
- shell 初学者 必读 ,强烈推荐新手读
背景: 很多人从C/C++转化而来,看了学习文档之后,踩入了很多坑 1 对变量赋值 不要有空格 a=123 # 正确 a = 123 # 错误 2 if语句 [] 要留有空格,变量最好加" ...
- 利用jquery实现自动登录
前提是需要引入jquery和jquery.cookie html 用户名:<input type="text" id="name"/><br ...
- 随机打乱工具sklearn.utils.shuffle,将原有的序列打乱,返回一个全新的错乱顺序的值
Shuffle arrays or sparse matrices in a consistent way This is a convenience alias to resample(*array ...
- UBUNTU9.10下安装TFTP学习笔记一(arm学习SEED-138板子)
擦,刚刚写的没保存都丢了,郁闷中~~~~ 简单重写 1什么是TFTP .安装TFTP(TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的 ...
- VC++ 限制窗口的大小范围的方法
响应WM_GETMAXMININFO 的消息 处理之 void CMainFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) { // TODO: Add y ...