在这个例子中我们通过继承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) |