Android+Apache+Servlet+MySQL简易粗糙Mini登录器实现

Android是在Google手下改进的,其互联网方面的支持当然不容小觑啦,好啦,废话不说,下面还是编写我们的Mini登录器吧~:

①建立一个Android工程(这不废话么)

给login这个Button添加点击监听器

private Button login;

 login.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                login();
            }
            
        });

②然后呢,就开始写login()方法啦

private static final String ServletUri= http://XXX.XXX.XXX.XXX:8080/MyClub/Login;

其中ServletUri中的MyClub是下文建立Servlet的Web Project项目工程名,Login是Servlet名称,这都是默认的,你可以改的,保持大小写哟~

   public void login(){
        post = new HttpPost(ServletUri);
        List param = new ArrayList();
        param.add(new BasicNameValuePair("username",userName.getText().toString()));
        param.add(new BasicNameValuePair("userpwd",userPwd.getText().toString()));
        try {
            post.setEntity(new UrlEncodedFormEntity(param,HTTP.UTF_8));
            try {
                HttpResponse response = new DefaultHttpClient().execute(post);
                if(response.getStatusLine().getStatusCode() == 201){
                        Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                }
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

先不要怀疑response.getStatusLine().getStatusCode() == 201 为什么为201 这是我乱设的哈~

③新建一个Web Project工程,我取名为MyClub,记住你的机器要安装Apache Tomcat哟

添加一个Servlet,我取名为Login

如果你发现你新建的web project工程里诸如这种

package com.xj.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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

/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
 private static final long serialVersionUID = 1L;
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String CONNECT = "jdbc:MySQL://localhost:3306/XXX";    //XXX为你的数据库名称
    private String baseName = "XXX";       //XXX为你的数据库账号
    private String basePwd = "XXX";   //XXX为你的数据库密码
    private Connection con;
    private Statement statement;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
        try {
            Class.forName(DRIVER);
            con = DriverManager.getConnection(CONNECT,baseName,basePwd);
            statement = con.createStatement();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub   
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
     String username = request.getParameter("username");
        String userpwd = request.getParameter("userpwd");
        try {
            ResultSet rs = statement.executeQuery("select * from user where username='" + username +"' and userpwd='" + userpwd + "'");
            rs.beforeFirst();
            if(rs.next()){
                response.setStatus(201);    //这个201就是与上面那个login()方法那个乱设的201的编码相对应啦~
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 }

}

 

import javax.servlet.http.HttpServlet;导入说找不到你可以在你web project下WebContent/WEB-INF/lib下添加这两个库jsp-api.jarservlet-api.jar这两个库在\Tomcat 6.0\lib\下呆着呢
④这时候设计数据库了,我用的是MySQL,用Navicat For MySQL可视化操作MySQL快点

账号密码就是③中的XXX啦

新建连接->新建数据库XXX->新建一张表,我的表名叫user

username                       int                          5                      0            主键          自动递增      无符号      填充0

userpwd                        varchar                  10                     0


⑤OK,这时候Run一下这个Web Project,然后运行下Android工程吧,如果账户密码符合就会有个Toast告诉你登陆成功哟!

你可能感兴趣的:(Android+Apache+Servlet+MySQL简易粗糙Mini登录器实现)