<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ex03_parameter</title>
<style type="text/css">
table, tr, td {
border: 1px solid gray;
border-collapse: collapse;
}
.title {
width: 100px;
}
td {
padding: 10px;
font-size: 24px;
text-align: center;
}
input[type=text]{
height: 26px;
font-size: 24px;
}
</style>
</head>
<body>
<h1>내용을 작성하세요</h1>
<form action="<http://localhost:8080/03_Servlet/paramServlet>" method="get">
<table>
<tr>
<td class="title">이름</td>
<td><input type="text" id="user_name" name="userName"/></td>
</tr>
<tr>
<td class="title">나이</td>
<td><input type="text" id="user_age" name="userAge"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="완료"/> <input type="reset" value="취소"/>
</td>
</tr>
</table>
</form>
</body>
</html>
package ex03;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ParamServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 웹 브라우저에서 전송된 데이터 인코딩 설정
request.setCharacterEncoding("UTF-8");
// 전송 데이터
// - getParameter() 메서드를 사용해서 <input> 태그의 name 속성값으로 전달된 value를 가져옵니다.
String name = request.getParameter("userName");
String s_age = request.getParameter("userAge");
int i_age = Integer.parseInt(s_age);
// 응답
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title> 결 과 </title>");
out.println("</head>");
out.println("<body>");
out.println(name+"님의 나이" + i_age + "세</br><br/>");
if(i_age >19)
out.println("성인");
else
out.println("미성년자");
out.println("</body>");
out.println("</html>");
}
}