본문 바로가기
Web/JSP/Servlet/JDBC

GET/POST 방식, GET/POST 예제, GET/POST example

by 나비와꽃기린 2016. 6. 22.
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

2.4 GET / POST 방식

2.4.1 GET 방식

-GET 방식으로 요청이 전송되는 경우

-사용방식: <a href="list.jsp?pageNo=2">[2]</a>

-브라우저 주소 표시줄에 주소를 직접 입력해서 요청을 전송하는 경우

Htmla 태그를 사용해서 링크를 걸어서 전송하는 경우

Html 폼 태그에서 method 속성을 GET로 지정하는 경우

 

2.4.2 POST 방식

사용 방식 : <form name=" " action=" " method="POST">

회원 가입 요청, 게시판 글쓰기 요청, 자료실 업로드 등을 처리할 때 사용하는 방식

 

***실습9

login.html

<!DOCTYPE html>

<html>

<head>

<meta charset="EUC-KR">

<title>Insert title here</title>

</head>

<body>

 

<h1>로그인</h1>

<form action="Login" method="get">

아이디: <input type="text" name="id"/><br/>

비밀번호: <input type="text" name="passwd"><br/>

<input type="submit" value="로그인"/>

</form>

</body>

</html>

 

LoginServlet.java

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;

 

 

@WebServlet("/Login")

public class LoginServlet extends HttpServlet {

             private static final long serialVersionUID = 1L;

 

    public LoginServlet() {

    }

 

             protected void doGet(HttpServletRequest request, HttpServletResponse response)

                                        throws ServletException, IOException {

                           doProcess(request, response);

                          

             }

            

             protected void doProcess(HttpServletRequest request, HttpServletResponse response)

                                        throws ServletException, IOException {

                          

                           request.setCharacterEncoding("euc-kr");

                  //설정하는 것이니 set  //인코딩 먼저! -> 하고 결과값 받아와야

                           String id=request.getParameter("id");

                           String passwd=request.getParameter("passwd");

                           response.setContentType("text/html;charset=euc-kr");

                           PrintWriter out=response.getWriter();

                           out.print("아이디:"+id+"<br/>");

                           out.print("비밀번호:"+passwd+"<br/>");

             }

            

 

             protected void doPost(HttpServletRequest request, HttpServletResponse response)

                                        throws ServletException, IOException {

                           doProcess(request, response);

             }

 

}

 

GET, POST에서의 한글처리

 

 

 

***실습10

memReg.html

<!DOCTYPE html>

<html>

<head>

<meta charset="EUC-KR">

<title>Insert title here</title>

</head>

<body>

<form action="MemReg" method="post">

회원명   : <input type="text" name="name"/><br/>

주소     : <input type="text" name="addr"/><br/>

전화번호 : <input type="text" name="tel"/><br/>

취미     : <input type="text" name="hobby"/><br/>

<input type="submit" value="회원가입"/>

</form>

</body>

</html>

MemRegServlet.java

 

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;

 

 

@WebServlet("/MemReg")

public class MemRegServlet extends HttpServlet {

        private static final long serialVersionUID = 1L;

 

 

        protected void doPost(HttpServletRequest request, HttpServletResponse response)

                         throws ServletException, IOException {

                 //한글인코딩, 요청처리

                 request.setCharacterEncoding("euc-kr");

                

                 String name=request.getParameter("name");

                 String addr=request.getParameter("addr");

                 String tel=request.getParameter("tel");

                 String hobby=request.getParameter("hobby");

                

                 //응답, 응답처리

                 response.setContentType("text/html;charset=euc-kr");

                

                 //출력

                 PrintWriter out=response.getWriter();

                 out.write("이름은"+name+"<br/>");

                 out.write("주소는"+addr+"<br/>");

                 out.write("전화번호는"+tel+"<br/>");

                 out.write("취미는"+name+"입니다.<br/>");

        }

 

}

 

***실습10

do.html

<!DOCTYPE html>

<html>

<head>

<meta charset="EUC-KR">

<title>Insert title here</title>

</head>

<body>

<h1>당신이 좋아하는 강아지를 선택하세요~</h1>

<form action="choiceDog" method="post">

<!-- 배열처럼. -->

<input type="checkbox" name="dog" value="image/pu.jpg"/> 푸들

<input type="checkbox" name="dog" value="image/jin.jpg"/> 진돗개

<input type="checkbox" name="dog" value="image/pung.jpg"/> 풍산개

<input type="checkbox" name="dog" value="image/sap.jpg"/> 삽살개

<input type="submit" value="선택"/>

 

</form>

</body>

</html>

ChoicDogServlet.java

 

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;

 

 

@WebServlet("/choiceDog")

public class ChoiceDogServlet extends HttpServlet {

             private static final long serialVersionUID = 1L;

 

 

             protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                          

                           response.setContentType("text/html;charset=euc-kr");

                           PrintWriter out=response.getWriter();

                           String[] dog=request.getParameterValues("dog");

                          

                           out.write("<body bgcolor='black'>");

                           out.write("<table align='center' bgcolor='yellow'>");

                           out.write("<tr>");  //한줄의

                           for (int i = 0; i < dog.length; i++) {  //배열반복

                                        out.print("<td>");

                                        out.print("<img src='"+dog[i]+"'/>");

                                        out.print("</td>");

                           }

                           out.print("</tr>");

                           out.print("</table></body>");

             }

 

}

 


'Web > JSP/Servlet/JDBC' 카테고리의 다른 글

redirect/forword/include 란 /예제/샘플  (0) 2016.06.22
서블릿에서의 Session/ Session 이란 / Session 개념 / Session 예제  (0) 2016.06.22
JSP란 / JSP 예제 / JSP example  (1) 2016.06.22
JSP란?  (0) 2015.01.30
Servlet이란?  (0) 2015.01.30