[eclipse] dynamic web project 생성하는 방법.
2022. 2. 15. 10:20ㆍTool
728x90
728x90
1.dynamic web project 생성.
2.생성된 프로젝트 구조 확인.
3.http post body data를 송/수신하는 servlet 코드 생성.(에코 서버)
3.1.http_post_servlet.httpPostServlet.java 생성.
package http_post_servlet;
import java.io.*;
import java.io.PrintWriter;
import java.net.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Httpserver
*/
public class httpPostServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public httpPostServlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doPost() is Running...");
InputStream in = null;
BufferedReader reader = null;
HttpURLConnection httpURLConnection = null;
String recvMsg = null;
String sendMsg = null;
try {
in = request.getInputStream();
reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
System.out.println("read is Running...");
StringBuffer fileData = new StringBuffer(1024);
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
fileData.append(buf, 0, numRead);
System.out.println("append is Running...");
}
in.close();
reader.close();
recvMsg = fileData.toString();
System.out.println("recvMsg : " + recvMsg);
sendMsg = recvMsg;
} catch (Exception e) {
} finally {
if (in != null)
in.close();
if (reader != null)
reader.close();
}
try {
response.setContentType("text/html);charset=euc-kr");
PrintWriter out = response.getWriter();
out.println(sendMsg);
System.out.println("sendMsg : " + sendMsg);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
3.2.생성된 "http_post_servlet.httpPostServlet.java" 구조 확인.
4."\eclipse-workspce\servlet\WebContent\WEB-INF\web.xml" 설정.
4.1."\eclipse-workspce\servlet\WebContent\WEB-INF\web.xml" 경로 확인.
4.2."\eclipse-workspce\servlet\WebContent\WEB-INF\web.xml" 설정 확인.
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="servlet-2_5" version="2.5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>servlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>httpPostServlet</servlet-name>
<servlet-class>http_post_servlet.httpPostServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>httpPostServlet</servlet-name>
<url-pattern>/httpPostServlet</url-pattern>
</servlet-mapping>
</web-app>
5.프로젝트 *.war 파일로 Export
728x90
728x90