post提交表单

///////////////////////////////////////////////首先是服务器端的配置---myeclipse+tomcat7////////////////////////////////////////////////

新建一个名字为Login的servlet,以下是代码

<span style="font-size:18px;">package com.melody.login;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Login extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Login() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String name = request.getParameter("name");
		String pass = request.getParameter("pass");
		
		name = new String(name.getBytes("iso8859-1"),"utf-8"); //对中文进行编码
		
		System.out.println(name);
		System.out.println(pass);
		System.out.println("------------");
		
		
		OutputStream os = response.getOutputStream();
		if("龙".equals(name) && "123".equals(pass)){
			os.write("success".getBytes("utf-8"));
		}else{
			os.write("fail".getBytes("utf-8"));
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
</span>
///////////////////////下面是服务器断,index.jsp的配置文件/////////////////////////////////////////

<span style="font-size:18px;"><body>
    <form action="/web/servlet/Login" method="get">
    	帐号:<input name="name" type="text"> <br/>
    	密码:<input name="pass" type="password"><br/>
    	<input type="submit" value="登录">
    </form>
    
    <form action="/web/servlet/Login" method="post">
    	帐号:<input name="name" type="text"> <br/>
    	密码:<input name="pass" type="password"><br/>
    	<input type="submit" value="登录">
    </form>
  </body></span>


//////////////////////////////////////下面是客户端eclipse的配置文件   MainActivity

<span style="font-size:18px;">package com.melody.post;


import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.melody.html.tool.Tools;

public class MainActivity extends Activity {
	private EditText et_name;
	private EditText et_pass;
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			Toast.makeText(MainActivity.this, (String) msg.obj, 0).show();
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	public void onclick(View v){
		//final String path = "http://192.168.119.69:8080/web/servlet/Login";
		
		et_name = (EditText) findViewById(R.id.et_name);
		et_pass = (EditText) findViewById(R.id.et_pass);
		
		//final String name = et_name.getText().toString().trim();
		final String name = "龙";
		final String pass = et_pass.getText().toString().trim();
		
		Thread t = new Thread(){
//			String path = "http://192.168.1.106:8080/web/servlet/Login?name=" + URLEncoder.encode(name)		//对要提交的表单数据进行URL编码(中文) 
//																													+ "&pass=" + pass;
			String path = "http://192.168.1.106:8080/web/servlet/Login";
			public void run() {
				try {
					URL url = new URL(path);
					HttpURLConnection conn = (HttpURLConnection) url.openConnection();
					
					conn.setRequestMethod("POST");
					conn.setReadTimeout(8000);
					conn.setConnectTimeout(8000);
					
					//添加post请求头中自动添加的属性
					//流里数据的mimetype
					conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
					//流里数据的长度
					String content = "name="+URLEncoder.encode(name)+"&pass="+pass;
					conn.setRequestProperty("Content-Length", content.length()+"");
					
					//打开连接对象的输入流
					conn.setDoOutput(true);
					//获取连接对象的输出流
					OutputStream os = conn.getOutputStream();
					//把数据写入输出流
					os.write(content.getBytes());
					
					//判断请求是否成功
					if(conn.getResponseCode() == 200){    //表示成功
						InputStream is = conn.getInputStream();   //成功获取输入流
						//使用自己写的获取流中文本的工具类
						String text = Tools.getTextFromStrem(is);
						//发送消息,刷新UI
						Message msg = handler.obtainMessage();
						msg.obj = text;
						handler.sendMessage(msg);					
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		};
		t.start();
	}
}
</span>
//////////////////////////////////////自定义 读取文本流的工具类---Tools  //////////////////////////////////////////////////////////

<span style="font-size:18px;">package com.melody.html.tool;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;


/**
 * 从流中获取文本工具类
 * @author Administrator
 *
 */
public class Tools {
	
	public static String getTextFromStrem(InputStream is){
		byte[] b = new byte[1024];  //定义一个1K的字节数组
		int len;									//定义数组长度
		
		//定义一个字节数组输出流,保存数据   写入数据所以用输出流  读入写出
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		//开始往bos字节数组写入数据    
		try {
			while((len = is.read(b)) != -1){
				bos.write(b, 0, len);    
			}
			//把字节数组输出流转换成字节数组,然后用字节数组构造一个字符串
			//String text = new String(bos.toByteArray(),"GBK");   //指定文件编码  如果乱码的话这里改,默认UTF-8
			String text = new String(bos.toByteArray());
			return text;
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}


}
</span>


////////////////////////////////////////////xml中记得添加权限/////////////////////////////////////////////////////////
<span style="font-size:18px;"><uses-permission android:name="android.permission.INTERNET"/></span>

///////////////////////布局配置文件//////////////////////////////////////////
 <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    
    <EditText
        android:id="@+id/et_pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    
    <Button 
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:onClick="onclick"/>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值