๐Ÿ’ป

JSP ํ”„๋กœ๊ทธ๋ž˜๋ฐ - [Servlet] Servlet(์„œ๋ธ”๋ฆฟ) (2) ๋ณธ๋ฌธ

KITRI/JSP

JSP ํ”„๋กœ๊ทธ๋ž˜๋ฐ - [Servlet] Servlet(์„œ๋ธ”๋ฆฟ) (2)

๋˜ํšจ๋‹ˆ 2020. 6. 8. 15:07

Q. URL vs URI ์˜ ์ฐจ์ด?

URL์€ ํ”„๋กœํ† ์ฝœ(http,https)๋ถ€ํ„ฐ servlet ํŒŒ์ผ๊นŒ์ง€์˜ ๊ฒฝ๋กœ๋ฅผ ๋งํ•œ๋‹ค.
(์˜ˆ: http://localhost:8181/webTesting/com/java/servlet/Example06)

URI๋Š” ํ•ด๋‹น context(ํ”„๋กœ์ ํŠธ๋ช…)๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ servletํŒŒ์ผ๊นŒ์ง€์˜ ๊ฒฝ๋กœ๋ฅผ ๋งํ•œ๋‹ค.
(์˜ˆ: /webTesting/com/java/servlet/Example06)

 

 

URL, URI, contextPath, servletPath 4๊ฐ€์ง€ ๊ผญ ๊ธฐ์–ตํ•˜๊ธฐ!!

 

URL : ํ”„๋กœํ† ์ฝœ๋ถ€ํ„ฐ ์„œ๋ธ”๋ฆฟ ํŒŒ์ผ๊นŒ์ง€ ๊ฒฝ๋กœ

URI :  ์ปจํ…์ŠคํŠธ(ํ”„๋กœ์ ํŠธ๋ช…)๋ถ€ํ„ฐ ์„œ๋ธ”๋ฆฟ ํŒŒ์ผ๊นŒ์ง€์˜ ๊ฒฝ๋ฃจ

contextPath :  ํ”„๋กœ์ ํŠธ ๊ฒฝ๋กœ

servletPath : ํ˜„์žฌ ์„œ๋ธ”๋ฆฟ์˜ ํ’€ ํŒจํ‚ค์ง€๋ช…

 

Example06.java

package com.java.servlet;

import java.io.IOException;
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;

/**
 * Servlet implementation class Example06
 */
public class Example06 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Example06() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/* request ๊ฐ์ฒด ์—ญํ•  - ์‚ฌ์šฉ์ž ์š”์ฒญ
		 * ํ•œ๊ธ€ Encoding ๊ธฐ๋Šฅ
		 * ํŒŒ๋ผ๋ฏธํ„ฐ ์ฝ๊ธฐ ๊ธฐ๋Šฅ
		 * ์„œ๋ฒ„์™€ ๊ด€๋ จ๋œ ์ •๋ณด ์ฝ๊ธฐ ๊ธฐ๋Šฅ
		 * ์›น๋ธŒ๋ผ์šฐ์ € ๊ด€๋ จ ์ •๋ณด ์ฝ๊ธฐ ๊ธฐ๋Šฅ
		 * ํ—ค๋” ์ฝ๊ธฐ ๊ธฐ๋Šฅ
		 * ์ฟ ํ‚ค ์ฝ๊ธฐ ๊ธฐ๋Šฅ
		 * ์†์„ฑ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ
		 * response ๊ฐ์ฒด 
		 * */
		
		//ํ•œ๊ธ€์„ค์ •
		request.setCharacterEncoding("utf-8");
		
		//ํŒŒ๋ผ๋ฏธํ„ฐ ์ฝ๊ธฐ
		int su = Integer.parseInt(request.getParameter("su"));
		System.out.println(su);
		
		//์„œ๋ฒ„์™€ ๊ด€๋ จ๋œ ์ •๋ณด ์ฝ๊ธฐ ๊ธฐ๋Šฅ
		String serverName = request.getServerName();
		int serverPort = request.getServerPort();
		
		// Uniform Resource Location 
		// :ํ”„๋กœํ† ์ฝœ(http,https)๋ถ€ํ„ฐ servlet ํŒŒ์ผ๊นŒ์ง€์˜ ๊ฒฝ๋กœ๋ฅผ ๋งํ•œ๋‹ค.
		// http://localhost:8181/webTesting/com/java/servlet/Example06
		StringBuffer URL = request.getRequestURL();	
		
		// Uniform Resource Identifier
		// :ํ•ด๋‹น context(ํ”„๋กœ์ ํŠธ๋ช…)๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ servletํŒŒ์ผ๊นŒ์ง€์˜ ๊ฒฝ๋กœ๋ฅผ ๋งํ•œ๋‹ค.
		// /webTesting/com/java/servlet/Example06
		String URI = request.getRequestURI();  
		
		// /webTesting
		String contextPath = request.getContextPath();
		// /com/java/servlet/Example06
		String servletPath = request.getServletPath();
		
		
		System.out.println("์„œ๋ฒ„์ด๋ฆ„:" + serverName);
		System.out.println("์„œ๋ฒ„ํฌํŠธ:" + serverPort);
		
		System.out.println("URL" + URL);
		System.out.println("URI" + URI);
		
		System.out.println("์ปจํ…์ŠคํŠธ(ํ”„๋กœ์ ํŠธ๋ช…) ๊ฒฝ๋กœ:" + contextPath);
		System.out.println("์„œ๋ธ”๋ฆฟ ๊ฒฝ๋กœ:" + servletPath);
		System.out.println("์š”์ฒญ๋ฐฉ์‹:" + request.getMethod());
		
		// ํด๋ผ์ด์–ธํŠธ ์ •๋ณด ๋ฐ ์›น๋ธŒ๋ผ์šฐ์ € ๊ด€๋ จ ์ •๋ณด ์ฝ๊ธฐ ๊ธฐ๋Šฅ
		System.out.println("์š”์ฒญํ”„๋กœํ† ๋ชฐ:" +request.getProtocol());
		System.out.println("ํด๋ผ์ด์–ธํŠธ ์ฃผ์†Œ:" + request.getRemoteAddr());
		System.out.println("ํด๋ผ์ด์–ธํŠธ ํฌํŠธ:" +request.getRemotePort());
		
		// ํ—ค๋” ์ฝ๊ธฐ ๊ธฐ๋Šฅ
		Enumeration<String> header = request.getHeaderNames();
		while(header.hasMoreElements()) {
			String key = header.nextElement();
			String value = request.getHeader(key);
			System.out.println(key + ":\t" + value);
		}
		// ์ฟ ํ‚ค ์ฝ๊ธฐ ๊ธฐ๋Šฅ - ์ฟ ํ‚ค ์ €์žฅ Response
		// ์†์„ฑ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

 

 

 

 

 

 

 

 


/* response ๊ฐ์ฒด
 * ๋ฌธ์„œํƒ€์ž…, ํ•œ๊ธ€ : setContextType ์„ค์ •
 * ์ถœ๋ ฅ : getWriter()
 * ์ฟ ํ‚ค ์ €์žฅ(์„ค์ •) ๊ธฐ๋Šฅ, ํ—ค๋” ์„ค์ •(ํŒŒ์ผ์—…๋กœ๋“œ ์‹œ) ๊ธฐ๋Šฅ
 * ํŽ˜์ด์ง€ ์ด๋™: sendRedirect
 * */

 

 

Example07_Sucess.java

package com.java.servlet;

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;

/**
 * Servlet implementation class Example07_Success
 */

public class Example07_Success extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Example07_Success() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("์„ฑ๊ณต");
		
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print("<html>");
		out.print("<head><title>๋กœ๊ทธ์ธ</title></head>");
		out.print("<body>");
		out.print("<h3>๋ฐฉ๋ฌธ์„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.</h3>");
		out.print("</body>");
		out.print("<html>");
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 

 

 

Example07_Fail.java

package com.java.servlet;

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;

/**
 * Servlet implementation class Example07_Fail
 */

public class Example07_Fail extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Example07_Fail() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("์‹คํŒจ");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 

 

 

07_Example.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
   <form action="http://localhost:8181/webTesting/com/java/servlet/Example07" method="post">
 	<label>์•„์ด๋””</label>
	<input type="text" name="id"/>
	<br /><br />
	
	<label>๋น„๋ฐ€๋ฒˆํ˜ธ</label>
	<input type="text" name="pw"/>
	<br /><br />
   
   	<input type="submit" value="์ „์†ก"/>
	<input type="reset" value="์ทจ์†Œ" />
   </form>
</body>
</html>

 

 

 

๋ฐ˜์‘ํ˜•
Comments