- 서블릿
- JSP
웹프로그램
- 사이트(site)
- eclipse Dynamic Web Project 프로젝트하나를 만드는 것과 동일한 의미
- tomcat의 가상 디렉토리
JSP
- 확장자명 : jsp
- java program source + html
- <%@
- 디렉티브 :전체 선언
- directive
- page
- taglib
- include
- <%!
- 멤버변수 / 멤버 메서드 선언
- <%
- 스크립트렛 : 특수 메서드 내부 내용
- 지역변수
- 제어문
- 자바소스코드
- <%=
- 자바 소스 =HTML출력
JSP파일 만들기
JSP 기본탬플릿이 html4 버전이기 때문에 html5버전 탬플릿을 새로 만듦
2
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//JavaScript : console.log와 비슷함 내부 디버깅용
System.out.println("Hello JSP");
//출력용
out.println("<b>Hello JSP</b>");
%>
<!-- 출력 간단하게 -->
<%= "<b>Hello JSP</b>" %>
</body>
</html>
3
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
out.println("<html>");
out.println("<head>");
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
out.println("<title>Insert title here</title>");
out.println("</head>");
out.println("<body>");
out.println("<b>Hello JSP</b>");
out.println("</body>");
out.println("</html>");
%>
4
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 메서드의 선언 -->
<%!
public int multiply(int a, int b){
return a*b;
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<!-- view : mobile -->
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
10*25 = <%= multiply(10,25) %>
</body>
</html>
5
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- IMPORT는 페이지 선언에 옴 -->
<%@page import="java.util.Calendar" %>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//JAVA의 라이브러리를 사용할 수 있음
java.util.Date date = new java.util.Date(); //1회성
out.println(date.toLocaleString()+"<br>");
Calendar cal = Calendar.getInstance(); //import
out.println(cal.get(Calendar.YEAR));
%>
</body>
</html>
6
추가시킬 라이브러리 위치
7
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer result = new StringBuffer();
try{
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String user = "scott";
String password = "tiger";
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, user, password);
String sql = "select * from dept";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
result.append("<table border='1'>");
while(rs.next()){
result.append("<tr>");
result.append("<td>"+ rs.getString("deptno")+"</td>");
result.append("<td>"+ rs.getString("dname")+"</td>");
result.append("<td>"+ rs.getString("loc")+"</td>");
result.append("</tr>");
}
result.append("</table>");
}catch(ClassNotFoundException e){
result.append("[에러]+ " +e.getMessage());
}catch(SQLException e){
result.append("[에러]+ " +e.getMessage());
}finally{
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%= result %>
</body>
</html>
8
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer result = new StringBuffer();
try{
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String user = "scott";
String password = "tiger";
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, user, password);
String sql = "select ename, deptno, sal from emp where sal in(select max(sal) from emp group by deptno)";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
result.append("<table border='1'>");
while(rs.next()){
result.append("<tr>");
result.append("<td>"+ rs.getString("ename")+"</td>");
result.append("<td>"+ rs.getString("deptno")+"</td>");
result.append("<td>"+ rs.getString("sal")+"</td>");
result.append("</tr>");
}
result.append("</table>");
}catch(ClassNotFoundException e){
result.append("[에러]+ " +e.getMessage());
}catch(SQLException e){
result.append("[에러]+ " +e.getMessage());
}finally{
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 부서별 최고 급여를 받는 사원명, 부서번호, 급여 출력 -->
<%= result %>
</body>
</html>
9
JSP 기본 객체
- 인스터스화(객체변수)가 되어있는 객체
- 기본
- request
- 사용자의 요청 정보를 기록하는 객체
- response
- out
- session
- application
- 추가
- page
- pageContext
- config
- exception
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 기본 객체 request : 사용자의 요청 정보를 기록-->
<!-- 객체가 이미 생성되었기 때문에 선언 안해도 됨 -->
<%= request.getRemoteAddr() %><br> <!-- 요청한 곳의 아이피 -->
<%= request.getContentLength() %><br>
<%= request.getCharacterEncoding() %><br>
<%= request.getContentType() %><br>
<%= request.getProtocol() %><br>
<%= request.getMethod() %><br>
<%= request.getRequestURI() %><br> <!-- 요청한 페이지 -->
<%= request.getContextPath() %><br>
<%= request.getServerName() %><br> <!-- 서버 아이피 -->
</body>
</html>
10
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="form01_ok.jsp" method="post">
아이디 <input type="text" name="id">
비밀번호 <input type="password" name="pass">
<input type="submit" value="전송">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
//post방식으로 전송된 데이터가 한글일 경우 깨지는 현상을 수정
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String pass = request.getParameter("pass");
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
난 form01_ok.jsp 이다. <br>
아이디 <%= id %><br>
비밀번호 <%= pass %><br>
</body>
</html>
11
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 구구단 단수 입력해서 다른페이지에서 띄움 -->
<form action="gugu01_ok.jsp" method="post">
시작단<input type="text" name="startdan" placeholder="입력"> <!-- placeholder 입력칸에 표시알림 -->
끝단<input type="text" name="enddan" placeholder="입력">
<input type="submit" value="전송">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
int startdan = Integer.parseInt(request.getParameter("startdan"));
int enddan = Integer.parseInt(request.getParameter("enddan"));
String result = "";
result += "<table border='1'>";
for(int i=startdan; i<=enddan; i++){
result += "<tr>";
for(int j=1;j<=9;j++){
result += "<td>"+i+" X "+j+" = "+i*j+"</td>";
}
result += "</tr>";
}
result += "</table>";
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%= result %>
</body>
</html>
12. 입력값이 없을 경우 서버에서 값을 처리하며 에러 날 수 있음
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
//클라이언트 페이지에서 처리 //값을 채우지 않았을때 서버로 전송하지 않음
window.onload = function(){
document.getElementById('frm').onsubmit = function(){
var startdan = document.getElementById('startdan').value;
var enddan = document.getElementById('enddan').value;
if(startdan.length<1 || enddan.length<1){
alert('시작단과 끝단을 입력하세요');
return false;
}
};
};
</script>
</head>
<body>
<!-- 구구단 단수 입력해서 다른페이지에서 띄움 -->
<form action="gugu01_ok2.jsp" method="get" id="frm">
시작단<input type="text" name="startdan" id="startdan" placeholder="입력"> <!-- placeholder 입력칸에 표시알림 -->
끝단<input type="text" name="enddan" id="enddan" placeholder="입력">
<input type="submit" value="전송">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
//서버에서 처리 //클라이언트로부터 값을 받지 못했을 경우 기본적인 값을 설정
int startdan = Integer.parseInt((request.getParameter("startdan")== null || request.getParameter("startdan").equals(""))?"1":request.getParameter("startdan"));
int enddan = Integer.parseInt((request.getParameter("enddan")== null || request.getParameter("startdan").equals(""))?"2":request.getParameter("enddan"));
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
table {border : 1px solid black; border-collapse: collapse;}
td {width: 150px; border: 1px solid black;}
</style>
</head>
<body>
<%
out.print("<table border='1'>");
for(int i=startdan; i<=enddan; i++){
out.print("<tr>");
for(int j=1;j<=9;j++){
out.print("<td>"+i+" X "+j+" = "+i*j+"</td>");
}
out.print("</tr>");
}
out.print("</table>");
%>
</body>
</html>
13
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- 한장에 클라이언트 + 서버 구구단 만들기 -->
<%
request.setCharacterEncoding("utf-8");
String result = "";
//서버에서 처리
if(request.getParameter("startdan") != null && request.getParameter("enddan")!=null){
int startdan = Integer.parseInt(request.getParameter("startdan"));
int enddan = Integer.parseInt(request.getParameter("enddan"));
result += "<table border='1'>";
for(int i=startdan; i<=enddan; i++){
result += "<tr>";
for(int j=1;j<=9;j++){
result += "<td>"+i+" X "+j+" = "+i*j+"</td>";
}
result += "</tr>";
}
result += "</table>";
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
table {border : 1px solid black; border-collapse: collapse;}
td {width: 150px; border: 1px solid black;}
</style>
<script type="text/javascript">
//클라이언트 페이지에서 처리 //값을 채우지 않았을때 서버로 전송하지 않음
window.onload = function(){
document.getElementById('frm').onsubmit = function(){
var startdan = document.getElementById('startdan').value;
var enddan = document.getElementById('enddan').value;
if(startdan.length<1 || enddan.length<1){
alert('시작단과 끝단을 입력하세요');
return false;
}
};
};
</script>
</head>
<body>
<form action="gugu1page.jsp" method="get" id="frm">
시작단<input type="text" name="startdan" id="startdan" placeholder="입력">
끝단<input type="text" name="enddan" id="enddan" placeholder="입력">
<input type="submit" value="전송">
</form>
<%= result %>
</body>
</html>
14
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- 우편번호 검색** 클라이언트 서버 한 페이지에 -->
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%
request.setCharacterEncoding("utf-8");
String dong = request.getParameter("dong");
StringBuffer result = new StringBuffer();
//서버로 보낸 값이 null 아닌 경우만 밑에 식을 실행함 //한페이지에 클라이언트+서버를 다 적을 경우 처음 페이지 로딩시 서버로 보내는 값은 모두 null
if(dong !=null){
//데이터 베이스 연결
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String user = "scott";
String password = "tiger";
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, user, password);
String sql = "select zipcode, sido, gugun, dong, ri, bunji from zipcode where dong like ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, dong+'%');
//동 검색후 HTML코드 작성
rs = pstmt.executeQuery();
result.append("<table>");
result.append("<tr><th>우편번호</th><th>시도</th><th>동</th><th>리</th><th>번지</th></tr>");
while(rs.next()){
result.append("<tr>");
result.append("<td>"+"["+rs.getString("zipcode")+"]"+"</td>");
result.append("<td>"+rs.getString("sido")+"</td>");
result.append("<td>"+rs.getString("dong")+"</td>");
result.append("<td>"+(rs.getString("ri")==null?" ":rs.getString("ri"))+"</td>");
result.append("<td>"+(rs.getString("bunji")==null?" ":rs.getString("bunji"))+"</td>");
result.append("</tr>");
}
result.append("<table>");
}catch(ClassNotFoundException e){
result.append("[에러]+ " +e.getMessage());
}catch(SQLException e){
result.append("[에러]+ " +e.getMessage());
}finally{
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
table {border : 1px solid black; border-collapse: collapse;}
td, th {width: 150px; border: 1px solid black;}
</style>
<script type="text/javascript">
//클라이언트 페이지에서 처리 //값을 채우지 않았을때 서버로 전송하지 않음
window.onload = function(){
document.getElementById('frm').onsubmit = function(){
var dong = document.getElementById('dong').value;
if(!dong){
alert('시작단과 끝단을 입력하세요');
return false;
}
};
};
</script>
</head>
<body>
<form action="zipcode.jsp" method="get" id="frm">
검색주소(동)<input type="text" name="dong" id="dong" placeholder="입력">
<input type="submit" value="전송">
</form>
<!-- 서버에서 작성한 HTML코드를 출력 -->
<%= result %>
</body>
</html>
댓글 없음:
댓글 쓰기