doGet与doPost

xiaoxiao2021-02-27  377

Servlet的doGet处理get请求,doPost处理post请求

表单:<form name='"regForm" action = "动作" method ="'提交方式">

表单有两种提交方式:get与post

1.get:以明文的方式通过URL提交数据,数据在URL中可以看到。

提交的数据最大一般不超过2kb.安全性较低,但效率比post方式高。

适合提交数据量不大,安全性不高的数据。比如:搜索、查询等功能。

2.post:将用户信息封装在HTML HEADER内,适合提交数据量大,安全性能高的用户信息。

比如:注册、修改、上传等功能

一帮两者没有太大区别,可以一起用。如:doGet()调用doPost()或者doPost()调用doGet()

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); if(request.getParameter("action")!=null) { this.action = request.getParameter("action"); if(action.equals("add")) //如果是添加商品进购物车 { if(addToCart(request,response)) { request.getRequestDispatcher("/success.jsp").forward(request, response); } else { request.getRequestDispatcher("/failure.jsp").forward(request, response); } } if(action.equals("show"))//如果是显示购物车 { request.getRequestDispatcher("/cart.jsp").forward(request, response); } if(action.equals("delete")) //如果是执行删除购物车中的商品 { if(deleteFromCart(request,response)) { request.getRequestDispatcher("/cart.jsp").forward(request, response); } else { request.getRequestDispatcher("/cart.jsp").forward(request, response); } } } }参考文献:http://blog.csdn.net/luoweifu/article/details/7865243 点击打开链接

转载请注明原文地址: https://www.6miu.com/read-3841.html

最新回复(0)