zoukankan      html  css  js  c++  java
  • 什么情况下调用doGet()和doPost()?

    什么情况下调用doGet()和doPost()?
    Jsp页面中的form标签里的method属性为get时调用doGet(),为post时调用doPost()

    测试方法如下:
    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
    <h1>get</h1>
    <!-- 绝对路径路径访问  <form action="http://localhost:8080/untitled/myServlet" method="get">-->
    <!-- 绝对路径访问—省略写法     <form action="/untitled/myServlet" method="get">-->
    <form action="myServlet" method="get"><!--相对路径访问  省略了 ./-->
        <input type="submit">
    </form>
    
    <h1>post</h1>
    <form action="http://localhost:8080/untitled/myServlet" method="post">
        <input type="submit">
    </form>
    
    </body>
    </html>

    MyServlet实现类

    @WebServlet("/myServlet")
    public class MyServlet extends HttpServlet {
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("get方法");
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            System.out.println("set方法");
        }
    
    }

    总结:

    默认情况是调用doGet()方法,JSP页面中的Form表单的method属性设置为post的时候,调用的为doPost()方法;为get的时候,调用deGet()方法。





  • 相关阅读:
    PS
    div 解决高度塌陷
    gradle Error:Cause: unable to find valid certification path to requested target
    HTML
    前端路线图
    css 选择器
    css-day01
    Python图像处理 | 把图像中的白色变成透明
    X-Frame-Options(点击劫持)
    python两张图片显示在一张图上
  • 原文地址:https://www.cnblogs.com/csk001/p/14223415.html
Copyright © 2011-2022 走看看