Intellij JSP - Servlet doGet(), doPost()
less than 1 minute read
Servlet doGet(), doPost()
-
Project - ServletTest
-
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="/Serv" method="post">
<input type="submit" value="DO POST">
</form>
<form action="/Serv" method="get">
<input type="submit" value="DO GET">
</form>
</body>
</html>
-
Servlet.java
@WebServlet(value = "/Serv")
public class Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("DD POST");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("DD GET");
}
}
-
doGet()
-
URL 매핑을 이용
-
URL 매핑은 은 기본적으로 get 방식이므로 매핑된 URL로 접근하게 되면 doGet() 메소드가 호출된다
-
doPost()
-
URL은 같지만 form 태그의 method=”post” 이었기 때문에 doPost()가 호출된다
-
Leave a comment