enctype="multipart/form-data"
)HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>File Upload Example</h3>
<form action="file_upload.do" method="post" enctype="multipart/form-data">
첨부파일 : <input type="file" name="attachedFile"/>
<input type="submit" value="전송"/>
</form>
</body>
</html>
Servlet
@WebServlet("/mod004/file_upload.do")
@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 2, // 2MB, 2MB보다 커지면 저장소를 쓴다는 의미
maxFileSize = 1024 * 1024 * 10, // 10MB, 업로드 최대 크기
maxRequestSize = 1024 * 1024 * 50) // 50MB, 다른 정보들과 파일을 포함한 총합 크기, - 주면 무제한
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static final String SAVE_DIR = "C:/temp"; // 경로를 상수로 지정
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
File saveDir = new File(SAVE_DIR);
if (!saveDir.exists()) { // 경로에 폴더가 존재하지 않는다면 폴더를 만듬
saveDir.mkdir();
}
for (Part part : request.getParts()) { //request로 정보를 받아오고, part에 나눠서 정보를 저장한다는 의미
// c:\temp\파일
part.write(saveDir.getPath() + File.separator + extractFileName(part));
}
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h3> Upload has been done successfully.</h3>");
out.println("</body></html>");
out.close();
}
//파일 넘버를 구분
private String extractFileName(Part part) {
String contentDisp = part.getHeader("Content-Disposition");
System.out.println("Content-Disposition: " + contentDisp);
for (String s : contentDisp.split(";")) {
if (s.contains(File.separator)) {
return s.substring(s.lastIndexOf(File.separator) + 1).replace("\"", "");
} else {
return s;
}
}
return "";
}
}
출처 : https://mangkyu.tistory.com/14
출처 : https://www.code-sample.com/2019/03/jsp-interview-questions-and-answers.html
<%-- --%>
<%@ %>
, <%@ include %>
, <%@ taglib %>
첫 줄<%! 멤버 선언 %>
, <% 자바 코드 %>
, <%= 출력 %>
${표헌식}
JSTL
import="java.utill.*"
contentType="text/html. charset=UTF-8"
: MIME타입과 문자 인코딩 설정session=true
: 해당 페이지가 세션을 지원하려면 true (디폴트 값은 true)errorPage="에러를 처리할 페이지"
isErrorPage="true"
: 이 값이 true일 때 exception 내장 객체를 사용 가능 (디폴트 값은 false)pageEncoding
: JSP 페이지의 문자 인코딩을 정의(ISO-8859-1)isELIgnored
, isThreadSafe
, language
, extends
, buffer
, autoFlush
, info
: 거의 활용 X- 밑의 코드는 분리되어서 처리되어 있지 않음
<%
if(age > 10) {
you need a ticket.
} else {
You are free.
}
%>
답안)
<%
if(age > 10) {
%>
you need a ticket.
<%
} else {
%>
You are free.
<%
}
%>
Ten is <% (2 * 5) %>
Thank you, <%= name %> </b> for registration.
The Current day and time is <%= new java.util.Date() %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%-- %@는 디렉티브 태그 --%>
<%!private static final String DEFAULT_NAME = "박강산";%>
<%-- %!는 멤버 변수 --%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<%-- JSP 주석 --%>
<body>
<%
String name = request.getParameter("userName");
if (name == null || name.length() == 0) {
name = DEFAULT_NAME;
}
%><%-- 자바 코드 입력 가능 --%>
<h3>
Hello,
<%=name%><%-- %=는 자바 코드 출력 --%>
</h3>
</body>
</html>