Ex1.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Ex1 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//요청시 한글 처리(post방식일때)
req.setCharacterEncoding("euc-kr");
//요청시 전달되는 파라메터 값을 받는다.
String s_name = req.getParameter("e_name");
String s_phone = req.getParameter("e_phone");
//받은 값을 화면에 출력하기 위해 응답준비
res.setContentType("text/html;charset=euc-kr");
PrintWriter out = res.getWriter();
out.println("<body>");
out.println("입력하신 정보 :");
out.println("<hr>");
out.println("이름 : " + s_name + "<br>");
out.println("전화번호 : " + s_phone + "<br>");
out.println("</body>");
out.close();
}
}
Ex1.html
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action="Ex1" method="post">
이름 : <input type="text" name="e_name" size="8"><br>
전화 : <input name ="e_phone" maxlength="15"><br><br>
<input type="submit" value="전송">
</form>
</body>
</html>
Ex1.java 결과
Ex2.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Ex2 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 요청시 한글처리
request.setCharacterEncoding("euc-kr");
// 파라메터 값들 받기
String s_id = request.getParameter("id");
String s_pwd = request.getParameter("pwd");
// 체크박스는 여러개가 선택되어 넘어 올수 있다.
// 그러므로 받을 때는 배열로 받아야 한다.
// 즉, 이름이 같은 것들이 여러개 있다면 받을때
//getParameterValues() 메서드로 받으면서 배열로 담는다.
String[] s_hobby = request.getParameterValues("hobby");
String s_army = request.getParameter("army");
//응답시 한글처리
response.setContentType("text/html;charset=euc-kr");
PrintWriter out = response.getWriter();
out.println("<body>");
out.println("입력하신 정보 :");
out.println("<hr>");
out.println("이름 : " + s_id + "<br>");
out.println("비밀번호 : " + s_pwd + "<br>");
out.println("취미 : ");
for(String n : s_hobby)
out.println(" "+n);
out.println("군필여부" + s_army +"<br>");
out.println("</body>");
out.close();
}
}
Ex2.html
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action="Ex2" method="post">
ID : <input name="id"><br>
Password : <input type="password" name="pwd"><br>
취미 : <input type="checkbox" value="독서" name="hobby">독서
<input type="checkbox" value="영화감상" name="hobby">영화감상
<input type="checkbox" value="star c" name="hobby">스타<br>
군필여부 : <input type="radio" value="필" name="army">필
<input type="radio" value="미필" name="army">미필<br>
<input type="submit" value="보내기">
</form>
</body>
</html>
Ex2 .결과
Ex3.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
public class Ex3 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
private int count = 0;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//요청시 한글 처리
request.setCharacterEncoding("euc-kr");
//파라메터 값 받기
String[] s_name = request.getParameterValues("test");
//응답시 한글처리
response.setContentType("text/html;charset=euc-kr");
PrintWriter out = response.getWriter();
//응답시작
out.println("<body>");
out.println("입력된값 : " + s_name.length);
out.println("<hr>");
int count = 0;
for(String n : s_name)
if(n.trim().length() > 0){ // 무의미한 공백 제거후
out.println(n+"<br>"); //길이가 0보다 큰경우만 출력!!
count++;
}
out.println("</body>");
out.close();
//DB작업
if(count == 5)
try {
InitialContext ic = new InitialContext();
Context ctx = (Context)ic.lookup("java:comp/env");
DataSource ds = (DataSource)ctx.lookup("jdbc/oracle");
Connection con = ds.getConnection();
PreparedStatement pstmt = con.prepareStatement("INSERT INTO test values(?,?,?,?,?)");
int cnt = 0;
for(String n : s_name)
pstmt.setString(++cnt, n);
pstmt.executeUpdate(); //DB저장
pstmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Ex3.html
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action="Ex3" method="post">
<table border="1" width="350" cellpadding="0" cellspacing="0" bordercolor="purple" background="white">
<tr>
<td align="center">친구1</td>
<td><input name="test"></td>
</tr>
<tr>
<td align="center">친구2</td>
<td><input name="test"></td>
</tr>
<tr>
<td align="center">친구3</td>
<td><input name="test"></td>
</tr>
<tr>
<td align="center">친구4</td>
<td><input name="test"></td>
</tr>
<tr>
<td align="center">친구5</td>
<td><input name="test"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="보내기">
</td>
</tr>
</table>
</form>
</body>
</html>
'Programing > JSP' 카테고리의 다른 글
JSP 에러관리 로그파일로 생성하기 (0) | 2008.11.19 |
---|---|
Servlet - Login (0) | 2008.07.29 |
Servlet - JDBC활용 (0) | 2008.07.29 |
Servlet - 공지파일 예제 (0) | 2008.07.29 |
Servlet - WAS 구동과정 및 Servlet의 생명 주기 (1) | 2008.07.29 |