Android 实现Http get 和post操作


配置服务器

这个是我的Web实体

index.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'index.jsp' starting page</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.     <!-- 
  19.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  20.     -->  
  21.   </head>  
  22.     
  23.   <body>   
  24. <h3>GET方法</h3>  
  25.      <form action="Test" method="get">  
  26. <P>学号:<input name="id" type="text" /></P>  
  27. <p>姓名:<input name="name" type="text" /></p>  
  28. <p>          <input name="" type="submit" value="确定" />  
  29.            
  30.   <input name="cancel" type="reset" value="取消" />  
  31. </p>  
  32.   
  33. </form>  
  34. <h3>POST方法</h3>  
  35.  <form action="Test" method="post">  
  36. <P>学号:<input name="id" type="text" /></P>  
  37. <p>姓名:<input name="name" type="text" /></p>  
  38. <p>          <input name="" type="submit" value="确定" />  
  39.            
  40.   <input name="cancel" type="reset" value="取消" />  
  41. </p>  
  42.   
  43. </form>  
  44.   </body>  
  45.     
  46. </html>  


配置Servlet

Test.java

  1. package rw.servlet;  
  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 Test extends HttpServlet {  
  12.   
  13.     /** 
  14.      * The doGet method of the servlet. <br> 
  15.      * 
  16.      * This method is called when a form has its tag value method equals to get. 
  17.      *  
  18.      * @param request the request send by the client to the server 
  19.      * @param response the response send by the server to the client 
  20.      * @throws ServletException if an error occurred 
  21.      * @throws IOException if an error occurred 
  22.      */  
  23.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  24.             throws ServletException, IOException {  
  25.   
  26.         request.setCharacterEncoding("gb2312");  
  27.         response.setContentType("text/html;charset=gb2312");  
  28.         PrintWriter out = response.getWriter();  
  29.         String idString=request.getParameter("id");  
  30.         String nameString=request.getParameter("name");  
  31.         out.println(idString);  
  32.         out.println(nameString);  
  33.         out.flush();  
  34.         out.close();  
  35.     }  
  36.   
  37.     /** 
  38.      * The doPost method of the servlet. <br> 
  39.      * 
  40.      * This method is called when a form has its tag value method equals to post. 
  41.      *  
  42.      * @param request the request send by the client to the server 
  43.      * @param response the response send by the server to the client 
  44.      * @throws ServletException if an error occurred 
  45.      * @throws IOException if an error occurred 
  46.      */  
  47.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  48.             throws ServletException, IOException {  
  49.   
  50.         request.setCharacterEncoding("gb2312");  
  51.         response.setContentType("text/html;charset=gb2312");  
  52.         PrintWriter out = response.getWriter();  
  53.         String idString=request.getParameter("id");  
  54.         String nameString=request.getParameter("name");  
  55.         out.println(idString);  
  56.         out.println(nameString);  
  57.         out.flush();  
  58.         out.close();  
  59.     }  
  60.   
  61. }  
  • 1
  • 2
  • 下一页

相关内容