Servlet的doGet与doPost方法的区别与使用
一,区别
在使用表单提交数据到服务器的时候有两张方式可共选择,一个是post一个是get。可在<form>中的method属性中指定提交的方式。如:<form action="inputForm"method="get">,如果不指定method属性,则会默认该属性为”get”方式。
Get和post都能够提交数据,那么他们有什么不同呢?
不同点一:
通过get方式提交的数据有大小的限制,通常在1024字节左右。也就是说如果提交的数据很大,用get方法就可需要小心;而post方式没有数据大小的限制,理论上传送多少数据都可以。
不同点二:
通过get传递数据,实际上是将传递的数据按照”key,value”的方式跟在URL的后面来达到传送的目的的;而post传递数据是通过http请求的附件进行的,在URL中并没有明文显示。
不同点三:
通过Get方式提交的数据安全性不高,而Post方式的更加安全~
二,使用
下面举个例子说明:
1.post提交--doPost方法
login.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>登录</title>
- </head>
- <body>
- <h3>登录</h3>
- <hr>
- <form action="LoginServlet" method="post">
- 用户名:<input type="text" name="username"/><br>
- 密码:<input type="password" name="password"/><br>
- <input type="submit" />
- </form>
- </body>
- </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录</title> </head> <body> <h3>登录</h3> <hr> <form action="LoginServlet" method="post"> 用户名:<input type="text" name="username"/><br> 密码:<input type="password" name="password"/><br> <input type="submit" /> </form> </body> </html>LoginServlet:
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- request.setCharacterEncoding("UTF-8");
- response.setCharacterEncoding("UTF-8");
- //向服务器发送请求获取到参数
- String username=request.getParameter("username");
- String password=request.getParameter("password");
- System.out.println(username+"--"+password);
- response.setHeader("Content-Type", "text/html; charset=UTF-8");
- Writer out=response.getWriter();
- out.write("用户名:"+username);
- out.write("密码:"+password);
- out.flush();
- out.close();
- }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); //向服务器发送请求获取到参数 String username=request.getParameter("username"); String password=request.getParameter("password"); System.out.println(username+"--"+password); response.setHeader("Content-Type", "text/html; charset=UTF-8"); Writer out=response.getWriter(); out.write("用户名:"+username); out.write("密码:"+password); out.flush(); out.close(); }效果图:
这就是Post方式提交和doPost方法使用的效果,是不是更安全呢~~~
2.get方式--doGet方法
login.jsp:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>登录</title>
- </head>
- <body>
- <h3>登录</h3>
- <hr>
- <form action="LoginServlet" method="get">
- 用户名:<input type="text" name="username"/><br>
- 密码:<input type="password" name="password"/><br>
- <input type="submit" />
- </form>
- </body>
- </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录</title> </head> <body> <h3>登录</h3> <hr> <form action="LoginServlet" method="get"> 用户名:<input type="text" name="username"/><br> 密码:<input type="password" name="password"/><br> <input type="submit" /> </form> </body> </html>LoginServlet:
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- request.setCharacterEncoding("UTF-8");
- response.setCharacterEncoding("UTF-8");
- //向服务器发送请求获取到参数
- String username=request.getParameter("username");
- String password=request.getParameter("password");
- System.out.println(username+"--"+password);
- response.setHeader("Content-Type", "text/html; charset=UTF-8");
- Writer out=response.getWriter();
- out.write("用户名:"+username);
- out.write("密码:"+password);
- out.flush();
- out.close();
- }
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); //向服务器发送请求获取到参数 String username=request.getParameter("username"); String password=request.getParameter("password"); System.out.println(username+"--"+password); response.setHeader("Content-Type", "text/html; charset=UTF-8"); Writer out=response.getWriter(); out.write("用户名:"+username); out.write("密码:"+password); out.flush(); out.close(); }效果图:
看这个效果图是不是觉得用户名和密码都暴露了呢~~这样很不安全~
3.也可以post方式提交,然后在doGet方式写逻辑代码,不过要在doPost方法中调用doGet方法,同样get方式也是一样的道理~~~
另外,除了doGet和doPost方法之外,还有doPut、doDelete、doTrace、doHead、doOptions,但使用的比较少。