(5)一针见血,servlet中的service,doGet ,doPost方法

分别运行servlet3.Java和login1.html,出现下面的页面,

只有画圈圈那里不同,说明他俩是在同一个文件夹里面的

自己验证:

servlet3代码

package cn.xdl.demo;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Servlet4
 */
@WebServlet("/s5")
public class Servlet3  extends HttpServlet {
    
        
    
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("get request ");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                response.getWriter().append("post request ");
    }

}

Login1.html代码

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
        <form action=" " >
     
   
          <input type="submit">
      
</body>
</html>

当我把提交的地址改为s5,那么他就会根据<input>标签触发相应的doget方法或者dopost方法

login1.html的action地址改为s5之后,重新运行login1.html页面就如页面点击查询,就会直接跳转的servlet3页面中去,执行doget方法

Servlet.java代码没变

改的是login.html代码

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

     <form action="s5">         //默认为get请求

          <input type="submit">

   

     </form>

</body>

</html>

同理,如果我修改input标签 为post请求,就会跳转到会直接跳转的servlet4页面中去,执行dopost方法

  Login.html代码

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

     <form action="s5" method="post">  //post请求

          <input type="submit">

   

     </form>

</body>

</html>

注意:重新写了java代码之后建议重新启动项目,或者等待tomcat部署完成,如果改的是webContent中的静态文件直接刷新网页就行,不用等待tomcatb部署

这样子我们就可以大概了解doGet,doPost方法的简单使用了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值