Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2567295
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-07-11 14:46:15

以下是servlet文件hiworld.java
其中doPost方法处理以post方式发送到数据。
 
  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import java.util.Enumeration;

  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 hiworld extends HttpServlet {

  9.     /**
  10.      * Constructor of the object.
  11.      */
  12.     public hiworld() {
  13.         super();
  14.     }

  15.     /**
  16.      * Destruction of the servlet.

  17.      */
  18.     public void destroy() {
  19.         super.destroy(); // Just puts "destroy" string in log

  20.         // Put your code here

  21.     }

  22.     /**
  23.      * The doGet method of the servlet.

  24.      *
  25.      * This method is called when a form has its tag value method equals to get.
  26.      *
  27.      * @param request the request send by the client to the server
  28.      * @param response the response send by the server to the client
  29.      * @throws ServletException if an error occurred
  30.      * @throws IOException if an error occurred
  31.      */
  32.     
  33.     int count=0;
  34.     public void doGet(HttpServletRequest request, HttpServletResponse response)
  35.             throws ServletException, IOException {

  36.         //建立输出类型

  37.         response.setContentType("text/html");
  38.         //得到输出流

  39.         PrintWriter out = response.getWriter();
  40.         int local_count;
  41.         //实现local_count在多线程环境下的同步

  42.         synchronized(this){
  43.             local_count = ++count;
  44.         }
  45.         
  46.         out.println("");
  47.         out.println("");
  48.         out.println(" A Servlet");
  49.         out.println(" ");
  50.         out.print(" This is ");
  51.         out.print(this.getClass());
  52.         out.println(", using the GET method");
  53.         out.println("ok?");
  54.         out.println("
    Accessed "
    +"

    "+local_count+" times

    "
    );
  55.         out.println(" ");
  56.         out.println("");
  57.         out.flush();
  58.         out.close();
  59.     }

  60.     /**
  61.      * The doPost method of the servlet.

  62.      *
  63.      * This method is called when a form has its tag value method equals to post.
  64.      *
  65.      * @param request the request send by the client to the server
  66.      * @param response the response send by the server to the client
  67.      * @throws ServletException if an error occurred
  68.      * @throws IOException if an error occurred
  69.      */
  70.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  71.             throws ServletException, IOException {

  72.         response.setContentType("text/html");
  73.         PrintWriter out = response.getWriter();
  74.         
  75.         out.println("");
  76.         out.println("");
  77.         out.println(" A Servlet");
  78.         out.println(" ");
  79.         //获取以POST方式传递的表单中的参数名称

  80.         Enumeration e = request.getParameterNames();
  81.         while(e.hasMoreElements()){
  82.             out.println("----------------------
    "
    );
  83.             out.println((String)e.nextElement());
  84.             out.println("
    "
    );
  85.         }
  86.         
  87.         out.println("welcome "+ ""+ request.getParameter("username")+"" );    
  88.         
  89.         out.print("
    "
    );        
  90.         out.print(" This is ");
  91.         out.print(this.getClass());
  92.         out.println(", using the POST method");
  93.         out.println(" ");
  94.         out.println("");
  95.         out.flush();
  96.         out.close();
  97.     }

  98.     /**
  99.      * Initialization of the servlet.

  100.      *
  101.      * @throws ServletException if an error occurs
  102.      */
  103.     public void init() throws ServletException {
  104.         // Put your code here

  105.     }

  106. }
以下html代码以post方式提交表单内容给hiworld
 
  1. <form method="post" action="/hi/servlet/hiworld" />
  2.        <input type="text" name="username" />
  3.        <input type="password" name="password" />
  4.        <input type="submit" value="Post"/>
  5.     </form>
阅读(752) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

tianbianfei2011-07-12 16:17:45

牛人,俺刚从java转android,请教高手一个问题:大家快来参与讨论吧:http://doumiw.com/market/community/t!showTopic.do?topicId=24