Servlet_4th_service、doGet、doPost方法

一、关于doGet和doPost方法
1、首先测试doGet方法。
①写一个TestGetPostServlet.java如下,首先对doGet方法进行测试:
package com.hpe.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestGetPostServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		System.out.println("doGet!!!");
	}
}

②在web.xml中对TestGetPostServlet.java进行配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
		xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
		id="WebApp_ID" version="3.1">
  
  <servlet>
  	<servlet-name>getpost</servlet-name>
  	<servlet-class>com.hpe.servlet.TestGetPostServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>getpost</servlet-name>
  	<url-pattern>/getpost</url-pattern>
  </servlet-mapping>
  
</web-app>

③在WebContent目录下新建一个html文件,这里取名为1.html,内容如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form name="myForm" action="getpost" method="get">
		用户名:<input type="text" name="userName">
		<input type="submit" value="提交">
	</form>
</body>
</html>

④启动服务器,并调出window—show view—other—Internal Web Browser浏览器窗口,打开http://localhost:8888/TestServlet/1.html,并在文本框中输入随意内容,点击提交,如下:



提交之后,注意到控制台打印了一句:doGet!!!,并且url的内容也发生变化:http://localhost:8888/TestServlet/getpost?userName=weqpo



2、首先测试doPost方法。
①在之前测试doGet方法的基础上,在TestGetPostServlet.java中重写doPost方法:
package com.hpe.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestGetPostServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		System.out.println("doGet!!!");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("doPost!!!");
	}
}

②将1.html中表单的提交方式改为"post",如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form name="myForm" action="getpost" method="post">
		用户名:<input type="text" name="userName">
		<input type="submit" value="提交">
	</form>
</body>
</html>

③进入http://localhost:8888/TestServlet/1.html页面或者
刷新1.html的页面。
注意:一定要刷新或者重新进入该页面,否则当前页面依然是按照之前为"get"提交方式来解析的。

④与之前一样,在文本框中输入任意内容,点击提交,控制台打印如下信息:


二、关于service方法
在之前测试doGet和doPost方法的基础上,在TestGetPostServlet.java中重写service方法:
package com.hpe.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestGetPostServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		System.out.println("doGet!!!");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		System.out.println("doPost!!!");
	}

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) 
			throws ServletException, IOException {
//		super.service(arg0, arg1);
		System.out.println("service!!!");
	}
}

②依然采用post的方式来提交,点击提交后显示:


③发现没有打印post,而是打印了service,接下来把重写service时的那一行super.service(arg0, arg1)给取消注释,再次进行提交,控制台打印:



总结:
①在html中通过表单form标签来提交表单数据,action表示要提交到服务器端的哪个文件,
method表示使用什么方式来进行表单数据的提交(get/post,默认为get方式)。
②使用get方式就会调用doGet方法,使用post就会调用doPost方法。

③服务器不会直接调用doGet或doPost方法,而是通过调用service方式来间接调用doGet或doPost方法。






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值