精华内容
下载资源
问答
  • Android实现服务器登录

    千次阅读 2017-09-28 17:48:03
    Android实现服务器登录 一、使用MyEclipse创建一个servlet service project服务;( 在创建服务器时一定要创建servlet service project,不能创建servlet project)代码如下: package login; import java.io...

    Android实现服务器登录

    一、使用MyEclipse创建一个servlet service project服务;(

    在创建服务器时一定要创建servlet service project,不能创建servlet project)代码如下:

    package login;
    
    import java.io.IOException;
    
    import java.io.PrintWriter;
    
    import java.util.HashMap;
    
    import java.util.Map;
    
    import javax.servlet.ServletException;
    
    import javax.servlet.http.HttpServlet;
    
    import javax.servlet.http.HttpServletRequest;
    
    import javax.servlet.http.HttpServletResponse;
    
     
    
    public class AndroidLogin extends HttpServlet {
    
    public AndroidLogin() {
    
    super();
    
    }
    
    public void destroy() {
    
    super.destroy();
    
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    
    throws ServletException, IOException {
    
    doPost(request, response);
    
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    
    throws ServletException, IOException {
    
    response.setContentType("text/html");  
    
            PrintWriter out = response.getWriter();  
    
            Boolean flag =false;    
    
            String userName = request.getParameter("phone");  
    
            String password = request.getParameter("password");   
    
            if(userName.equals("123123")&&password.equals("123123"))   {
    
                flag =true;  }
    
            else {flag = false;}
    
            System.out.println("userName:"+userName+"password:"+password);
    
            out.print(flag);  
    
            out.flush();  
    
            out.close();
    
    }
    
    }

    二、修改WebRoot/WEB-INF/web.xml文件中的代码,加上如下代码:

    <servlet>
    
        <description>This is the description of my J2EE component</description>
    
        <display-name>This is the display name of my J2EE component</display-name>
    
        <servlet-name>AndroidLogin</servlet-name>
    
        <servlet-class>login.AndroidLogin</servlet-class>
    
      </servlet>
    
      <servlet-mapping>
    
        <servlet-name>AndroidLogin</servlet-name>
    
        <url-pattern>/login/AndroidLogin</url-pattern>
    
    </servlet-mapping>

    三、修改Android的Layout界面

    <?xml version="1.0"encoding="utf-8"?>
    <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"android:layout_width="match_parent"
        android:layout_height="match_parent">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="phone"
            android:ems="10"
            android:id="@+id/editText3"/>
    
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/editText"/>
    
        <Button
            android:text="登录"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button"/>
    </LinearLayout>

    四、修改LoginDemo代码(我自己建的Login Demo,默认为MainActivity)

    packagecom.example.jsondemo;
    
    importandroid.os.Bundle;
    importandroid.os.Handler;
    importandroid.os.Looper;
    importandroid.os.Message;
    importandroid.support.annotation.Nullable;
    importandroid.support.v7.app.AppCompatActivity;
    importandroid.view.View;
    importandroid.widget.Button;
    importandroid.widget.EditText;
    importandroid.widget.TextView;
    importandroid.widget.Toast;
    importorg.apache.http.HttpResponse;
    importorg.apache.http.client.HttpClient;
    importorg.apache.http.client.methods.HttpPost;
    importorg.apache.http.impl.client.DefaultHttpClient;
    importorg.apache.http.util.EntityUtils;
    importjava.io.ByteArrayOutputStream;
    importjava.io.InputStream;
    importjava.net.HttpURLConnection;
    importjava.net.URL;
    
    public classLoginDemoextendsAppCompatActivity {
        privateEditTextphone;
        privateEditTextpassword;
        privateButtonlogin;
        Handlerhandler=newHandler(){
            @Override
            public void handleMessage(Message msg) {
                switch(msg.what){
                    case1:
                        Toast.makeText(LoginDemo.this,"成功",Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        Toast.makeText(LoginDemo.this,"失败",Toast.LENGTH_SHORT).show();
                        break;
                }    }  };
        @Override
        protected void onCreate(@NullableBundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login_demo);
            password= (EditText) findViewById(R.id.editText);
            login= (Button) findViewById(R.id.button);
            phone= (EditText) findViewById(R.id.editText3);
            login.setOnClickListener(newView.OnClickListener() {
                @Override
                public void onClick(View v) {
                    xxx();   }    });   }
        private void xxx() {
            newThread(newRunnable() {
                @Override
                public void run() {
                    String msg ="";
                    try{
                        String phone1 =phone.getText().toString().trim();
                        String pw =password.getText().toString().trim();
                        String url ="http://10.0.2.2:8888/AndroidLogin/login/"+
                                "AndroidLogin?phone="+phone1+"&password="+pw+"";
                        HttpURLConnection conn = (HttpURLConnection)newURL(url).openConnection();
                        //设置请求方式,请求超时信息
                        conn.setRequestMethod("POST");
                        conn.setReadTimeout(5000);
                        conn.setConnectTimeout(5000);
                        //设置运行输入,输出:
                        conn.setDoOutput(true);
                        conn.setDoInput(true);
                        //Post方式不能缓存,需手动设置为false
                        conn.setUseCaches(false);
                        if(conn.getResponseCode() ==200) {
                            //使用httpClient发送post请求
                            HttpClient httpClient = newDefaultHttpClient();
                            HttpPost post =newHttpPost(url);
                            HttpResponse httpResponse = httpClient.execute(post);
                            String result = EntityUtils.toString(httpResponse.getEntity());
                            if(result.equals("true")){
                                Message message1 =newMessage();
                                message1.what=1;
                                handler.sendMessage(message1);
                            }else{
                                Message message1 =newMessage();
                                message1.what=0;
                                handler.sendMessage(message1);
                            }
                        }
                    }catch(Exception e) {
                        e.printStackTrace();   }    }    }).start();   }   }

    五、启动模拟器,分别输入123123/123123,点击“登录”按钮,效果,如图。

     

    展开全文
  • java.io.IOException: Server returned HTTP response code: 403 for URL: http:// 这个是什么异常呢? 当你使用java程序检索其他网站上的内容...如果是服务器需要访问权限,比如说你要登录才能访问的网页,那么你抓...

    java.io.IOException: Server returned HTTP response code: 403 for URL: http://

    这个是什么异常呢?

    当你使用java程序检索其他网站上的内容时,如果其服务器设置了禁止抓取,或者其访问需要权限,

    如果此时你去检索网页那么就会有异常该异常出现.

    如果是服务器需要访问权限,比如说你要登录才能访问的网页,那么你抓取不了的.

    如果是服务器端禁止抓取,那么这个你可以通过设置User-Agent来欺骗服务器

    connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

     

    什么是User Agent呢?

    User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。  

    一些网站常常通过判断 UA 来给不同的操作系统、不同的浏览器发送不同的页面,因此可能造成某些页面无法在某个浏览器中正常显示,但通过伪装 UA 可以绕过检测。

     

    转自:http://blog.csdn.net/zhaoshl_368/article/details/6603498

    转载于:https://www.cnblogs.com/xujanus/p/3423029.html

    展开全文
  • 客户机代码段 ...import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) throws UnknownHostExce...

    客户机代码段

    package A_Test;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    public class Client {
    	public static void main(String[] args) throws UnknownHostException, IOException {
    		int i = 0;
    		while(i++<10) {
    		    //在客户机使用循环或者run两次该文件以达到多线程效果
    			String host = "127.0.0." + i;
    			System.out.println("当前主机:" + host);
    			Socket a = new Socket(host,7539);
    		}
    	}
    }
    

    服务器代码段

    package A_Test;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.HashSet;
    public class Server {
    	public static void main(String[] args) throws IOException {
    		@SuppressWarnings("resource")
    		ServerSocket server = new ServerSocket(7539);
    		HashSet<Socket> users = new HashSet<Socket>();
    		for(int i = 1; i < 11; i++) {
    			Socket a = server .accept();
    			System.out.println("已有" + i + "人登录服务器!");
    			users.add(a);
    		}
    		for(Object obj : users) {
    			Socket a = (Socket) obj;
    			System.out.println("接口:" + a);
    		}
    	}
    }
    
    展开全文
  • 服务器端代码loginservlet.javaimport java.io.ioexception;import java.io.printwriter;import javax.servlet.servletexception;import javax.servlet.http.httpservlet;import javax.servlet....

    服务器端代码

    loginservlet.java

    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;

    import com.wut.bean.user;

    import com.wut.dao.userdao;

    public class loginservlet extends httpservlet {

    protected void doget(httpservletrequest request,httpservletresponse response) throws servletexception, ioexception {

    dopost(request, response);

    }

    protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {

    response.setcontenttype("text/html;charset=utf-8");

    response.setcharacterencoding("utf-8");

    printwriter out = response.getwriter();

    string username=request.getparameter("username");

    string password=request.getparameter("password");

    userdao dao=new userdao();

    user user = dao.login(username, password);

    if (user!=null) {

    out.println("succse");

    request.getsession().setattribute("username", username);

    }else{

    out.println("failure");

    }

    out.flush();

    out.close();

    }

    }

    userdao操作类

    import java.sql.connection;

    import java.sql.sqlexception;

    import org.apache.commons.dbutils.dbutils;

    import org.apache.commons.dbutils.queryrunner;

    import org.apache.commons.dbutils.handlers.beanhandler;

    import com.wut.bean.user;

    import com.wut.util.dbutil;

    public class userdao {

    /**

    * 用户名密码登录

    * @param username

    * @param password

    */

    public user login(string username,string password){

    user user=new user();

    connection conn = dbutil.getconnection();

    queryrunner queryrunner = new queryrunner();

    try {

    user = queryrunner.query(conn, "select * from user where username='"+username+"' and password='"+password+"'", new beanhandler(user.class));

    } catch (sqlexception e) {

    e.printstacktrace();

    }

    return user;

    }

    }

    这里我运用了commons-dbutils-1.7.jar库

    注意jsp的web.xml

    this is the description of my j2ee component

    this is the display name of my j2ee component

    loginservlet

    com.wut.servlet.loginservlet

    loginservlet

    /loginservlet

    安卓端代码

    webservice.java

    import java.io.bytearrayoutputstream;

    import java.io.ioexception;

    import java.io.inputstream;

    import java.net.httpurlconnection;

    import java.net.url;

    public class webservice {

    public static string executehttpget(string username, string password) {

    httpurlconnection conn = null;

    inputstream is = null;

    try {?

    string path = "https://xxx/webconference/loginservlet";

    path = path + "?username=" + username + "&password=" + password;

    conn = (httpurlconnection) new url(path).openconnection();

    conn.setconnecttimeout(3000);

    conn.setreadtimeout(3000);

    conn.setdoinput(true);

    conn.setrequestmethod("get");

    conn.setrequestproperty("charset", "utf-8");

    if (conn.getresponsecode() == 200) {

    is = conn.getinputstream();

    return parseinfo(is);

    }

    }catch (exception e) {

    e.printstacktrace();

    } finally {

    if (conn != null) {

    conn.disconnect();

    }

    if (is != null) {

    try {

    is.close();

    } catch (ioexception e) {

    e.printstacktrace();

    }

    }

    }

    return null;

    }

    private static string parseinfo(inputstream instream) throws exception {

    byte[] data = read(instream);

    return new string(data, "utf-8");

    }

    public static byte[] read(inputstream instream) throws exception {

    bytearrayoutputstream outputstream = new bytearrayoutputstream();

    byte[] buffer = new byte[1024];

    int len = 0;

    while ((len = instream.read(buffer)) != -1) {

    outputstream.write(buffer, 0, len);

    }

    instream.close();

    return outputstream.tobytearray();

    }

    }

    loginactivity

    import android.os.bundle;

    import android.os.handler;

    import android.view.view;

    import android.view.view.onclicklistener;

    import android.widget.autocompletetextview;

    import android.widget.button;

    import android.widget.edittext;

    public class loginactivity extends baseactivity {

    private autocompletetextview memailview;

    private edittext mpasswordview;

    private button submit;

    private string info=null;

    private static handler handler = new handler();

    @override

    protected void oncreate(bundle savedinstancestate) {

    super.oncreate(savedinstancestate);

    setcontentview(r.layout.activity_test);

    init();

    }

    private void init() {

    submit = (button) findviewbyid(r.id.email_sign_in_button);

    memailview=(autocompletetextview) findviewbyid(r.id.email);

    mpasswordview=(edittext) findviewbyid(r.id.password);

    submit.setonclicklistener(new onclicklistener() {

    @override

    public void onclick(view v) {

    new thread(new mythread()).start();

    }

    });

    }

    public class mythread implements runnable {//开启一个线程

    @override

    public void run() {

    info = webservice.executehttpget(memailview.gettext().tostring(), mpasswordview.gettext().tostring());

    handler.post(new runnable() {

    @override

    public void run() {

    if(info.equals("succse")){

    showtoast(info+"登录成功");

    }else{

    showtoast("登录失败");

    }

    }

    });

    }

    }

    }

    如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

    展开全文
  • 应工作需求,猫哥需要实现java工程远程登录linux服务器,并执行相应的linux命令。 自定义一个连接函数即可搞定。 private static String exeCommand(String host, int port, String user, String password, ...
  • ... 登录界面 登录 ...import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; public class DButils { private static final String DRIVER;
  • package njg.ssh.util; import java.io.BufferedReader;...import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.
  • 服务器端代码LoginServlet.javaimport java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet....
  • 问题原因:之前一直用root用户登录linux服务器,启动服务也是用root。后来root账号被收回了,给了一个新用户,但是用户权限有好多没有赋到,导致了这个导出功能不能用了。 问题解决:百度了下,是临时文件夹没有权限...
  • Android 登录界面连接PHP后台服务器

    千次阅读 2015-04-07 08:24:57
    package com.login.main;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import org.apache.http.HttpEntit
  • 问题原因:之前一直用root用户登录linux服务器,启动服务也是用root。后来root账号被收回了,给了一个新用户,但是用户权限有好多没有赋到,导致了这个导出功能不能用了。 问题解决:百度了下,...
  • <td colspan="2"> 用户登录界面</td> 用户名: <td> <input Type="text" name="logonName"> 密  码: <td> <input Type="password" name="password"></td> ...
  • import java.io.IOException; import java.io.PrintWriter; import java.sql.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpSe
  • 一般我们获取文件的时候除了直接通过接口读取还有邮箱和ftp,这次写关于ftp读取文件的流程和常见的问题 1、ftp连接和测试 public class Ftp { private static final ... //ftp服务器登录凭证 private String ho...
  • 部署在LINUX上的SSO服务器近日每隔几天就会当掉,查看日志有这样的错误:java.io.IOException: Too many open files。网上查了下,说是由于linux有打开文件句柄的限制。可以使用ulimit -a 查看当前用户可最大打开的...
  • 1.自己准备好公众号账号等东西,不懂的可以先百度一波2.建立请求主体,微信会从这个入口来验证,是否... import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; imp...
  • !... final String pwd =mPwd.getText().toString().trim(); if(TextUtils.isEmpty(phone) || TextUtils.isEmpty(pwd)){ ... } catch (IOException e) { e.printStackTrace(); } } }.start();
  • 登录系统后,隔一段时间后再操作出现java.io.IOException: 远程主机强迫关闭了一个现有的连接 原因: redis服务器配置了timeout超时时间,spring serivice层中注入StringRedisTemplate,过了超时时间后该service对象...
  • 微信小程序登录注册demo+java服务器(二)

    千次阅读 热门讨论 2018-05-22 12:16:32
    四.eclipse 1.关键代码及注解 LoginServlet package yan.servlet; ...import java.io.File;...import java.io....import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; im...
  • importjava.io.IOException;importjava.io.PrintWriter;importjava.sql.*;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletReq...
  • packagecom.itcast....importjava.io.IOException;importjava.io.InputStream;importjava.net.HttpURLConnection;importjava.net.URL;importandroid.os.Bundle;importandroid.os.Handler;importandroid.os.Me...
  • 本文将带你了解Android应用开发之Android通过Tomcat用JSP做服务器实现用户登录(MySQL数据库),希望本文对大家学Android有所帮助。服务器端代码LoginServlet.javaimport java.io.IOException;import java.io....
  • packagecom.example.java4androidmysql; importjava.io.IOException; importjava.io.UnsupportedEncodingException; importjava.util.ArrayList; import...
  • 1.自己准备好公众号账号等东西,不懂的可以先百度一波2.建立请求主体,微信会从这个入口来验证,是否...import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import ja...
  • java.io.IOException: Server returned HTTP response code: 403 for URL: http:// 这个是什么异常呢? 当你使用java程序检索其他网站上的内容时,...如果是服务器需要访问权限,比如说你要登录才能访问的网页,那...
  • 后来我登录到部署的Linux 服务器上查看,的确 目录不存在了。 出现这种错误主要是因为 : 使用HTTP POST 请求,需要建立一个临时的文件目录(比如这里的【/tmp/tomcat.4124735587175…/work/Tomcat/localhost/…...
  • import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import...
  • 用jsch.jar session.connect() 登录sftp服务器上报错 :End of IO Stream Read  sftp End of IO Strea 1.概述  最近做了一个从sftp服务器上下载文件的java项目。用到了jsch-0.1.54.jar 2.过程  本机搭建了一...

空空如也

空空如也

1 2 3 4 5 ... 11
收藏数 209
精华内容 83
关键字:

服务器登录ioexception