分别运行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方法的简单使用了