servlet04-调用doGet和doPost的顺序

1.创建一个名为Test1的servlet,web.xml中的url-pattern是Test1;
在doGet和doPost方法中添加测试代码;

package com.test.servlet;

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

/**
 * Servlet implementation class Test1
 */
public class Test1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Test1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("访问的是doGet方法");
		//该方法会在浏览器中显示提交的内容,比如name="文本框中的值";
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("访问的是doPost方法");
		//该方法不会在浏览器中显示提交的内容;
	}

}

2.创建一个名为test的html文件,文件中有一个表单,表单中有一个文本框和一个按钮,通过点击按钮,提交内容到servlet;

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
    var submitButton = function(){
    	var form = document.getElementById("form_01");
    	
    	form.submit();
    }
</script>
</head>
<body>
<form action="http://localhost:8080/webDemo/Test1" method="get" id="form_01">
<!-- action中的路径是进行操作的servlet路径,method指定执行哪个方法 -->
    <input type="text" name="subContent">
    <!-- subContent中就是显示在浏览器地址栏的内容>
   <!-- 格式为subContent=输入的内容 -->
   <!-- 调用post方法时不显示这些内容 -->
    <input type="button" value="提交" onclick="submitButton();">
</form>
</body>
</html>

当使用浏览器直接访问servlet时,浏览器默认调用doGet方法;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值