点击访问我的网站,查看效果 上海驾校
直观区别
相同方法代码:
package com.study.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWordServlet extends HttpServlet {
@SuppressWarnings("deprecation")
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("doget");
process(req, resp);
}
@SuppressWarnings("deprecation")
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("dopost");
process(req, resp);
}
private void process(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/html");
String username = req.getParameter("username");
String password = req.getParameter("password");
PrintWriter out = resp.getWriter();
out.write("<html><head><title>helloworld</title></head>");
out.write("<body><h>hello world " + new Date().toLocaleString()
+ "<br>" + username + "<br>" + password + "<br>"
+ "</h></body></html>");
System.out.println(new Date().toGMTString());
out.flush();
}
}
当用户在处理上述代码处理一个表单请求时,如果用户使用Get()方法进行请求,我们会发现在浏览器输入栏上会显示用户名和密码,但是用post请求就不会有该现象
解析:
post
POST方法用于向服务器发送请求,要求服务器接受附在请求后面的数据。POST方法在表单提交的时候用的最多
采用POST方法提交表单的例子
POST /login.jsp HTTP/1.1 (CRLF)
Accept:image/gif (CRLF) (….)
Host:www.sample.com (CRLF)(….)
….
Cache-Control:no=cache (CRLF)
(CRLF)
username=hello&password=123456
也就是说post方法提交的是:
POST /login.jsp HTTP/1.1 (CRLF)
Accept:image/gif (CRLF) (….)
Host:localhost (CRLF)(….)
….
Cache-Control:no=cache (CRLF)
(CRLF)
username=hello&password=123456
GET方法用于获取由Request-URI所标识 的资源的信息,常见形式是:
GET Request-URI HTTP/1.1
也就是说get方法提交的是:
GET /login.jsp?username=hello&password=123456 HTTP/1.1 (CRLF) (CRLF)
所以说当你需要文件上传的时候,需要使用post方法