새로 만든다.
소스
//action="" 이부분에는 PersonServlet 을 실행시킨후 주소표시줄을 넣어준다.
<html>
<h2>Servlet 폼 데이터 처리</h2>
<form method="post" action="http://localhost:8080/JSP/PersonServlet">
<table border="1">
<tr>
<td>이름</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>나이</td>
<td><input type="text" name="age">
</tr>
<tr>
<td>성별</td>
<td><input type="radio" name="gender" value="1">남자 <input
type="radio" name="gender" value="2">여자</td>
</tr>
<tr>
<td>취미</td>
<td><input type="checkbox" name="hobby" value="1">독서 <input
type="checkbox" name="hobby" value="2">등산 <input
type="checkbox" name="hobby" value="3">운동 <input
type="checkbox" name="hobby" value="4">낚시</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="전송">
<input type="reset" value="취소">
</tr>
</table>
</form>
</html>
서블릿 만들기
study -> PersonServlet.java 를 만든다.
서블릿소스 - PersonServlet.java
package study;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PersonServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String name = new String(request.getParameter("name")
.getBytes("8859_1"), "euc-kr");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
String hobby[] = request.getParameterValues("hobby");
response.setContentType("text/html;charset=euc-kr");
PrintWriter out = response.getWriter();
out.println("<h2> Servlet 폼 데이터 처리 </h2>");
out.println("당신의 이름은 " + name + "이고<br>");
out.println("나이는 " + age + "살이고<br>");
if (gender.equals("1")) {
out.println("남자입니다.<br>");
} else if (gender.equals("2"))
out.println("여자입니다.<br>");
out.print("당신의 취미는");
for (int i = 0; i < hobby.length; i++) {
switch (Integer.parseInt(hobby[i])) {
case 1:
out.print(" 독서");
break;
case 2:
out.print(" 등산 ");
break;
case 3:
out.print(" 운동");
break;
case 4:
out.print(" 낚시");
break;
}
}
out.println("입니다.");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
xml소스 추가
<servlet>
<display-name>PersonServlet</display-name>
<servlet-name>PersonServlet</servlet-name>
<servlet-class>study.PersonServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PersonServlet</servlet-name>
<url-pattern>/PersonServlet</url-pattern>
</servlet-mapping>
'개인 공부방 > JSP' 카테고리의 다른 글
JSP로 만들기 (0) | 2011.12.08 |
---|---|
이클립스 JSP 백업하기 (0) | 2011.12.07 |
자바-eclipse 에서 html 설정 및 기본실행 (0) | 2011.12.07 |
서블릿 servlet (0) | 2011.12.06 |
html 기본문법2 (0) | 2011.12.06 |