歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 實現Http get 和post操作

Android 實現Http get 和post操作

日期:2017/3/1 11:07:25   编辑:Linux編程

配置服務器

這個是我的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. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'index.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <h3>GET方法</h3>
  22. <form action="Test" method="get">
  23. <P>學號:<input name="id" type="text" /></P>
  24. <p>姓名:<input name="name" type="text" /></p>
  25. <p> <input name="" type="submit" value="確定" />
  26. <input name="cancel" type="reset" value="取消" />
  27. </p>
  28. </form>
  29. <h3>POST方法</h3>
  30. <form action="Test" method="post">
  31. <P>學號:<input name="id" type="text" /></P>
  32. <p>姓名:<input name="name" type="text" /></p>
  33. <p> <input name="" type="submit" value="確定" />
  34. <input name="cancel" type="reset" value="取消" />
  35. </p>
  36. </form>
  37. </body>
  38. </html>


配置Servlet

Test.java

  1. package rw.servlet;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. public class Test extends HttpServlet {
  9. /**
  10. * The doGet method of the servlet. <br>
  11. *
  12. * This method is called when a form has its tag value method equals to get.
  13. *
  14. * @param request the request send by the client to the server
  15. * @param response the response send by the server to the client
  16. * @throws ServletException if an error occurred
  17. * @throws IOException if an error occurred
  18. */
  19. public void doGet(HttpServletRequest request, HttpServletResponse response)
  20. throws ServletException, IOException {
  21. request.setCharacterEncoding("gb2312");
  22. response.setContentType("text/html;charset=gb2312");
  23. PrintWriter out = response.getWriter();
  24. String idString=request.getParameter("id");
  25. String nameString=request.getParameter("name");
  26. out.println(idString);
  27. out.println(nameString);
  28. out.flush();
  29. out.close();
  30. }
  31. /**
  32. * The doPost method of the servlet. <br>
  33. *
  34. * This method is called when a form has its tag value method equals to post.
  35. *
  36. * @param request the request send by the client to the server
  37. * @param response the response send by the server to the client
  38. * @throws ServletException if an error occurred
  39. * @throws IOException if an error occurred
  40. */
  41. public void doPost(HttpServletRequest request, HttpServletResponse response)
  42. throws ServletException, IOException {
  43. request.setCharacterEncoding("gb2312");
  44. response.setContentType("text/html;charset=gb2312");
  45. PrintWriter out = response.getWriter();
  46. String idString=request.getParameter("id");
  47. String nameString=request.getParameter("name");
  48. out.println(idString);
  49. out.println(nameString);
  50. out.flush();
  51. out.close();
  52. }
  53. }
Copyright © Linux教程網 All Rights Reserved