你好,游客 登录 注册 搜索
背景:
阅读新闻

Android学习之Http使用Post方式进行数据提交

[日期:2013-03-02] 来源:Linux社区  作者:wulianghuan [字体:大 中 小]

我们再创建一个服务端工程,一个web project,这里创建一个myhttp的工程,先给它创建一个servlet,用来接收参数访问。

创建的servlet配置如下:

  1. <servlet> 
  2.         <description>This is the description of my J2EE component</description> 
  3.         <display-name>This is the display name of my J2EE component</display-name> 
  4.         <servlet-name>LoginAction</servlet-name> 
  5.         <servlet-class>com.login.manager.LoginAction</servlet-class> 
  6.     </servlet> 
  7.  
  8.     <servlet-mapping> 
  9.         <servlet-name>LoginAction</servlet-name> 
  10.         <url-pattern>/servlet/LoginAction</url-pattern> 
  11.     </servlet-mapping> 

建立的LoginAction.java类继承HttpServlet:

  1. package com.login.manager; 
  2.  
  3. import java.io.IOException; 
  4. import java.io.PrintWriter; 
  5.  
  6. import javax.servlet.ServletException; 
  7. import javax.servlet.http.HttpServlet; 
  8. import javax.servlet.http.HttpServletRequest; 
  9. import javax.servlet.http.HttpServletResponse; 
  10.  
  11. public class LoginAction extends HttpServlet { 
  12.  
  13.     /** 
  14.      * Constructor of the object. 
  15.      */ 
  16.     public LoginAction() { 
  17.         super(); 
  18.     } 
  19.  
  20.     /** 
  21.      * Destruction of the servlet. <br> 
  22.      */ 
  23.     public void destroy() { 
  24.         super.destroy(); // Just puts "destroy" string in log 
  25.         // Put your code here 
  26.     } 
  27.  
  28.     /** 
  29.      * The doGet method of the servlet. <br> 
  30.      * 
  31.      * This method is called when a form has its tag value method equals to get. 
  32.      *   
  33.      * @param request the request send by the client to the server 
  34.      * @param response the response send by the server to the client 
  35.      * @throws ServletException if an error occurred 
  36.      * @throws IOException if an error occurred 
  37.      */ 
  38.     public void doGet(HttpServletRequest request, HttpServletResponse response) 
  39.             throws ServletException, IOException { 
  40.             this.doPost(request, response); 
  41.     } 
  42.  
  43.     /** 
  44.      * The doPost method of the servlet. <br> 
  45.      * 
  46.      * This method is called when a form has its tag value method equals to post. 
  47.      *   
  48.      * @param request the request send by the client to the server 
  49.      * @param response the response send by the server to the client 
  50.      * @throws ServletException if an error occurred 
  51.      * @throws IOException if an error occurred 
  52.      */ 
  53.     public void doPost(HttpServletRequest request, HttpServletResponse response) 
  54.             throws ServletException, IOException { 
  55.  
  56.         response.setContentType("text/html;charset=utf-8"); 
  57.         request.setCharacterEncoding("utf-8"); 
  58.         response.setCharacterEncoding("utf-8"); 
  59.         PrintWriter out = response.getWriter(); 
  60.         String userName = request.getParameter("username"); 
  61.         String passWord = request.getParameter("password"); 
  62.         System.out.println("userName:"+userName); 
  63.         System.out.println("passWord:"+passWord); 
  64.         if(userName.equals("admin") && passWord.equals("123456")){ 
  65.             out.print("login successful!"); 
  66.         }else{ 
  67.             out.print("login failed"); 
  68.         } 
  69.         out.flush(); 
  70.         out.close(); 
  71.     } 
  72.  
  73.     /** 
  74.      * Initialization of the servlet. <br> 
  75.      * 
  76.      * @throws ServletException if an error occurs 
  77.      */ 
  78.     public void init() throws ServletException { 
  79.         // Put your code here 
  80.     } 
  81.  
  82. } 

我们运行java project,控制台输出如下:

>>>login successful!

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

 

 

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款