javamailapi 로 구글검색. 오라클 사이트로 들어가서.
JavaMail API 1.5.1 Release 누르고 javax.mail.jar 다운. javax.mail-api.jar 다운.
뒤로 -> JavaBeans Activation Framework (JAF) 클릭.
JAF 1.1.1 클릭. Download 선택. jaf-1_1_1.zip 클릭해서 다운.
압축 풀어 있는 activation.jar 을 lib폴더에 넣고,
메일 발송
sendmail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String email = "";
String sender = null;
String userid = (String)session.getAttribute("username");
if(userid != null){
sender = (String)session.getAttribute("username");
}else if((sender = request.getParameter("name")) != null){
email = request.getParameter("email");
}else{
sender = "";
email = "";
}
%>
<!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>Welcome to Javasoft.com, inc.</title>
<link rel="stylesheet" type="text/css" href="../common/style.css">
<script>
function confirm(){
document.forms[0].submit();
}
</script>
</head>
<body topmargin="0">
<table width="760" height="100%" cellpadding="0" cellspacing="0" border="0" align="center">
<tr>
<td colspan="3" width="760" height="100">
<!-- top 시작 -->
<jsp:include page="../common/top_menu.jsp" />
</td>
</tr>
<tr width="760" height="520">
<td width="132" height="520" valign="top">
<!-- left 시작 -->
<jsp:include page="../common/left_menu.jsp" />
</td>
<td width="5" height="520">
</td>
<td width="620" height="520" class="main" valign="top">
<table width="620" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<!-- main영역은 좌로부터 40px 더 들여서 한다. -->
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="40"> </td>
<td>
<!-- main 시작 -->
<table border="0" cellspacing="0" cellpadding="0" width="595">
<tr height="10"><td colspan="2"> </td></tr>
<tr height="50"><td colspan="2" class="bo1">Email 발송하기</td></tr>
</table>
<form action="sendmail1.jsp" method="post">
<table border="0" cellspacing="0" cellpadding="5" width="595" style="padding-top:10px">
<tr>
<td width="120" align="right"><font color="#000000"><b>발송자의 이름</b></font></td>
<td><input type="text" value="<%=sender%>" name="sender"></td>
</tr>
<tr>
<td align="right"><font color="#000000"><b>발송자의 Email</b></font></td>
<td><input type="text" value="<%=email%>" name="sender_email" size="50"></td>
</tr>
<tr>
<td align="right"><font color="#000000"><b>수신자의 이름</b></font></td>
<td><input type="text" name="receiver"></td>
</tr>
<tr>
<td align="right"><font color="#000000"><b>수신자의 Email</b></font></td>
<td><input type="text" name="receiver_email" size="50"></td>
</tr>
<tr>
<td align="right"><font color="#000000"><b>제목</b></font></td>
<td><input type="text" name="subject" size="55"></td>
</tr>
<tr>
<td align="right"><font color="#000000"><b>내용</b></font></td>
<td>
<textarea rows="10" cols="55" name="contents"></textarea>
</td>
</tr>
<tr height="30" bgcolor="#f0f0f0">
<td colspan="2" align="center">
<input type="button" value="발송하기" style='font-size:1.3em' onclick="confirm()">
<input type="reset" value="취소하기" style='font-size:1.3em'>
</td>
</tr>
</table>
</form>
<!-- main 끝 -->
</td>
<td width="40"> </td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
sendmail1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" errorPage="../error/error.jsp" %>
<jsp:directive.page import="javax.activation.*" />
<jsp:directive.page import="java.util.Properties" />
<jsp:directive.page import="javax.mail.*" />
<jsp:directive.page import="javax.mail.internet.*" />
<% request.setCharacterEncoding("utf-8"); %>
<%
String sender = request.getParameter("sender");
String sender_email = request.getParameter("sender_email");
String receiver = request.getParameter("receiver");
String receiver_email = request.getParameter("receiver_email");
String subject = request.getParameter("subject");
String contents = request.getParameter("contents");
String server = "192.168.89.135"; //mail server ip or mail server name
Properties info = new Properties();
info.put("mail.smtp.host", server);
Session s = Session.getDefaultInstance(info, null);
Message message = new MimeMessage(s);
Address sender_address = new InternetAddress(sender_email);
Address receiver_address = new InternetAddress(receiver_email);
message.setHeader("content-type", "text/html;charset=utf-8");
message.setFrom(sender_address);
message.addRecipient(Message.RecipientType.TO, receiver_address);
message.setSubject(subject);
message.setContent(contents, "text/html;charset=utf-8");
message.setSentDate(new java.util.Date());
Transport transport = s.getTransport("smtp");
transport.connect(server, null, null);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
out.println("<h2>Send Mail Successfully</h2>");
%>
----------------------
방문자 수
left_menu.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.File, java.io.FileReader, java.io.BufferedReader" %>
<jsp:directive.page import="java.io.FileWriter" />
<%
String path = application.getRealPath(".") + "/count/count.txt";
File file = new File(path);
if(!file.exists()) file.createNewFile();
BufferedReader br = new BufferedReader(new FileReader(file));
String num = br.readLine().trim();
br.close();
if(num == null){
FileWriter fw = new FileWriter(file);
fw.write("0");
num = "0";
fw.close();
}
if(session.getAttribute("id_num") == null){
session.setAttribute("id_num", session.getId());
int count = Integer.parseInt(num) + 1;
FileWriter fw = new FileWriter(file);
fw.write(String.valueOf(count));
fw.close();
}
%>
<table width="132" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="132" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><img src="../images/left_maintop.gif" border="0"></td>
</tr>
<tr>
<td><img src="../images/left_main3.gif" border="0"></td>
</tr>
<tr>
<td height="35" class="left2" align='center'>
<!-- 총 방문자 수 들어갈 공간 -->
<%=imageCount(num) %>
</td>
</tr>
<tr>
<td colspan="2"><img src="../images/left_main4.gif" border="0"></td>
</tr>
<tr>
<td height="35" class="left1" align='center'>
<!-- 현재 방문자 수 들어갈 공간 -->
<%
Integer activeObj = (Integer)application.getAttribute("activecount");
if(activeObj == null){
activeObj = new Integer(1);
application.setAttribute("activecount", activeObj);
}else{
application.setAttribute("activecount", ++activeObj);
}
%>
<%=imageCount(activeObj.toString()) %>
</td>
</tr>
</table>
</td>
</tr>
<tr height="10" width="132">
<td> </td>
</tr>
<tr class="left">
<td>
<table width="132" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="right">
<% if(session.getAttribute("userid") == null){ %>
<a href="../member/login.jsp"><img src="../images/login.gif" border="0"></a>
<% }else{ %>
<a href="../member/logout.jsp"><img src="../images/logout.gif" border="0"></a>
<% } %>
</td>
</tr>
<tr height="10">
<td></td>
</tr>
<tr>
<td align="right">
<a href="../board/glist.jsp"><img src="../images/board.gif" border="0"></a>
</td>
</tr>
<tr height="10">
<td></td>
</tr>
<tr>
<td align="right">
<a href="../gesipan/list.jsp"><img src="../images/reply.gif" border="0"></a>
</td>
</tr>
<tr height="10">
<td></td>
</tr>
<tr>
<td align="right">
<a href="../mail/sendmail.jsp"><img src="../images/mail.gif" border="0"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>
<%!
private String imageCount(String num){
String str = "";
for(int i = 0 ;i < (6 - num.length()); i++){
str += "<img src='../images/0.gif'>";
}
for(int i = 0 ; i < num.length(); i++){
str += "<img src='../images/" + num.charAt(i) + ".gif'>";
}
return str;
}
%>
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
==============================================
JSTL
1. JSTL ?
1) JSP Standard Tag Library
2) 표준화된 태그 라이브러리를 제공함으로써 보다 편리하고 표준에 맞게 웹 프로그램을 개발할 수 있기 위함.
3) JSTL 1.2.x 는 Serlvet 2.5 이상, JSP 2.1 이상
JSTL 1.1.x 는 Servlet 2.4 이상, JSP 2.0 이상에서 지원
2. Homepage
1) JSTL 1.2.x
https://jstl.java.net/
2) JSTL 1.1.x
- http://tomcat.apache.org/taglibs/standard/
3. Download
1) JSTL 1.2.x
- https://jstl.java.net/download.html
- JSTL API : javax.servlet.jsp.jstl-api-1.2.1.jar
- JSTL Implementation : javax.servlet.jsp.jstl-1.2.1.jar
- Copy to /WEB-INF/lib
2) JSTL 1.1.x
- http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
- jakarta-taglibs-standard-1.1.2.zip
- Unzip
- /lib/jstl.jar, /lib/standard.jar
- copy to /WEB-INF/lib
4. JSTL 에서 제공하는 Tag 종류
1) Core : http://java.sun.com/jsp/jstl/core : c
2) XML Processing : http://java.sun.com/jsp/jstl/xml : x
3) I18N capable formatting : http://java.sun.com/jsp/jstl/fmt : fmt
4) SQL : http://java.sun.com/jsp/jstl/sql : sql
5) Functions : http://java.sun.com/jsp/jstl/functions : fn
5. Reference Sites
- java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html
--> http://docs.oracle.com/cd/E17802_01/products/products/jsp/jstl/1.1/docs/tlddocs/index.html
- http://docs.oracle.com/javaee/6/tutorial/doc/
6. Core Tag
1) JSP 에서 가장 필요하고 핵심적인 기본적인 기능들을 모아 놓은 라이브러리
2) <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3) Tags
4) <c:catch />
- 예외 처리를 위한 태그
- Syntax
<c:catch var="변수명" />
- 예외가 발생하면 var 에 지정된 변수에 예외 객체를 할당
5) <c:out />
- JSTL 에서 지정된 값을 출력하는 태그
- Syntax
<c:out value="출력값" default="기본값" escapeXml = "true | false" />
- value : 변수의 내용값
- default 속성은 기본 값을 의미하는데, value 의 값이 null일 경우 사용
- escapeXml 는 기본인 true 이고 따라서 true 이면 < <, > > 으로 변환되고,
false로 설정하면 <, > 등 특수기호를 처리할 때 사용
6) <c:set />
-지정된 변수에 값을 설정
- Syntax
<c:set var="변수명" value="설정값" target="객체" property="값" scope="범위" />
- var는 값을 저장할 변수의 이름
- value 저장할 값
- target 값을 설정할 프라퍼티를 갖고 있는 객체(JavaBean, Map)을 의미
- property 값을 설정할 객체의 프라퍼티
- scope : application | session | request | page
7) <c:if>
- 조건 처리할 때 사용되는 태그
- <c:if test="조건" var="변수명" scope="범위" />
- test : 조건을 지정
- var : 조건 처리한 결과를 저장할 변수
8) <c:choose>, <c:when>, <c:otherwise>
- 조건 처리할 때 사용하는 태그, switch ~ case 와 같다.
<c:choose>
<c:when test="조건"></c:when>
<c:when test="조건"></c:when>
<c:when test="조건"></c:when>
<c:otherwise></c:otherwise>
</c:choose>
9)<c:forEach />
- 반복 처리할 때 사용, Java의 for 와 같다.
<c:forEach items="객체명" begin="시작" end="끝" step="증감식" var="변수명" varStatus="상태변수명" />
- items 반복할 속성을 갖고 있는 컬렉션이나 배열형태의 객체
- begin 과 end 만큼 반복
- step 증감식 설정
- var 반복할 때 반복하고 값을 저장할 변수
- varStatus 반복의 상태를 갖는 변수
10) <c:forTokens>
- Java 의 for문과 StringTokenizer 객체를 결합한 것
- <c:forTokens items="객체명" delims = "구분자" begin="시작인덱스" end ="끝 인덱스"
step ="증감식" var="변수명" varStatus="상태변수" />
11) <c:import >
- 지정된 URL 이 포함된 JSP 페이지를 출력할 때 사용하는 태그
- <jsp:include > 동일
<c:import url="URL 값" var="변수명" scope="범위" varReader="입력스트림명"
charEncoding="인코딩값" context="contextName" />
- url 은 HTTP, FTP 외부 리소스
12) <c:redirect>
7. I18N capable formatting
1) 국제화/형식화의 기능을 제공해 주는 라이브러리
2) <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
3) <fmt:requestEncoding/>
4) <fmt:setLocale />
5) <fmt:timeZone />
6) <fmt:setTimeZone />
7) <fmt:bundle />
8) <fmt:setBundle />
9) <fmt:formatNumber />
10) <fmt:formatDate />
11) <fmt:formatTime />
8. JSTL sql
1) DAO 나 DTO 를 사용하지 않는 환경에서 서버에 연결, 접근, 쿼리 전송, 트랜잭션 처리를
하기 위한 라이브러리
2) <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
3) <sql:setDataSource />
4) <sql:query />
- query 가 SELECT 일 때
- javax.servlet.jsp.jstl.sql.Result interface 객체를 얻어옴
5) <sql:update />
6) <sql:param />
7) <sql:transaction />
예제
fmtdemo.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<fmt:requestEncoding value="utf-8" />
<form method="post">
Name : <input type="text" name="irum"/><br />
<input type="submit" value="전송" />
</form><br />
Name : <c:out value="${param.irum }"/>
fmtdemo1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Number : <fmt:formatNumber value="12345678.3456" type="number" /><br />
Currency : <fmt:formatNumber value="1500000" type="currency"
currencySymbol="$" /><br />
Percent : <fmt:formatNumber value="1500000" type="percent" /><br />
fmtdemo2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="now" value="<%=new java.util.Date() %>" />
Now is ${now} <br />
Now is <c:out value="${now}" /><br />
Date : <fmt:formatDate value="${now}" type="date" /><br />
Time : <fmt:formatDate value="${now}" type="time" /><br />
Both : <fmt:formatDate value="${now}" type="both" /><br />
<hr>
type : short <fmt:formatDate value="${now}" type="date" dateStyle="short"/><br />
type : long <fmt:formatDate value="${now}" type="date" dateStyle="long"/><br />
type : full <fmt:formatDate value="${now}" type="date" dateStyle="full"/><br />
fmtdemo3.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="now" value="<%=new java.util.Date() %>" />
KOREA : <fmt:formatDate type="both" value="${now}"
dateStyle="full" timeStyle="full" /><br />
UK : <fmt:timeZone value="GMT">
<fmt:formatDate type="both" value="${now}"
dateStyle="full" timeStyle="full" />
</fmt:timeZone><br />
US : <fmt:timeZone value="America/New_York">
<fmt:formatDate type="both" value="${now}"
dateStyle="full" timeStyle="full" />
</fmt:timeZone>
----------------------------------
server.xml 에서 추가해주고
<Context docBase="0703" path="/0703" reloadable="true" source="org.eclipse.jst.jee.server:0703">
<Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" maxActive="10" maxIdle="10" maxWait="10000" name="jdbc/myoracle" password="tiger" type="javax.sql.DataSource" url="jdbc:oracle:thin:@192.168.228.132:1521:orcl" username="scott"/>
</Context>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>0627</display-name>
<resource-ref>
<description>Oracle JDNI DataSource</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
sqldemo.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<sql:setDataSource var="conn"
driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@192.168.228.132:1521:orcl"
user="scott"
password="tiger"
/>
<sql:query var="rs" dataSource="${conn}">
SELECT empno, ename, hiredate, sal, comm
FROM emp
</sql:query>
<table border="1" cellpadding="0" cellspacing="0" width="500">
<caption>사원 정보</caption>
<thead>
<tr>
<th>사번</th><th>사원명</th><th>입사일</th><th>봉급</th><th>보너스</th>
</tr>
</thead>
<tbody>
<c:forEach var="data" items="${rs.rows}">
<tr>
<td><c:out value="${data.empno}" /></td>
<td><c:out value="${data['ename']}" /></td>
<td><c:out value="${data.hiredate}" /></td>
<td><c:out value="${data['sal']}" /></td>
<td><c:out value="${data.comm}" /></td>
</tr>
</c:forEach>
</tbody>
</table>
----------------------------
sqldemo1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<sql:query var="rs" dataSource="jdbc/myoracle">
SELECT * FROM dept
</sql:query>
<table border="1" cellpadding="0" cellspacing="0" width="600">
<caption>부서 정보</caption>
<thead>
<tr height="40">
<c:forEach var="columnName" items="${rs.columnNames}">
<th><c:out value="${columnName}" /></th>
</c:forEach>
</tr>
</thead>
<tbody>
<c:forEach var="row" items="${rs.rowsByIndex}">
<tr height="50">
<c:forEach var="column" items="${row}">
<td>
<c:if test="${column != null}">
<c:out value="${column}" />
</c:if>
<c:if test="${column == null}">
</c:if>
</td>
</c:forEach>
</tr>
</c:forEach>
</tbody>
</table>
sqldemo1.jsp뒤에 ?deptno=60&dname=IT&loc=Pusan 을 써서 추가도 가능하고 select도 가능하게.
----------------
우편번호 검색
sqldemo2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<sql:query var="rs" dataSource="jdbc/myoracle">
SELECT zipcode, sido, gugun, dong, bunji FROM zipcode
WHERE dong LIKE CONCAT(CONCAT('%', ?), '%')
<sql:param value="${param.keyword}" />
</sql:query>
<table border="1" cellpadding="0" cellspacing="0" width="1000" style="margin:auto">
<caption>동/읍/면 : <c:out value="${param.keyword}" /></caption>
<thead>
<tr height="50">
<th width="10%">우편번호</th>
<th width="10%">시도</th>
<th width="20%">구군</th>
<th width="30%">동/읍/면</th>
<th width="30%">번지</th>
</tr>
</thead>
<tbody>
<c:forEach var="row" items="${rs.rowsByIndex}">
<tr height="40">
<c:forEach var="column" items="${row}">
<td>
<c:if test="${column != null}">
<c:out value="${column}" />
</c:if>
<c:if test="${column == null}">
</c:if>
</td>
</c:forEach>
</tr>
</c:forEach>
</tbody>
</table>
'Java & Oracle' 카테고리의 다른 글
Command Pattern (0) | 2014.07.07 |
---|---|
Custom Tag (0) | 2014.07.04 |
게시판 작성자 링크 댓글기능 삭제 전까지 (0) | 2014.07.02 |
게시판 쓰기 및 목록 띄우기까지 (0) | 2014.07.01 |
자료실(파일업로드), 게시판(하기전세팅) (0) | 2014.06.30 |