android登录tomcat服务器并查找数据库的内容
2012-11-05 14:00
731 查看
目前的项目需要涉及到与服务器的交互,就把大概的代码贴出来交流一下。
大概的内容就是android端用post方法与服务器连接,然后在服务器端用servlet去根据用户名去查找数据库,如果用户名存在那么登录成功并查找存取的数据。
android端的内容:
然后是服务器上的servlet:
由于本人也只是很肤浅的学习了一下,有不足的地方希望大家提出来。
大概的内容就是android端用post方法与服务器连接,然后在服务器端用servlet去根据用户名去查找数据库,如果用户名存在那么登录成功并查找存取的数据。
android端的内容:
final String uriConnection = "http://192.168.0.102:8080/IMLOP/servlet/MyServlet.servlet"; //用户名及其密码 String username; String password; //获取用户名及其密码 ed_username = (EditText) findViewById(R.id.username); ed_password = (EditText) findViewById(R.id.password); username = ed_username.getText().toString(); password = ed_password.getText().toString(); HttpResponse httpResponse = null; List<NameValuePair> params = new ArrayList<NameValuePair>(); if (!username.equals("") && !password.equals("")) { params.add(new BasicNameValuePair("username", username)); params.add(new BasicNameValuePair("password", password)); } try { httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); httpResponse = new DefaultHttpClient().execute(httpRequest); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (httpResponse.getStatusLine().getStatusCode() == 200) { Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show(); }
然后是服务器上的servlet:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username= request.getParameter("username"); String password= request.getParameter("password"); response.getOutputStream().print(">>>>>>"); System.out.println("username:" + username); System.out.println("password:" + password); // 下面这的字符串是用于去数据库查找是否有匹配的用户 String parameters = username+ "," + password; //匹配用户名及其密码 用了hibernate List user = HibernateService.execProcedure("p_userBylogininfo_chk", parameters); //查询数据库的内容,VehicleDao类就是存放的一些数据的处理方法,使用其中的queryHQL方法查找 VehicleDao search_dao = new VehicleDao(); String search_information; search_information = "select cs.publishTime,cs.arriveAt " + "from UserserviceCargosource cs "; ArrayList<?> list; try { list = search_dao.queryHQL(search_information); //打印查找到的内容 if (list != null && list.size() > 0) { for (Object ob : list) { Object[] id=(Object[])ob; System.out.println(id[0]+" "+id[1]); } } }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
由于本人也只是很肤浅的学习了一下,有不足的地方希望大家提出来。
相关文章推荐
- 前段时间,接手一个项目使用的是原始的jdbc作为数据库的访问,发布到服务器上在运行了一段时间之后总是会出现无法访问的情况,登录到服务器,查看tomcat日志发现总是报如下的错误。 Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected est
- android利用servlet获取服务器数据库数据进行登录
- 利用简易Tomcat服务器结合MysqL实现Android手机注册与登录(服务器部分)
- tomcat服务器用Servlet类查找磁盘文件上的Json信息,如果匹配则在浏览器上显示出该条内容的全部信息
- 利用简易Tomcat服务器结合MysqL实现Android手机注册与登录(客户端部分)
- 利用简易Tomcat服务器结合MysqL实现Android手机注册与登录(客户端部分)
- Android客户端与Tomcat服务器通信实现登录验证
- 利用简易Tomcat服务器结合MysqL实现Android手机注册与登录(服务器部分)
- SQLserver2005服务器登录名、角色、数据库用户、架构
- 利用JDBC连接服务器数据库(Android)
- Android虚拟机访问tomcat服务器下的文件
- Android 登录界面连接PHP后台服务器
- 54款开源服务器软件(内容管理、数据库、电子商务、邮件服务器、文件传输、操作系统、安全、小公司服务 .
- Cognos安装与配置(使用自带的数据库derby+服务器Tomcat)
- 安装网站时出现“连接数据库出现数据库服务器或登录密码无效,无法连接数据库,请重新设定”解决方法
- 加入数据库mysql实现android注册登陆功能的客户端服务器源码与解析
- 自己的笔记___摘自别人(Android数据库中查找一条数据 query方法详解)
- Android 开发 上传用户头像到服务器的数据库再获取显示
- Android通过webservice连接SQLServer 详细教程(数据库+服务器+客户端)
- Linux tomcat服务器与android 4.1手机应用交户实例