apache and tomcat, sample form servlet application hyunsuk jung 2002.11.11

17
Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung HyunSuk Jung 2002.11.11 2002.11.11

Upload: paul-brooks

Post on 18-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

Apache and Tomcat, Sample Form Servlet Application

HyunSuk JungHyunSuk Jung

2002.11.112002.11.11

Page 2: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

2Data Warehousing

Lab.DW

Apache 소개

가장 인기있는 가장 인기있는 internet web serverinternet web server 지속적 패치 파일 제공 최고의 퍼포먼스 무료 제공

Page 3: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

3Data Warehousing

Lab.DW

Apache 기본 설정 (httpd.conf)

지시어지시어 설명설명

portport 웹 서버의 기본포트는 웹 서버의 기본포트는 80 80 번으로번으로 , , 이미 또 다른 프로세스에 의해 이미 또 다른 프로세스에 의해 80 80 번 포트가 점유되어 번 포트가 점유되어 있지는 않은가를 살펴본다있지는 않은가를 살펴본다 . 80 . 80 번 포트가 사용되어 지고 있다면번 포트가 사용되어 지고 있다면 , 1024 , 1024 번 이상의 포트번호를 번 이상의 포트번호를 설정하여 재 시도 해본다설정하여 재 시도 해본다 ..

GroupGroup #-1 #-1 과 같이 지정되어 있으나과 같이 지정되어 있으나 , , 이것을 시스템에서 인식하지 못한다면 이것을 시스템에서 인식하지 못한다면 nobody nobody 와 같은 와 같은 적절한 그룹을 지정한다적절한 그룹을 지정한다 ..

ServerNameServerName 주석이 되어 있는 경우주석이 되어 있는 경우 , , 시스템에서 사용하는 시스템에서 사용하는 www.apache.kr.net www.apache.kr.net 와 같은 와 같은 FQDN FQDN 형식으로 입력하여 주며형식으로 입력하여 주며 , , 도메인이 없는 경우 시스템의 도메인이 없는 경우 시스템의 IP IP 주소를 입력하거나 또는 주소를 입력하거나 또는 localhost localhost 로 입력한다로 입력한다 ..

ServerRootServerRoot 아파치 웹 서버가 위치하는 경로가 적절히 지정되어 있는지 확인한다아파치 웹 서버가 위치하는 경로가 적절히 지정되어 있는지 확인한다 . . 이 경로는 아파치 이 경로는 아파치 생 성 시 에 생 성 시 에 ----prefix prefix 옵 션 에 지 정 된 경 로 이 며옵 션 에 지 정 된 경 로 이 며 , , 지 정 하 지 않 을 경 우 에 는 지 정 하 지 않 을 경 우 에 는 ‘‘ // usr/local/apache’ usr/local/apache’ 의 기본 경로를 가지게 된다의 기본 경로를 가지게 된다 ..

DocumentRootDocumentRoot 아파치 웹 서버의 기본이 되는 문서 디렉토리로아파치 웹 서버의 기본이 되는 문서 디렉토리로 , , 클라이언트가 요청시에 제일 먼저 클라이언트가 요청시에 제일 먼저 DocumentRoot DocumentRoot 에 의해 지정된 디렉토리의 문서 내용을 보여주게 된다에 의해 지정된 디렉토리의 문서 내용을 보여주게 된다 ..

Page 4: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

4Data Warehousing

Lab.DW

Tomcat 소개

아파치 소프트웨어에서 개발되고 있는 아파치 소프트웨어에서 개발되고 있는 Java ServletJava Servlet 과 과 JSP(Java Server JSP(Java Server Page)Page) 등의 자바 기술을 이용할 수 있는 등의 자바 기술을 이용할 수 있는 Servlet ContainerServlet Container 이다이다 . .

Tomcat Tomcat 실행시실행시

Page 5: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

5Data Warehousing

Lab.DW

Apache 와 Tomcat 을 함께 쓰는 이유

1.1. Tomcat is not as fast as Apache when it comes to static Tomcat is not as fast as Apache when it comes to static pages.pages.

2.2. Tomcat is not as configurable as Apache.Tomcat is not as configurable as Apache.3.3. Tomcat is not as robust as Apache.Tomcat is not as robust as Apache.4.4. Tomcat may not address many sites' need for functionality Tomcat may not address many sites' need for functionality

found only in Apache modules (e.g. Perl, PHP, etc.). found only in Apache modules (e.g. Perl, PHP, etc.).

Page 6: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

6Data Warehousing

Lab.DW

나의 실행 환경

Jdk1.3+Apache2.0.4+Tomcat4.0.4+cocoon2.0.3 Jdk1.3+Apache2.0.4+Tomcat4.0.4+cocoon2.0.3 의 환경에서 의 환경에서 연동하였다연동하였다 ..

<<cocooncocoon 화면화면 >>

Page 7: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

7Data Warehousing

Lab.DW

Page 8: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

8Data Warehousing

Lab.DW

HttpServletRequest, HttpServletResponse 의 개요

HttpServletRequestHttpServletRequest• 클라이언트의 모든 요청 정보 보유• request header

•  form data, query parameter

•  InputStream( 클라이언트로부터 전송되어지는 데이터 )• 기타 클라이언트 정보얻기 세션 정보 , 쿠키 , path ... 

HttpServletResponseHttpServletResponse•   클라이언트에게 보내지는 모든 정보 보유•   response header

•   OutputStream ( 클라이언트에게 보내지는 데이터 )•    쿠키 설정•    세션 설정

Page 9: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

9Data Warehousing

Lab.DW

HttpServletRequest – Request 메시지를 보내어 페이지 받기

Page 10: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

10Data Warehousing

Lab.DW

RequestMessage.javaRequestMessage.java

import java.io.*; import java.io.*;

import javax.servlet.*; import javax.servlet.*;

import javax.servlet.http.*; import javax.servlet.http.*;

import java.net.*; import java.net.*;

public class RequestMessage extends HttpServlet { public class RequestMessage extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException { throws ServletException, IOException {

OutputStream out = response.getOutputStream(); OutputStream out = response.getOutputStream();

InetAddress webServer = InetAddress.getByName("kr.yahoo.com"); InetAddress webServer = InetAddress.getByName("kr.yahoo.com");

Socket httpPipe = new Socket(webServer, 80);Socket httpPipe = new Socket(webServer, 80); // // 웹서버포트웹서버포트 :80 :80 InputStream is = httpPipe.getInputStream(); InputStream is = httpPipe.getInputStream();

PrintStream os = new PrintStream(httpPipe.getOutputStream()); PrintStream os = new PrintStream(httpPipe.getOutputStream());

os.println("GET " + "/index.html" + " HTTP/1.0\n");// send GET HTTP request os.println("GET " + "/index.html" + " HTTP/1.0\n");// send GET HTTP request

int temp; int temp;

ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();

while ( (temp = is.read())!=-1) { while ( (temp = is.read())!=-1) {

baos.write(temp); baos.write(temp);

} }

out.write(baos.toByteArray()); out.write(baos.toByteArray());

os.close(); os.close();

is.close(); is.close();

out.close(); out.close();

} }

}}

Page 11: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

11Data Warehousing

Lab.DW

HttpServletResponse – 다른 페이지 보내기

Page 12: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

12Data Warehousing

Lab.DW

ResponseRedirect.javaResponseRedirect.java

import java.io.*; import java.io.*;

import java.util.*; import java.util.*;

import javax.servlet.*; import javax.servlet.*;

import javax.servlet.http.*; import javax.servlet.http.*;

public class ResponseRedirect extends HttpServlet { public class ResponseRedirect extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException { throws ServletException, IOException {

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

PrintWriter out = response.getWriter(); PrintWriter out = response.getWriter();

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

if(gopage!=null && gopage.equals("yahoo")){ if(gopage!=null && gopage.equals("yahoo")){

response.sendRedirect("http://www.yahoo.co.kr"); response.sendRedirect("http://www.yahoo.co.kr");

}else { }else {

out.println("<html><body>"); out.println("<html><body>");

out.println("<h1>Responseout.println("<h1>Response 의 의 sendRedirect</h1>"); sendRedirect</h1>");

out.println("out.println(" 다른페이지로 이동하기 위해서는다른페이지로 이동하기 위해서는 <<br>"); br>");

out.println("URLout.println("URL 에 에 ??gopage=yahoogopage=yahoo 를 붙이시오를 붙이시오 "); ");

out.println("</body></html>"); out.println("</body></html>");

} }

out.close(); out.close();

} }

}}

Page 13: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

13Data Warehousing

Lab.DW

HttpServletResponse–setHeader

Page 14: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

14Data Warehousing

Lab.DW

SetHeaderTest.javaSetHeaderTest.java

import java.io.*; import java.io.*;

import javax.servlet.*; import javax.servlet.*;

import javax.servlet.http.*; import javax.servlet.http.*;

public class SetHeaderTest extends HttpServlet{ public class SetHeaderTest extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response) public void doGet(HttpServletRequest request, HttpServletResponse response)

throws IOException, ServletException{ throws IOException, ServletException{

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

PrintWriter out = response.getWriter(); PrintWriter out = response.getWriter();

response.response.setHeader("Refresh", "URL=http://www.yahoo.co.kr"); setHeader("Refresh", "URL=http://www.yahoo.co.kr");

out.println("<HTML><BODY>"); out.println("<HTML><BODY>");

out.println("<H3> out.println("<H3> 잠시만 기다려 주세요잠시만 기다려 주세요 ... </... </H3>"); H3>");

out.println("<H2> 5out.println("<H2> 5 초후에 초후에 yahoo yahoo 홈페이지로 이동합니다홈페이지로 이동합니다 .</.</H2>"); H2>");

out.println("</BODY></HTML>"); out.println("</BODY></HTML>");

} }

}}

Page 15: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

15Data Warehousing

Lab.DW

Page 16: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

16Data Warehousing

Lab.DW

Sample Form Servlet Application

Form1.html<html><title> testing JAVA Servlet Second</title><body bgcolor=white><br><BR><hr><form action=http://localhost:8000/examples/servlet/form1 method=get><br>메세지를 입력 하세요 : <input type=text name=msg><br><input type=submit value= 전송 ><br></form><hr></body></html>

Page 17: Apache and Tomcat, Sample Form Servlet Application HyunSuk Jung 2002.11.11

17Data Warehousing

Lab.DW

Form1.javaimport javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*; public class form1 extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String server, protocol, querystring, msg, date; int port, leng; res.setContentType("text/html"); PrintWriter toClient = new PrintWriter(res.getOutputStream(),true); server = req.getServerName(); port = req.getServerPort(); protocol= req.getProtocol(); msg = req.getParameter("msg"); //form tag 에서 msg 라고 이름을 정한 field 값을 받아옴 . querystring = req.getQueryString(); Date today = new Date(); date = today.toString(); toClient.println("<html><title> test servlets GET </title>"); toClient.println("<body bgcolor=white>"); toClient.println("<br><hr><h1> From Servlet </h1><hr><br><BR>"); toClient.println("<br>Your Server name : "+server+"<br>"); toClient.println("<br>Your Port number: "+port+"<br>"); toClient.println("<br>Your Protocol : "+protocol+"<br>"); toClient.println("<br>Message from Client : "+msg+"<br>"); toClient.println("<br>Your Query String : "+querystring+"<br>"); toClient.println("<BR><BR><BR>"); toClient.println("</body></html>"); }}