0. 개발환경
jdk 1.8
tomcat 7.0
eclipse 2021-06
1. new Dynamic Web Project (Dynamic web module version: 3.0)
2. new Servlet
3. Servlet1.java 코드가 생성됨.
web.xml의 servlet-mapping을 사용하지 않고, @WebServlet이 이용되었다.
package com.exmaple;
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;
@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
public Servlet1() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
3. server 연결
4. server 실행
5. request로 들어오는 raw 데이터 확인 (웹서비스 등의 데이터 확인하는 용도로...)
package com.exmaple;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
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("/Servlet1")
public class Servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
public Servlet1() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
System.out.println("==================== header ====================");
printHeader(request);
System.out.println("==================== parameter ====================");
printParameter(request);
System.out.println("==================== body ====================");
System.out.println(getBody(request));
doGet(request, response);
}
public static void printHeader(HttpServletRequest request) throws IOException {
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
System.out.println("header: " + headerName);
Enumeration<String> headers = request.getHeaders(headerName);
while (headers.hasMoreElements()) {
String headerValue = headers.nextElement();
System.out.println("value: " + headerValue);
}
System.out.println("");
}
}
public static void printParameter(HttpServletRequest request) {
Enumeration<String> names = request.getParameterNames();
while(names.hasMoreElements()) {
String name = names.nextElement();
String value = request.getParameter(name);
System.out.println("name: " + name);
System.out.println("value: " + value);
System.out.println();
}
}
public static String getBody(HttpServletRequest request) throws IOException {
String body = null;
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
try {
InputStream inputStream = request.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
}
} catch (IOException ex) {
throw ex;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
throw ex;
}
}
}
body = stringBuilder.toString();
return body;
}
}
console log 결과
==================== header ====================
header: host
value: localhost:8080
header: content-type
value: text/plain
header: cookie
value: JSESSIONID=6135A7F982712586F8C070ED65DEDEC9
header: content-length
value: 28
==================== parameter ====================
name: name
value: yyy
name: age
value: 12
==================== body ====================
<a>
111
</a>
aaaa...~!~!~
출처:
https://examples.javacodegeeks.com/enterprise-java/servlet/get-all-request-headers-in-servlet/
'Java > Spring' 카테고리의 다른 글
spring3 legacy (0) | 2023.01.30 |
---|---|
spring bean 등록 확인 (0) | 2021.10.29 |
lombok 같은 library를 만들면 (0) | 2021.05.17 |
[deploy] Github Action / S3 / CodeDeploy / EC2 (0) | 2021.05.14 |
java.lang.IllegalAccessException: Class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor can not access a member of class org.example.entity.BaseEntity with modifiers "public" (0) | 2021.05.09 |