๐ป
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Servlet(์๋ธ๋ฆฟ) (2) ๋ณธ๋ฌธ
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 : ํ์ฌ ์๋ธ๋ฆฟ์ ํ ํจํค์ง๋ช
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>
'KITRI > JSP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JSP ํ๋ก๊ทธ๋๋ฐ - JSTL(JSP Standard Tag Library) ๋ค์ด๋ก๋ / c:set, c:remove (0) | 2020.06.10 |
---|---|
JSP ํ๋ก๊ทธ๋๋ฐ - EL, JSTL (0) | 2020.06.10 |
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Cookie(์ฟ ํค)/ Session(์ธ์ ) (0) | 2020.06.09 |
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Servlet ๊ฐ์ฒด ๋ฒ์ (0) | 2020.06.09 |
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Servlet(์๋ธ๋ฆฟ) (1) (0) | 2020.06.05 |