까먹지 말자! 자주 보자!
doGet Servlet과 doPost Servlet (get방식, post방식) 본문
HttpGetServlet.java
---------------------------------------------------------------------------------------------------------------------
package doo;
import java.io.IOException;
import java.io.PrintWriter;
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 serveletTest
*/
@WebServlet("/HttpGetServlet")
public class HttpGetServlet extends HttpServlet {
/*
* 서블릿으로 전달된 파라미터 정보를 얻기 위해서는 HttpSErvletRequest 객체의 getParameter 메서드를 이용한다.
* getParamert() 메서드는 서블릿으로 전달된 파라미터의 이름을 인자로 갖는다.
*/
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
/* servlet으로 전달된 name 파라미터의 값을 얻어 helloName변수에 저장한다. */
String helloName = req.getParameter("name");
out.println("<html>" + "<head>" + "<title>doGet Servlet</title>"
+ "</head>" + "<body>");
out.println("<h1>doGet Method</h1>");
if (!helloName.equals("")) {
out.println("<h2>" + helloName + "</h2>");
}
out.println("</body></html>");
}
}
---------------------------------------------------------------------------------------------------------------------
HttpPostServlet.java
---------------------------------------------------------------------------------------------------------------------
package doo;
import java.io.IOException;
import java.io.PrintWriter;
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 HttpPostServlet
*/
@WebServlet("/HttpPostServlet")
public class HttpPostServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
String helloName = req.getParameter("name");
out.println("<h1>doPost Metod</h1>");
out.println("<h2>" + helloName + "</h2>");
}
}
---------------------------------------------------------------------------------------------------------------------
ExcutePostServlet.html
---------------------------------------------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>서블릿의 doPost</title>
</head>
<body>
<h1>POST 방식 전송</h1>
<form action ="http://localhost:8080/testjsp/HttpPostServlet" method = "POST" >
<input type = "text" name = "name">
<input type = "submit" value = "submit">
</form>
</body>
</html>
---------------------------------------------------------------------------------------------------------------------
'JSP' 카테고리의 다른 글
JSP BOARDER(1)_게시판 관리를 위한 컴포넌트(AdminManager.java) (0) | 2016.05.24 |
---|---|
request와 response의 동작과정 (0) | 2016.05.18 |
서블릿(Servler) 동작원리 (0) | 2016.05.18 |
웹의 원리 (0) | 2016.05.17 |
서블릿(Servlet), 서블릿 컨테이너, jsp (0) | 2016.05.17 |