设为首页 收藏本站
查看: 1449|回复: 0

[经验分享] 2 我的第一个手机提交数据到tomcat服务器

[复制链接]

尚未签到

发表于 2018-12-7 08:29:25 | 显示全部楼层 |阅读模式
  ~~~~~~~~~~~~~~~~WebProject的项目,在这里我们建为fe工程~~~~~~~

  

  -----------------LoginServlet.java-------------
  package com.csx;
  

  import java.io.IOException;
  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 LoginServlet extends HttpServlet {
  

  public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  

  doPost(request, response);
  }
  

  public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  String name = request.getParameter("name");
  String password = request.getParameter("password");
  if(name.equals("zz") && password.equals("123")) {
  response.getOutputStream().write("success".getBytes());
  } else {
  response.getOutputStream().write("failed!!!!".getBytes());
  
  }
  }
  

  }
  

  。。。。。。web.xml。。。。。。。。。
  
  LoginServlet
  com.csx.LoginServlet
  
  

  
  LoginServlet
  /*
  
  

  

  ~~~~~~~~~~~~~~~~~~~Android项目~~~~~~~~~~~~~~~~~~~
  

  ----------------main.java--------------

  package com.example.jn;
  

  import android.os.Bundle;
  import android.support.v7.app.ActionBarActivity;
  import android.view.View;
  import android.widget.EditText;
  import android.widget.Toast;
  

  public class MainActivity extends ActionBarActivity {
  

  private EditText etUserName;
  private EditText etPassword;
  

  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  etUserName = (EditText) findViewById(R.id.et_username);
  etPassword = (EditText) findViewById(R.id.et_password);
  }
  

  public void doGet(View v) {
  final String userName = etUserName.getText().toString();
  final String password = etPassword.getText().toString();
  
  new Thread(
  new Runnable() {
  
  @Override
  public void run() {
  // 使用get方式抓去数据
  final String state = NetUtils.loginOfGet(userName, password);
  
  // 执行任务在主线程中
  runOnUiThread(new Runnable() {
  @Override
  public void run() {
  // 就是在主线程中操作
  Toast.makeText(MainActivity.this, state, 0).show();
  }
  });
  }
  }).start();
  }
  
  public void doPost(View v) {
  final String userName = etUserName.getText().toString();
  final String password = etPassword.getText().toString();
  
  new Thread(new Runnable() {
  @Override
  public void run() {
  final String state = NetUtils.loginOfPost(userName, password);
  // 执行任务在主线程中
  runOnUiThread(new Runnable() {
  @Override
  public void run() {
  // 就是在主线程中操作
  Toast.makeText(MainActivity.this, state, 0).show();
  }
  });
  }
  }).start();
  }
  }
  

  //javaURL方式的访问网页
  ---------------------NetUtils.java------------------
  

  

  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLConnection;
  import java.net.URLEncoder;
  

  import android.util.Log;
  

  public class NetUtils {
  

  private static final String TAG = "NetUtils";
  
  /**
   * 使用post的方式登录
   * @param userName
   * @param password
   * @return
   */
  public static String loginOfPost(String userName, String password) {
  HttpURLConnection conn = null;
  try {
  URL url = new URL("http://192.168.137.1:8080/fe/LoginServlet");
  
  conn = (HttpURLConnection) url.openConnection();
  
  conn.setRequestMethod("POST");
  conn.setConnectTimeout(10000); // 连接的超时时间
  conn.setReadTimeout(5000); // 读数据的超时时间
  conn.setDoOutput(true);// 必须设置此方法, 允许输出
  //conn.setRequestProperty("Content-Length", 234);// 设置请求头消息, 可以设置多个
  
  // post请求的参数
  String data = "name=" + userName + "&password=" + password;
  
  // 获得一个输出流, 用于向服务器写数据, 默认情况下, 系统不允许向服务器输出内容
  OutputStream out = conn.getOutputStream();
  out.write(data.getBytes());
  out.flush();
  out.close();
  
  int responseCode = conn.getResponseCode();
  if(responseCode == 200) {
  InputStream is = conn.getInputStream();
  String state = getStringFromInputStream(is);
  return state;
  } else {
  Log.i(TAG, "访问失败: " + responseCode);
  }
  } catch (Exception e) {
  e.printStackTrace();
  } finally {
  if(conn != null) {
  conn.disconnect();
  }
  }
  return null;
  }
  

  /**
   * 使用get的方式登录
   * @param userName
   * @param password
   * @return 登录的状态
   */
  public static String loginOfGet(String userName, String password) {
  HttpURLConnection conn = null;
  try {
  String data = "name=" + URLEncoder.encode(userName) + "&password=" + URLEncoder.encode(password);
      //由于我是通过真机连接电脑的wifi访问电脑的tomcat服务器,所以192.168.137.1为电脑发射wifi的ip地址(在手机上查则是网关地址)

  URL url = new URL("http://192.168.137.1:8080/fe/LoginServlet?" + data);
  conn = (HttpURLConnection) url.openConnection();
  
  conn.setRequestMethod("GET");// get或者post必须得全大写
  conn.setConnectTimeout(10000); // 连接的超时时间
  conn.setReadTimeout(5000); // 读数据的超时时间
  
  int responseCode = conn.getResponseCode();
  if(responseCode == 200) {
  InputStream is = conn.getInputStream();
  String state = getStringFromInputStream(is);
  return state;
  } else {
  Log.i(TAG, "访问失败: " + responseCode);
  }
  } catch (Exception e) {
  e.printStackTrace();
  } finally {
  if(conn != null) {
  conn.disconnect();// 关闭连接
  }
  }
  return null;
  }
  
  /**
   * 根据流返回一个字符串信息
   * @param is
   * @return
   * @throws IOException
   */
  private static String getStringFromInputStream(InputStream is) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int len = -1;
  
  while((len = is.read(buffer)) != -1) {
  baos.write(buffer, 0, len);
  }
  is.close();
  
  String html = baos.toString();// 把流中的数据转换成字符串, 采用的编码是: utf-8
  
  //String html = new String(baos.toByteArray(), "GBK");
  
  baos.close();
  return html;
  }
  }
  

  

  。。。。。。。。。。。。。main.xml。。。。。。。。
  
  

  
  

  
  

  
  

  
  
  

  、、、、、、权限、、、
  
  

  

  //Android提供的访问网页的方式
  //以下的红色字体可以代替上面的蓝色字体
  ----------------NetUtils2.java--------------
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.ArrayList;
  import java.util.List;
  

  import org.apache.http.HttpResponse;
  import org.apache.http.NameValuePair;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.message.BasicNameValuePair;
  

  import android.util.Log;
  

  public class NetUtils2 {
  

  private static final String TAG = "NetUtils";
  
  /**
   * 使用post的方式登录
   * @param userName
   * @param password
   * @return
   */
  public static String loginOfPost(String userName, String password) {
  HttpClient client = null;
  try {
  // 定义一个客户端
  client = new DefaultHttpClient();
  
  // 定义post方法
  HttpPost post = new HttpPost("http://192.168.137.1:8080/fe/LoginServlet");
  
  // 定义post请求的参数
  List parameters = new ArrayList();
  parameters.add(new BasicNameValuePair("name", userName));
  parameters.add(new BasicNameValuePair("password", password));
  
  // 把post请求的参数包装了一层.
  
  // 不写编码名称服务器收数据时乱码. 需要指定字符集为utf-8
  UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "utf-8");
  // 设置参数.
  post.setEntity(entity);
  
  // 设置请求头消息
  //post.addHeader("Content-Length", "20");
  
  // 使用客户端执行post方法
  HttpResponse response = client.execute(post);// 开始执行post请求, 会返回给我们一个HttpResponse对象
  
  // 使用响应对象, 获得状态码, 处理内容
  int statusCode = response.getStatusLine().getStatusCode();// 获得状态码
  if(statusCode == 200) {
  // 使用响应对象获得实体, 获得输入流
  InputStream is = response.getEntity().getContent();
  String text = getStringFromInputStream(is);
  return text;
  } else {
  Log.i(TAG, "请求失败: " + statusCode);
  }
  } catch (Exception e) {
  e.printStackTrace();
  } finally {
  if(client != null) {
  client.getConnectionManager().shutdown();// 关闭连接和释放资源
  }
  }
  return null;
  }
  

  /**
   * 使用get的方式登录
   * @param userName
   * @param password
   * @return 登录的状态
   */
  public static String loginOfGet(String userName, String password) {
  HttpClient client = null;
  try {
  // 定义一个客户端
  client = new DefaultHttpClient();
  
  // 定义一个get请求方法
  String data = "name=" + userName + "&password=" + password;
  HttpGet get = new HttpGet("http://192.168.137.1:8080/fe/LoginServlet?" + data);
  
  // response 服务器相应对象, 其中包含了状态信息和服务器返回的数据
  HttpResponse response = client.execute(get);// 开始执行get方法, 请求网络
  
  // 获得响应码
  int statusCode = response.getStatusLine().getStatusCode();
  
  if(statusCode == 200) {
  InputStream is = response.getEntity().getContent();
  String text = getStringFromInputStream(is);
  return text;
  } else {
  Log.i(TAG, "请求失败: " + statusCode);
  }
  } catch (Exception e) {
  e.printStackTrace();
  } finally {
  if(client != null) {
  client.getConnectionManager().shutdown();// 关闭连接, 和释放资源
  }
  }
  return null;
  }
  
  /**
   * 根据流返回一个字符串信息
   * @param is
   * @return
   * @throws IOException
   */
  private static String getStringFromInputStream(InputStream is) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int len = -1;
  
  while((len = is.read(buffer)) != -1) {
  baos.write(buffer, 0, len);
  }
  is.close();
  
  String html = baos.toString();// 把流中的数据转换成字符串, 采用的编码是: utf-8
  
  //String html = new String(baos.toByteArray(), "GBK");
  
  baos.close();
  return html;
  }
  }
  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-644163-1-1.html 上篇帖子: tomcat线上更新后台管理程序代码 下篇帖子: linux下两个tomcat通过不同端口访问不同项目
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表