retrieve.jsp
<select>태그에서 일치하는 직업의 <option>태그를 seleted 하는 코드
<%@page import="com.dto.EmpDTO"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
List<EmpDTO> list = (List<EmpDTO>)request.getAttribute("list");
EmpDTO dto = list.get(0);
int empno = dto.getEmpno();
String ename = dto.getEname();
String job = dto.getJob();
int mgr = dto.getMgr();
String hiredate = dto.getHiredate();
int sal = dto.getSal();
double comm = dto.getComm();
%>
<!DOCTYPE html>
<html>
<head>
<title>Employee Form</title>
<!-- Add your CSS here if necessary -->
</head>
<body>
<form action="EmpUpdateServlet" method="post"> <!-- Replace with your actual submission handling JSP -->
<input type="hidden" name="empno" value="<%=empno %>">
<table border=1>
<tr>
<th>사원번호</th>
<td><%=empno %></td>
</tr>
<tr>
<th>사원이름</th>
<td><%=ename %></td>
</tr>
<tr>
<th>직업</th>
<td>
<select name="job">
<option <%if(job.equals("CLERK")){%> selected<%} %>>CLERK</option>
<option <%if(job.equals("ANALYST")){%> selected<%} %>>ANALYST</option>
<option <%if(job.equals("SALESMAN")){%> selected<%} %>>SALESMAN</option>
<option <%if(job.equals("PRESIDENT")){%> selected<%} %>>PRESIDENT</option>
<option <%if(job.equals("MANAGER")){%> selected<%} %>>MANAGER</option>
</select>
</td>
</tr>
<tr>
<th>관리자</th>
<td><%=mgr %></td>
</tr>
<tr>
<th>입사일</th>
<td><%=hiredate %></td>
</tr>
<tr>
<th>월급</th>
<td><input type="text" value="<%=sal %>" name="sal"></td>
</tr>
<tr>
<td colspan="2">
<button>사원수정</button>
</td>
</tr>
</table>
<a href="EmpListServlet">목록보기</a>
<a href="EmpDeleteServlet?empno=<%=empno %>">삭제</a>
</form>
</body>
</html>
mybatis에서 MAP, choose when, like을 이용
like 사용시 concat, + 를 사용하면 오류 뜸.
그냥 한 문자열 내에 붙여도 문제가 생김
<select id="select" resultType="EmpDTO" parameterType="hashmap">
select empno, ename, job, mgr, to_char(hiredate, 'yyyy/mm/dd') hiredate, sal, comm, deptno
from emp
<choose>
<when test="searchName == 'hiredate'">
where to_char(hiredate, 'yyyy') like '%'||#{searchValue}||'%'
</when>
<when test="searchName == 'ename'">
where ename like '%'||#{searchValue}||'%'
</when>
<otherwise>
</otherwise>
</choose>
order by empno desc
</select>
where를 앞에 자동으로 붙여주는 <where> <if test="">
<select id="select2" resultType="EmpDTO" parameterType="hashmap">
select empno, ename, job, mgr, to_char(hiredate, 'yyyy/mm/dd') hiredate, sal, comm, deptno
from emp
<where>
<if test="searchName == 'hiredate'">
to_char(hiredate, 'yyyy') like '%'||#{searchValue}||'%'
</if>
<if test="searchName == 'ename'">
ename like '%'||#{searchValue}||'%'
</if>
</where>
order by empno desc
</select>
기존 전체 선택 : select all => dto => list 리턴 출력
페이지 나누기
curPage: 현재 보는 페이지 번호
perPage: 한 페이지에 보여지는 목록의 갯수
totalCount: 전체 레코드의 갯수
totalCount/perPage = 7
totalCount%perPage > 0 => totalPage+1
curPage 1인경우 => 0,2
curPage 2인경우 => 2,2
curPage 3인경우 => 4,2
curPage 4인경우 => 6,2
(curPage-1)*perPage
rowbounds로 결과값 중에 필요한 정보들만 반환하기
48번 워크샵 분석하기
세션은 브라우저에 저장되는 반면 쿠키는 사용자의 컴퓨터에 저장이 된다.
key value (아쉽게도 오브젝트 리스트는 저장이 불가능하고 String만 저장된다.)
또한 클라이언트에서 쿠키를 사용하지 못하도록 설정할 수 있기 때문에 제약이 있다.
쿠키는 사용자의 상태정보를 클라이언트에서 관리하는 메커니즘을 의미한다.
쿠키는 도메인당 300개까지 저장이 가능하지만, 클라이언트에서 쿠키를 사용하지 못하도록 설정할 수 있기 때문에 제약이 있다.
쿠키는 클라이언트의 브라우저 메모리 또는 OS파일에 저장이 가능하다.
세션과 동일하게 TIME-OUT 적용이 가능하다.
KEY/VALUE 하나가 쿠키다.
생성자 및 메서드 :
setCookie.java
package com.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SetCookie
*/
@WebServlet("/SetCookie")
public class SetCookie extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SetCookie() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 쿠키 생성
Cookie c= new Cookie("username", "홍길동");
Cookie c1= new Cookie("age", "10");
//2. 작업 후 응답에 쿠키 시간 설정 후 추가
c.setMaxAge(60*60*24); // 설정 필수
response.addCookie(c); // 이 시전에 사용자 pc에 저장
response.addCookie(c1); // 이 시전에 사용자 pc에 저장
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);
}
}
getCookie.java
package com.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class GetCookie
*/
@WebServlet("/GetCookie")
public class GetCookie extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GetCookie() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
// 1. 요청에서 쿠키 얻기
Cookie[] cookies = request.getCookies(); // 모든 쿠키를 배열로 반환
//2. 쿠키 사용하기
for(Cookie c : cookies) {
if("username".equals(c.getName())) {
String name = c.getName();
String value = c.getValue();
System.out.println(name+"\t"+value);
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
mybatis 처음부터 selectAll후 jsp에 순회하며 출력하는 것까지 연습
list<EmpDTO> 순회 for문 작성 중 시작 index와 끝 index를 설정하는 것에 있어서 헷갈림.for(int i=0; i<list.size()-1; i++) 까지가 범위이다. 기본적인 배열과 index 시작 값이 같다고 보면 될 것 같다.pt읽고 따로 정리해보기
추가 참고 사이트 : https://handam.tistory.com/138
동적 jsp를 사용하는 문법에서 flush=true를 필수로 넣으라는데 flush가 정확히 어떤 기능을 하는 속성인지?
=> 문서의 출력 결과를 항상 버퍼내에서 갱신 하라는 의미이다.
출처: https://yongblog.tistory.com/entry/jspinclude-와-include-차이 [기억력이 나쁜 나에게 주는 페이지:티스토리]
쿠키를 get해서 Cookie[]로 받은 데이터를 개별 선택하여 사용하는 방법이 있는지?