Android使用jsp+sevlet+mysql实现简单的前后台登录系统

转载请注明出处:http://blog.csdn.net/dennisxzr/article/details/23847073(来自Dennis锐少的csdn博客)

简单的使用jsp+sevlet+mysql来创建android的服务端

实现客户端与服务端的交互

可以在此基础上练习 添加其他功能

客户端代码:

[java]  view plain  copy
  1. package com.android.cn;  
  2.   
  3.   
  4. import org.apache.http.client.methods.HttpPost;  
  5.   
  6.   
  7. import android.app.Activity;  
  8. import android.app.AlertDialog;  
  9. import android.content.DialogInterface;  
  10. import android.content.Intent;  
  11. import android.os.Bundle;  
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14. import android.widget.Button;  
  15. import android.widget.EditText;  
  16.   
  17.   
  18. public class AndroidDemosActivity extends Activity {  
  19.   
  20.   
  21.         private Button cancelBtn, loginBtn;  
  22.         private EditText userEditText, pwdEditText;  
  23.   
  24.   
  25.         @Override  
  26.         public void onCreate(Bundle savedInstanceState) {  
  27.                 super.onCreate(savedInstanceState);  
  28.                 setContentView(R.layout.main);  
  29.   
  30.   
  31.                 cancelBtn = (Button) findViewById(R.id.Canclebtn);  
  32.                 loginBtn = (Button) findViewById(R.id.Loginbtn);  
  33.   
  34.   
  35.                 userEditText = (EditText) findViewById(R.id.etUserName);  
  36.                 pwdEditText = (EditText) findViewById(R.id.etPassword);  
  37.   
  38.   
  39.                 cancelBtn.setOnClickListener(new OnClickListener() {  
  40.                         public void onClick(View v) {  
  41.                                 finish();  
  42.                         }  
  43.                 });  
  44.   
  45.   
  46.                 loginBtn.setOnClickListener(new OnClickListener() {  
  47.                         public void onClick(View v) {  
  48.                                 HttpPost request = new HttpPost("http://www.baidu.com");  
  49.                                 if (validate()) {  
  50.                                         if (login()) {  
  51.                                                 Intent intent = new Intent(AndroidDemosActivity.this,  
  52.                                                                 MainActivity.class);  
  53.                                                 startActivity(intent);  
  54.                                         } else {  
  55.                                                 showDialog("用户名称或者密码错误,请重新输入!");  
  56.                                         }  
  57.                                 }  
  58.                         }  
  59.                 });  
  60.         }  
  61.   
  62.   
  63.         private boolean login() {  
  64.                 String username = userEditText.getText().toString();  
  65.                 String pwd = pwdEditText.getText().toString();  
  66.                 String result = query(username, pwd);  
  67.                 if (result != null && result.equals("1")) {  
  68.                         return true;  
  69.                 } else {  
  70.                         return false;  
  71.                 }  
  72.         }  
  73.   
  74.   
  75.         private boolean validate() {  
  76.                 String username = userEditText.getText().toString();  
  77.                 if (username.equals("") && username == null) {  
  78.                         showDialog("用户名称是必填项!");  
  79.                         return false;  
  80.                 }  
  81.                 String pwd = pwdEditText.getText().toString();  
  82.                 if (pwd.equals("")) {  
  83.                         showDialog("用户密码是必填项!");  
  84.                         return false;  
  85.                 }  
  86.                 return true;  
  87.         }  
  88.   
  89.   
  90.         private void showDialog(String msg) {  
  91.                 AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  92.                 builder.setMessage(msg).setCancelable(false).setPositiveButton("确定",  
  93.                                 new DialogInterface.OnClickListener() {  
  94.                                         public void onClick(DialogInterface dialog, int id) {  
  95.                                         }  
  96.                                 });  
  97.                 AlertDialog alert = builder.create();  
  98.                 alert.show();  
  99.         }  
  100.   
  101.   
  102.         private String query(String username, String password) {  
  103.                 String queryString = "username=" + username + "&password=" + password;  
  104.                 String url = HttpUtil.BASE_URL + "servlet/ServiceTest?" + queryString;  
  105.                 return HttpUtil.queryStringForGet(url);  
  106.         }  
  107. }  
服务端的主要代码:

[java]  view plain  copy
  1. package com.server.an;  
  2.   
  3.   
  4. import java.io.IOException;  
  5. import java.io.PrintWriter;  
  6.   
  7.   
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.http.HttpServlet;  
  10. import javax.servlet.http.HttpServletRequest;  
  11. import javax.servlet.http.HttpServletResponse;  
  12.   
  13.   
  14. /** 
  15.  
  16. * Android客户端发来的请求 
  17. */  
  18. @SuppressWarnings("serial")  
  19. public class ServiceTest extends HttpServlet {  
  20.   
  21.   
  22.         public void doGet(HttpServletRequest request, HttpServletResponse response)  
  23.                         throws ServletException, IOException {  
  24.                 System.out.println("do Get");  
  25.                 response.setContentType("text/html");  
  26.                 response.setCharacterEncoding("utf-8");  
  27.                 PrintWriter out = response.getWriter();  
  28.                 UserDao dao = new UserDaoImpl();  
  29.                 // 获得客户端请求参数  
  30.                 String username = request.getParameter("username");  
  31.                 String password = request.getParameter("password");  
  32.   
  33.   
  34.                 User u = dao.login(username, password);  
  35.                 if (u != null) {  
  36.                         // 响应客户端内容,登录成功  
  37.                         //out.print("登录成功");  
  38.                         out.print("登录成功");  
  39.                 } else {  
  40.                         // 响应客户端内容,登录失败  
  41.                         out.print("登录失败");  
  42.                 }  
  43.                 out.flush();  
  44.                 out.close();  
  45.         }  
  46.   
  47.   
  48.         public void doPost(HttpServletRequest request, HttpServletResponse response)  
  49.                         throws ServletException, IOException {  
  50.                 System.out.println("do Post");  
  51.                 String username = request.getParameter("username");  
  52.                 PrintWriter out = response.getWriter();  
  53.                 UserDao dao = new UserDaoImpl();  
  54.                 // 获得客户端请求参数  
  55.                 String password = request.getParameter("password");  
  56.   
  57.   
  58.                 User u = dao.login(username, password);  
  59.                 if (u != null) {  
  60.                         // 响应客户端内容,登录成功  
  61.                         //out.print("登录成功");  
  62.                         response.sendRedirect("/AndroidServer/success.jsp");  
  63.                 } else {  
  64.                         // 响应客户端内容,登录失败  
  65.                         out.print("登录失败");  
  66.                 }  
  67.                 out.flush();  
  68.                 out.close();  
  69.         }  
  70.   
  71.   
  72.         public void init() throws ServletException {  
  73.         }  
  74.   
  75.   
  76.         public ServiceTest() {  
  77.                 super();  
  78.         }  
  79.   
  80.   
  81.         public void destroy() {  
  82.                 super.destroy();  
  83.         }  
  84.   
  85.   
  86. }  
源码下载: http://download.csdn.net/detail/xzr1526/7202127
转载请注明出处: http://blog.csdn.net/dennisxzr/article/details/23847073 (来自Dennis锐少的csdn博客)

猜你喜欢

转载自blog.csdn.net/leansmall/article/details/80350125