자바로 만든 서버가 있고 post형식으로 요청전문을 보낼경우
이때 전송된 body를 출력하는 것을 구현하려는데 뒤에 body가 붙어서 오는것인지 아닌 것인지 조차 모르겠습니다. ㅠㅠ
try {
in = clientSocket.getInputStream(); // 클라이언트로 부터 바이트 단위로 입력을 받는 InputStream을 얻어와 개통합니다.
reader = new BufferedReader(new InputStreamReader(in)); // 클라에서 넘어온 형식을 저장
String eachLine = reader.readLine();
request.put("method", URLDecoder.decode(eachLine.substring(0, eachLine.indexOf(" /")), "UTF-8"));
request.put("requestUrl", eachLine.substring(eachLine.indexOf("/"), eachLine.lastIndexOf("HTTP/")));
request.put("httpVersion", eachLine.substring(eachLine.lastIndexOf("/") + 1, eachLine.length()));
while (eachLine != null && !eachLine.equals("")) // HttpHeader 다읽고 탈출
{
System.out.println(eachLine);
eachLine = reader.readLine();
if (!eachLine.equals("")) { // 읽어들이고 나서 해야함 안그러면 빈공간을 읽고 편집할라니까 오류가 발생함
String name = eachLine.substring(0, eachLine.indexOf(":"));
String data = eachLine.substring(eachLine.indexOf(":"), eachLine.length());
request.put(name, data);
}
}
System.out.println("request.get(\"method\") = "+request.get("method"));
if(request.get("method").equals("POST")) // POST라면
{
try {
System.out.println("reader.readLine(); ========"+reader.readLine());
eachLine = reader.readLine(); // 바로 전에서 eachLine이 null이거나 eachLine이 ""으로 루프를 빠져나왔으니
// 한번 더읽어 주어 내용이 더 있나 검사
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//System.out.println("●●●●RequestHandler의 IOException = "+e);
}
while (eachLine != null && !eachLine.equals("")) // HttpHeader 뒤에 오는게 body니까 끝까지 읽어서 출력
{
System.out.println(eachLine);
eachLine = reader.readLine();
}
}
}
post일 경우 앞서 헤더 부분을 출력할때 null이나 ""일 경우 루프를 빠져나왔으니 한번 더 읽고 그다음에 body를 읽어야한다고 생각하여 위와 같이 짜보았습니다.
<HTML>
<HEAD>
<TITLE>POST방식</TITLE>
</HEAD>
<BODY>
<form action="/servlet/PostServlet" method="post">
<div>
<label for="name">Name:</label>
<input type="text" id="name" />
</div>
<div>
<label for="msg">Message:</label>
<textarea id="msg"></textarea>
</div>
<div class="button">
<button type="submit">Send your message</button>
</div>
</form>
</BODY>
</HTML>
클라에서 실행하는 html 은 위 코드처럼 간략하게 버튼을 누르면 name과 msg가 전송 되는 것을 목적으로 하였습니다. 이때 실행되는 PostServlet에서는 따로 동작하는것이 아무것도 없습니다.
실행을하면 get방식은 잘 출력하지만 post방식은 method도 post로 잘 들어오는데 뒤에 body를 출력하기전 readLine에서 동장이 멈추게됩니다.
이미 다 읽어들여서 읽어 들일게 없으니까 동작이 멈춘거같기는 한데... 그럼 method는 잘들어왔는데 body가 안들어왔단 것이니... 왜 안들어왔는지... ㅠㅠ
조언 좀 던져주시면 감사하겠습니다 ㅠ.ㅠ