Chinaunix首页 | 论坛 | 博客
  • 博客访问: 411290
  • 博文数量: 101
  • 博客积分: 2324
  • 博客等级: 大尉
  • 技术积分: 887
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-19 19:28
文章分类

全部博文(101)

文章存档

2012年(3)

2011年(60)

2010年(38)

分类: 系统运维

2010-11-21 21:21:58

HttpServlet方法如下:
JAVA代码如下:
 

package shark.zd.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;

import org.omg.CORBA.Request;

import com.sun.jmx.snmp.Enumerated;


public class TestHttpServlet extends HttpServlet {

    
    
    




    public void doGet(HttpServletRequest request, HttpServletResponse response)
    {
    execute(request, response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
    {
        execute(request, response);
    }
    public void execute(HttpServletRequest request,
            HttpServletResponse response){
        //容器:装载信息object

        /*
         * Enumerationg有几个方法
         * 1,判断内部有没有任务.hasMoreElements
         * 2,获得下一个信息.nextElement
         */

        Enumeration headers = request.getHeaderNames();
        PrintWriter pw;
        try {
            pw = response.getWriter();
        
        pw.print("

");
        pw.print("
");
        pw.print("
");
        pw.print("
");
        pw.print("
");
        
        while(headers.hasMoreElements()){
            Object obj = headers.nextElement();
            String headName = String.valueOf(obj);
            String headValue = request.getHeader(headName);
            pw.print("
");
            pw.print("
");
            pw.print("
");
            
            
        }
        
        pw.print("
");
        pw.print("头信息名");
        
        pw.print("
");
        pw.print("头信息值");
        pw.print("
");
            pw.print(headName);
            pw.print("
");
            pw.print(headValue);
            pw.print("/td");
            pw.print("
");
        pw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }
        
        
    }
    
}

HTML代码如下:


 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>test.html</title>
    

  </head>
  
  <body>
   <form action="wlecome" method="get">
   请输入你的名字:<input type="text" name="username" /></br>
   
   <input type="radio" name="testRadio" value="man"></br>
   <input type="radio" name="testRadio" value="woman"></br>
   
   <select name="sw">
           <option value="s">s</option>
           <option value="w">w</option>
           </select>
           
           <input type="checkbox" name="c1" value="c1">
           <input type="checkbox" name="c2" value="c2">
           <input type="checkbox" name="c3" value="c3">
           <input type="checkbox" name="c4" value="c4">
   <input type="submit" value="提交" />
   
   </form>
   <a href="testMethod">超链接方法</a>>
  </body>
</html>

 

web.xml代码如下:


 

<!-- servlet的名字和本个Servlet需要访问java类文件全地址 -->
<servlet>
        <servlet-name>testMethodServlet</servlet-name>
        <servlet-class>shark.zd.servlets.TestHttpServlet</servlet-class>

</servlet>
<!-- 寻找外部访问url的Servletname -->
<servlet-mapping>
        <servlet-name>testMethodServlet</servlet-name>
        <!-- 允许外部访问的url -->
        <url-pattern>/testMethod</url-pattern>
</servlet-mapping>

 

实验结果:得到访问网页的各个信息

阅读(5118) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-11-22 17:53:10

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com