Chinaunix首页 | 论坛 | 博客
  • 博客访问: 523918
  • 博文数量: 151
  • 博客积分: 7010
  • 博客等级: 少将
  • 技术积分: 1405
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-22 14:32
文章分类

全部博文(151)

文章存档

2011年(1)

2010年(23)

2009年(1)

2008年(126)

我的朋友

分类: 系统运维

2008-07-04 17:15:50

    在这个例子中我们通过继承HttpServlet来开发servlet。通过继承HttpServlet,需要重写doGet、doPost方法。这是目前用得最多的一种方法。

这里介绍一下表单提交数据get请求和post请求的区别:
1、从安全性看get 不如 post。
   get提交的数据会在地址栏中显示
2、从提交的内容看get 小于 post
   get提交的数据不能大于2K,而post提交的数据理论上不受限制,但实际编程中建议不要大于64K
3、从请求响应速度看 get 快于 post
   get要求服务器立即处理请求,而post请求可能形成一个队列请求

一、代码(HttpSvl.java)
import javax.servlet.http.*;
import java.io.*;
public class HttpSvl extends HttpServlet{
    public HttpSvl() {
    }
   
    //处理get请求
    public void doGet(HttpServletRequest req, HttpServletResponse res)
    {
      try{
      PrintWriter pw = res.getWriter();
      pw.println("call doGet1");
     }
     catch(Exception e)
     {
      e.printStackTrace();
     }
    }
   
    //处理post请求
    public void doPost(HttpServletRequest req, HttpServletResponse res)
    {
     this.doGet(req,res);
    }
}

二、web.xml(在前两个例子基础上增加)


  Welcome to Tomcat
 
     Welcome to Tomcat
 

 
 
   
    hello
    
    test
 
 
 
   
    hello
    
    
    /list
 

 
  
    helloGeneric
    genericSvl
 
 
 
    helloGeneric
    /gen
 

 
 
    helloHttp
    HttpSvl
 
 
 
    helloHttp
    /httpsvl
 



三、访问servlet
 1、启动tomcat
 2、访问  



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