复习之Http与get()、post()解析

点击访问我的网站,查看效果 上海驾校

直观区别

相同方法代码

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方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值