javax.servlet:javax.servlet-api:4.0.1
org.mariadb.jdbc:mariadb-java-client:3.1.4
org.apache.tomcat:tomcat-servlet-api:9.0.78
<%--jsp 각주--%>
<%@ 디렉티브. 문서의 시작, import할 때 사용 %>
<%@include file=""%>
<%@include file="./nav.jsp"%>
<%!
선언문
%>
<% 스크립트릿 %>
<%=표현식 %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Response</title>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
<%
request.setCharacterEncoding("UTF-8"); // 페이지 내 한글 깨짐 문제
response.setContentType("text/html; charset=utf-8");
response.setCharacterEncoding("UTF-8");
%>
</head>
<body>
</body>
</html>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
object.setAttribute(String name, Object value); // 이름이 name인 속성에 value 값 지정
object.getAttribute(String name); // 이름이 name인 속성의 Attribute 객체 반환
object.removeAttribute(String name); //이름이 name인 속성 삭제
object.getAttributeNames(); // 속성의 이름 목록 반환
request.setCharacterEncoding("UTF-8");
// request 한국어 인식 설정
String id = request.getParameter("id");
// form에서 post한 데이터 받을 때
String[] animal = request.getParameterValues("animal");
// form에서 checkbox와 같이 복수의 데이터를 받을 때
request.getProtocol()
request.getServerName()
request.getMethod()
request.getQueryString()
request.getRequestURL()
request.getRequestURI()
request.getRemoteHost()
request.getRemoteAddr()
request.getRemotePort()
request.getServerPort()
request.getContextPath()
request.getHeader("Accept")
request.getHeader("Host")

String path = request.getContextPath(); // 현재 프로젝트 디렉토리를 알아서 가져옴.
...
<p>img: <img src="<%=path%>/img/dog%20ears.gif" alt="img"></p>
response.setCharacterEncoding("UTF-8");
response.addHeader("Site", "https://chunjae.co.kr"); // 있는 헤더 추가
response.setHeader("Url2", "https://www.naver.com"); // 없는 헤더 추가
response.sendRedirect("test05.jsp?msg="+msg);
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.nio.charset.StandardCharsets" %>
response.sendRedirect("test05.jsp?msg="+URLEncoder.encode(msg, StandardCharsets.UTF_8));
response.setStatus(200); // 수동 정상 처리
response.sendError(200, "페이지가 정상적으로 처리되었습니다.");
response.setStatus(404); // 수동 오류 처리
response.sendError(404, "해당페이지가 존재하지 않습니다.");
response.getContentType()
response.getCharacterEncoding()
response.containsHeader("Site") // 헤더 존재 유무
response.getStatus()
String pw = (String) session.getAttribute("pw");
session.setAttribute("id", id);
session.invalidate(); // 로그아웃 처리. 모든 session의 설정된 attribute를 지움
session.removeAttribute("id"); // 특정 항목의 session atttribute를 지움
request, response, session 많이 사용.
JspWriter pageOut = pageContext.getOut(); // out
ServletRequest pageRequest = pageContext.getRequest(); // request
ServletResponse pageResponse = pageContext.getResponse(); // response
HttpSession pageSession = pageContext.getSession(); // session
ServletContext pageServlet = pageContext.getServletContext(); // application
ServletConfig pageConfig = pageContext.getServletConfig(); // config
Exception pageException = pageContext.getException(); // exception
Object pageObject = pageContext.getPage(); // page
out은 html(태그 포함) 출력이 가능하여, <%스크립트릿%> 을 열고 닫는 것을 생략해준다.
- include: 현재 페이지에 target.jsp의 내용을 가져오는 것
- forward: 현재 페이지로 target.jsp의 내용을 전송하는 것. 페이지 이동과는 다르다.
| include | forward | |
|---|---|---|
| title | 유지 | target의 title |
| 주소 url | 유지 | 유지 |
| 내용 | 현재 내용 중간에 그 내용이 들어감 | target의 내용만을 가져와 표시함 |
package dao;
public class DBConTest {
public static void main(String[] args) {
final String DRIVER = "org.mariadb.jdbc.Driver"; // 마리아db lib의 패키지 안의 클래스
final String DNS = "localhost"; // post server 주소
final String PORT = "3306";
final String NAME = "edu";
final String user = "root";
final String PW = "1234";
final String URL = "jdbc:mariadb://" +DNS+":"+PORT+"/"+NAME;
// jdbc:mariadb://localhost:3306/edu
// 프로토콜:디비종류://호스트주소:포트번호/데이터베이스
}
}
<display-name>ch02</display-name>
<welcome-file-list>
<welcome-file>test6.jsp</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/notFoundErrorPage.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.NumberFormatException</exception-type>
<location>/formatException.jsp</location>
</error-page>
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
String notFoundPath = request.getContextPath();
%>
</head>
<body>
<div class="container">
<br>
<h2>요청하신 페이지를 찾을 수 없습니다.</h2>
<a href="index.jsp">To Home</a>
<br>
<img src="<%=notFoundPath%>/img/notfound.gif" alt="This Page is not Found">
</div>
</body>
</html>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
<url-pattern>*.css</url-pattern>
<url-pattern>*.jpg</url-pattern>
<url-pattern>*.png</url-pattern>
<url-pattern>*.gif</url-pattern>
<url-pattern>*.webp</url-pattern>
</servlet-mapping>
javascript로 드래그 및 복사 붙여넣기 금지하기(공통으로 들어가는 head 파일에 추가하기)
잘 읽었습니다. 좋은 정보 감사드립니다.