HTTP 方法:GET 与POST

在客户机和服务器之间进行请求-响应时,两种最常被用到的方法是:GET 和 POST。
(1)GET - 从指定的资源请求数据。
(2)POST - 向指定的资源提交要被处理的数据

新建一个web项目:
(1)TestServlet.java


```java
package sxx;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("DoGet");
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("DoPost");
	}
	@Override
	public void destroy() {
		System.out.println("destroy");
	}

}

(2)web.xml

```java
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>T1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 <servlet>
  	<servlet-name>t1</servlet-name>
  	<servlet-class>sxx.TestServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>t1</servlet-name>
  	<url-pattern>/t1.do</url-pattern>
  </servlet-mapping>
</web-app>

(3)index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <a href="t1.do">我是超链接</a><br>
    <form action="t1.do" method="get">
    用户名: <input type="text" name="user">
    <input type="submit" value="get提交">
    </form>
     <form action="t1.do" method="post">
     用户名: <input type="text" name="user">
    <input type="submit" value="post提交">
    </form>
    
  </body>
</html>

依次点击超链接、get、post后,控制台输出:DoGet DoGet DoPost
且get提交后网址为:http://127.0.0.1:8081/T1/t1.do?user=zs,而post为:http://127.0.0.1:8081/T1/t1.do
可以看出:
(1)超链接等价于URL直接访问,以get方式
(2)表单可以使用POST也可以使用GET方式

post与get的区别

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值