HTTP请求方式GET和POST代码示例
- 1.HTTP名词解释
- 1.2 HTTP传输方式
- 1.2.1GET名词解释
- 1.2.1POST名词解释
- 1.3Http中Post 和Get代码实现
1.HTTP名词解释
HTTP大名叫做超文本传输协议,字面理解就是传输超文本,web使用一种HTTP的协议作为规范,完成从客户端到服务器端等一系列动作流程。可以说,web是建立在HTTP协议上通信的。
1.2 HTTP传输方式
客户向服务器请求服务时,只需传送请求方法和路径。请求方法常用的有GET、HEAD、POST。
1.2.1GET名词解释
GET用于从指定资源请求数据
GET 请求可被缓存
GET 请求保留在浏览器历史记录中
GET 请求可被收藏为书签
GET 请求不应在处理敏感数据时使用
GET 请求有长度限制
GET 请求只应当用于取回数据(不修改)
1.2.1POST名词解释
POST 用于将数据发送到服务器来创建/更新资源
POST 请求不会被缓存
POST 请求不会保留在浏览器历史记录中
POST 不能被收藏为书签
POST 请求对数据长度没有要求
1.3Http中Post 和Get代码实现
程序视图:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210705150756979.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzE4MzYxMQ==,size_16,color_FFFFFF,t_70)
业务类:HttpTest
import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebService(name = "HttpTest")
public class HttpTest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ PrintWriter writer = response.getWriter();
writer.println("this is POST"+request.getParameter("test2")); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ PrintWriter writer = response.getWriter();
writer.println("this is GET"+request.getParameter("test1")); }
}
web.xml(服务注册)
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet> <servlet-name>HttpTest</servlet-name>
<servlet-class>HttpTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HttpTest</servlet-name>
<url-pattern>/HttpTest</url-pattern>
</servlet-mapping>
</web-app>
前端代码 index.jsp
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210705151226246.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzE4MzYxMQ==,size_16,color_FFFFFF,t_70)
<html>
<body>
<h2>Hello World!</h2>
<form action="HttpTest" method="get">
<input type="text" name="test1">
<input type="submit" name="get method"></form>
<form action="HttpTest" method="post">
<input type="text" name="test2">
<input type="submit" name="post method"> </form>
</body>
</html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)