[Day2] JSP/Servlet 2 [12/5]
์ฐธ๊ณ ) https://javacpro.tistory.com/43
ใด JSP ๊ด๋ จ ์ค๋ช ๋งํฌ
db ์ฐ๊ฒฐํด์ ํ ์ด๋ธ select ํ๊ทธ ์ ํํ๋ฉด table ํ์์ผ๋ก ๋ฐํํ๋ ์ฝ๋ฉ
<form> action="" ์๋ธ๋ฐ X -> jquery Ajax
1) ์ ํํ ์ต์ ์ selected ์์ฑ์ ์ค์ ํ๋ฉด๋๋ค. ( jquery )
<%
// JSP ๊ธฐ๋ณธ ๊ฐ์ฒด (9๊ฐ์ง) : request ๊ฐ์ฒด : url ๊ฐ์ ธ์ด.
String contextPath = request.getContextPath();
%>
<script>
$("#selDept").change(function() {
// js value, text
// jquery value, text
// ํผ ํ๊ทธ ์์ด ์๋ธ๋ฐ.. BOM-W,L,N,S,H
// "/jspPro"
location.href = "<%= contextPath %>/days02/test01.jsp?deptno=" + $(this).val();
}); // change
</script>
<script>
// 1์กฐ ์ฝ๋ฉ ?deptno=30
// $("#selDept option[value=<%=pdeptno%>]").attr("selected", true);
// $("#selDept").val( "<%=pdeptno%>" );
// EL ?deptno=30
// empty EL ์ฐ์ฐ์
// param EL ๊ธฐ๋ณธ ๊ฐ์ฒด
$("#selDept").val( "${ empty param.deptno ? 10 : param.deptno}" );
</script>
<script>
// [ ์ ์ฒด ์ ํ ๊ตฌํ ]
//$("table thead :checkbox")
$("#ckbAll").change( function (){
$("table tbody tr")
.find("td:first-child :checkbox")
.prop("checked", $(this).prop("checked") );
}); // change
</script>
<script>
// "์ ํํ empno ํ์ธ" ๋ฒํผ์ ํด๋ฆญํ ๋..
$("#checkedEmpno").on("click" , function (){
var empnos = [];
// $("table tbody :checkbox") ๋ฐ๋ณต checked ํ์ธ
$("table tbody :checkbox[name=ckbEmp]:checked").each(function(i, element) {
// console.log( i + " / " + element )
// DOM ํ์
// var empno = $(this).parent().next().html();
// <input type="checkbox" name="ckbEmp" data-empno="7369">
var empno = $(this).data("empno");
// console.log( empno );
empnos.push( empno );
}); // each
// $("#demo").html( "<ul><li>" + empnos.join("</li><li>") + "</li></ul>" ) ;
// ์)
// location.href="<%= contextPath %>/days02/test01_ok.jsp?empno=7499&empno=7500&empno=8763";
location.href="<%= contextPath %>/days02/test01_ok.jsp?empno=" + empnos.join("&empno=");
}); // click
</script>
* ํด๋น ํ์ด์ง์์ ํด๋ฆญ์ผ๋ก ํด๋น ํ์ด์ง ํ๋ผ๋ฏธํฐ ๋ ๋ ค์ฃผ๋ ๊ฒ ๊ฐ๋ฅ!
* ๋ค๋ฅธ ํ์ด์ง๋ก ๊ฐ ๋๊ฒจ์ฃผ๋ ๊ฒ๋ ๊ฐ๋ฅ!
(formํ๊ทธ ์์ด)
์๋ธ๋ฆฟ(Servlet)
1. ์๋ธ๋ฆฟ(Servlet) ์ด๋?
์๋ฐ ์น ๊ฐ๋ฐ ํ์ ์ฌํญ.
- JSP ํ์ค์ด ๋์ค๊ธฐ ์ ์ ์๋ฐ ์น ๊ฐ๋ฐ์ ์ํด์ ๋ง๋ค์ด์ก๋ค.
- ์๋ธ๋ฆฟ์ ์ด์ฉํ๋ฉด
"์๋ฐ ํด๋์ค" -> ์น ๊ฐ๋ฐ.
[2]. ์๋ธ๋ฆฟ ๊ตฌํ ๋ฐฉ๋ฒ(๊ณผ์ )
1) [์๋ธ๋ฆฟ ๊ท์ฝ]์ ๋ฐ๋ผ ์๋ฐ ํด๋์ค ์์ฑ(์ ์ธ) -- ???.java
ใฑ. ์๋ฐ ํด๋์ค์ ์ ๊ทผ ์ง์ ์ : public
ใด. javax.servlet.http.HttpServlet ํด๋์ค ์์
ใท. service(), get(), post() ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ ํด์ผํ๋ค.
์ดํด๋ฆฝ์ค ๊ฐ๋ฐ 2+3 X
2) ???.java ์ ์ปดํ์ผ ํด์ ???.class ์์ฑ ( ํด๋์คํ์ผ )
3) ํด๋์ค ํ์ผ์ /WEB-INF/classes ํด๋์ ์ด๋.
4)
ใฑ. web.xml ํ์ผ์ ์๋ธ๋ฆฟ ํด๋์ค๋ฅผ ์ค์ (๋ฑ๋ก).
--------------------------------------------------------------------------------------------------------
์)
1) ์๋ธ๋ฆฟ ๊ท์ฝ์ ๋ฐ๋ ์๋ฐ ํด๋์ค ์ ์ธ
days02 ํจํค์ง + Now.java ์ถ๊ฐ
NOW
package days02;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Now extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(request, response);
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8" );
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset='UTF-8'>");
out.println("<title>2022.12.05 ์๋ธ๋ฆฟ</title>");
out.println("</head>");
out.println("<body>");
Date now = new Date();
String pattern = "yyyy-MM-dd hh:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String strNow = sdf.format(now);
out.println("> ํ์ฌ ๋ ์ง, ์๊ฐ : " + strNow);
out.println("</body>");
out.println("</html>");
} // service
} // class
** alt shift s : ์ค๋ฒ๋ผ์ด๋ฉ -> do get, do post, service
2) web.xml Now.java ์๋ธ๋ฆฟ ๋ฑ๋ก.
<!-- ์๋ธ๋ฆฟ ๋ฑ๋ก + URL ๋งคํ ์ค์ -->
<servlet>
<description>ํ์ฌ ๋ ์ง,์๊ฐ์ ์๋ตํ๋ ์ฒซ ๋ฒ์งธ ์๋ธ๋ฆฟ</description>
<description>days02 ํจํค์ง ์ ์ธ๋ ์๋ธ๋ฆฟ</description>
<servlet-name>now</servlet-name>
<servlet-class>days02.Now</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>now</servlet-name>
<!-- *** URL ํจํด ๋งคํ ๊ท์น *** -->
<!-- <url-pattern>http://localhost/jspPro/days02/now</url-pattern> -->
<url-pattern>/days02/now</url-pattern>
<url-pattern>/days02/now.htm</url-pattern>
<!-- <url-pattern>/test/now.ss</url-pattern> -->
<!-- ์ฌ๋ฐ๋ฅธ url ํจํด์ด ์๋์์. 1/2/3/4 ํด๋น๋์ง ์๋๋ค. -->
<!-- <url-pattern>/test/*.ss</url-pattern> -->
<!-- <url-pattern>*.ss</url-pattern> -->
<!-- <url-pattern>/foo/bar/*</url-pattern> -->
<!-- <url-pattern>/baz/*</url-pattern> -->
<!-- <url-pattern>/catalog</url-pattern> -->
<!-- <url-pattern>*.bop</url-pattern> -->
</servlet-mapping>
3) ์๋ธ๋ฆฟ ๋งคํ ์ค์
์์ฒญURL -> ์๋ธ๋ฆฟ ํธ์ถ-> ์๋ต
http://localhost/jspPro[/days02/now] ์์ฒญ
http://localhost/jspPro[/days02/now.htm] ์์ฒญ
<a href="http://localhost/jspPro/days02/now">now ์๋ธ๋ฆฟ ํธ์ถ</a><br>
<a href="http://localhost/jspPro/days02/now.htm">now ์๋ธ๋ฆฟ ํธ์ถ</a><br>
<!-- web.xml url-pattern ์ค์ -->
<a href="http://localhost/jspPro/test/now.ss">now ์๋ธ๋ฆฟ ํธ์ถ</a><br>
<a href="http://localhost/jspPro/today.ss">now ์๋ธ๋ฆฟ ํธ์ถ</a><br>
Now.java
response.setContentType("text/html; charset=UTF-8" );
--------------------------------------------------------------------------------------------------------
ใด. ์๋ธ๋ฆฟ 3.0 @WebServlet ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํด์ ๋ฑ๋ก ๊ฐ๋ฅ.
( tomcat8.5 -> jsp2.3 servlet4.0 EL3.0 )
days02.Hello.java ์๋ธ๋ฆฟ ํด๋์ค ์์ฑ.
src-main-java-days02 - servlet ์์ฑ
package days02;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
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 Hello
*/
/*
@WebServlet(
description = "@WebServlet ์ด๋
ธํ
์ด์
์ผ๋ก ๋ ๋ฒ์งธ ์๋ธ๋ฆฟ",
urlPatterns = {
"/hello.htm",
"/hello.ss",
"/hello"
})
*/
public class Hello extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Hello() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8" );
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset='UTF-8'>");
out.println("<title>2022.12.05 ์๋ธ๋ฆฟ</title>");
out.println("</head>");
out.println("<body>");
out.println("> ์ธ์ฌ : Hello~ " );
out.println("</body>");
out.println("</html>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<!-- ์ฃผ์ : -->
<!-- ๊ธฐ์ต : Deployment Descriptor - DDํ์ผ = web.xml -->
<hr>
<a href="http://localhost/jspPro/hello.ss">hello ์๋ธ๋ฆฟ ํธ์ถ</a><br>
<a href="http://localhost/jspPro/hello.htm">hello ์๋ธ๋ฆฟ ํธ์ถ</a><br>
<a href="http://localhost/jspPro/hello">hello ์๋ธ๋ฆฟ ํธ์ถ</a><br>
* DDํ์ผ web.xml ์์ ๋งตํ ํ์ธ ๊ฐ๋ฅ!
5) ํฐ์บฃ ๋ฑ์ ์ปจํ ์ด๋๋ฅผ ์คํ( ์๋ฒ ์ฌ ์์.)
6) ์น ๋ธ๋ผ์ฐ์ ์์ ํ์ธ.
์๋ธ๋ฆฟ์ jsp ๋ณด๋ค ๋ณต์กํ์ง๋ง MVC ํจํด์ผ๋ก ๊ฐ๋ฐํ ๋๋ ์๋ธ๋ฆฟ ์ดํด ๋ฐ๋์
์์ด์ผ ๋๋ค.
[3]. ์๋ธ๋ฆฟ ๋งคํ ์ค์
4. ์ด๊ธฐํ ํ๋ผ๋ฏธํฐ
[5]. URL ํจํด ๋งคํ ๊ท์น p 514
1) / ์์ /* ๋
์) /test/board/*
/days02/*
2) *. ์์ ํ์ฅ์์ ๋ํ ๋งคํ
*.ํ์ฅ์๋ช
์) *.htm
*.ss
3) ์ค์ง / ๋ง ์๋ ๊ฒฝ์ฐ. ( ๊ธฐ๋ณธ ์๋ธ๋ฆฟ )
4) ์์ 1~3 ๊ฒฝ์ฐ์ธ์ ์ ํํ๊ฒ URL ๋งคํ
์) /days02/now.html
์๋ธ๋ฆฟ ์์
0. ex03.jsp
ใฑ. get ์ด๋ฆ/๋์ด -> Info ์๋ธ๋ฆฟ ํด๋์ค ์ ๋ฌ
ใด. post ์ด๋ฆ/๋์ด -> Info ์๋ธ๋ฆฟ ํด๋์ค ์ ๋ฌ
์ค์ (jquery, css)
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
1. Info ์๋ธ๋ฆฟ ํด๋์ค ์ ์ธ
2. /info
/days02/info.htm
* a๋งํฌ ํ๊ทธ -> ok jsp ๋ก ์ด๋
jspPro url ๊ฐ์ ธ์ด
<%
// "/jspPro"
String contextPath = request.getContextPath();
%>
1) ์ผ๋ฐ get (servlet์์ด)
์ ์ : <input type="text" id="n" name="n" value="10"> <br>
<a href="<%= contextPath %>/days02/ex03_ok.jsp">ex03_ok.jsp</a>
$("#tabs-1 a").click(function() {
// event.preventDefault();
// 1๋ฒ
/*
var origHref = $(this).attr("href");
$(this).attr( "href" , origHref +"?n=" + $("#n").val() );
*/
// 2๋ฒ
$(this).attr("href", function (i, origHref){
return origHref +"?n=" + $("#n").val();
});
});
2) get, post + servlet
<form>
Name : <input type="text" id="name" name="name" value="ํ๊ธธ๋"><br>
Age : <input type="text" id="age" name="age" value="20"><br>
<input type="radio" name="method" value="get" checked="checked">GET ์์ฒญ
<input type="radio" name="method" value="post" >POST ์์ฒญ
<br>
<!-- "/info", "/days02/info.htm" -->
<button type="button">์ ์ก(submit)</button>
</form>
// ํญ-2 ์ ์ ์ก๋ฒํผ์ ํด๋ฆญํ ๋...
$("form button").on("click" , function (){
// ์ฒดํฌ๋ ๋ผ๋์ค ๋ฒํผ
var method = $("#tabs-2 :radio:checked").val();
$("form")
.attr({
// /jspPro
"action": "<%= contextPath %>/info"
, "method": method
})
.submit();
});
servlet
package days02;
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 Info
*/
//@WebServlet({ "/info", "/days02/info.htm" })
public class Info extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Info() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Info ์๋ธ๋ฆฟ ์ GET ๋ฐฉ์์ผ๋ก ์์ฒญํ ๋ ํธ์ถ๋๋ ๋ฉ์๋
response.setContentType("text/html; charset=UTF-8");
String name = request.getParameter("name");
int age = Integer.parseInt( request.getParameter("age") );
// ์๋ต
PrintWriter out = response.getWriter();
out
.append("GET ๋ฐฉ์ ์์ฒญ")
.append( request.getContextPath() +"<br>" )
.append( "> ์ด๋ฆ : " + name +"<br>" )
.append( "> ๋์ด : " + age +"<br>" );
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Info ์๋ธ๋ฆฟ ์ POST ๋ฐฉ์์ผ๋ก ์์ฒญํ ๋ ํธ์ถ๋๋ ๋ฉ์๋
// > ์ด๋ฆ : íยย길ëยย - ํ๊ธ ๊นจ์ง๋๋ผ๊ณ ..
// ํฐ์บฃ(8) ์ธ์ฝ๋ฉ์ ์ค์ ํ์ง ์์ผ๋ฉด -> GET UTF-8
// POST ISO 8859-1
// ํ๊ธธ๋-> ์์ฒญ-> íยย길ëยย -> íยย길ëยย
// UTF-8 ISO 8859-1 UTF-8
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
String name = request.getParameter("name");
int age = Integer.parseInt( request.getParameter("age") );
// ์๋ต
PrintWriter out = response.getWriter();
out
.append("POST ๋ฐฉ์ ์์ฒญ")
.append( request.getContextPath() +"<br>" )
.append( "> ์ด๋ฆ : " + name +"<br>" )
.append( "> ๋์ด : " + age +"<br>" );
}
}
[ request ๊ฐ์ฒด] p 72
1. ์น๋ธ๋ผ์ฐ์ -> ์์ฒญ -> ์น์๋ฒ
์์ฒญ ์ ๋ณด ๊ฐ์ง๋ ๊ฐ์ฒด request
2. ํด๋ผ์ด์ธํธ๊ฐ ์ ์กํ ์ฟ ํค, ํค๋, ํ๋ผ๋ฏธํฐ ์ ๋ณด ์ฝ๊ธฐ ๊ธฐ๋ฅ
3. ์๋ฒ์ ๊ด๋ จ๋ ์ ๋ณด ์ฝ๊ธฐ
// js ์ฟ ํค : Cookie.js ํ์ผ ์์ฑ, document.cookie ์์ฑ
ํด๋ผ์ด์ธํธ + ์๋ฒ ๊ด๋ จ ์ ๋ณด ์ฝ๊ธฐ ํ3.3 ( p 73 )
*** 1. contextPath(์ปจํ ์คํธ ๋ฃจํธ) : <%= request.getContextPath() %><br>
*** 2. ํด๋ผ์ด์ธํธ IP ์ฃผ์ : <%= request.getRemoteAddr() %><br>
*** 3. ์์ฒญ ์ ์ก ๋ฐฉ์ : <%= request.getMethod() %><br>
*** 4. ์์ฒญ URL : <%= request.getRequestURL() %><br>
*** 5. ์์ฒญ URI : <%= request.getRequestURI() %><br>
์ต๊ทผ๋๊ธ