Dynamic Web Project: testServlet3
Folder: person
File: person .html
Package: com.person
Servlet: PersonServlet.java
http://localhost:8080/testServlet3/person/person.html 요청
↓ 서블릿으로 요청
http://localhost:8080/testServlet3/person-servlet
<form action="testServlet3/person">
<form action="/testServlet3/person"> -- /가 꼭 들어가야한다.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>testServlet2</display-name>
<servlet>
<servlet-name>PersonServlet</servlet-name>
<servlet-class>com.person.PersonServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PersonServlet</servlet-name>
<url-pattern>/person</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
</web-app>
person.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="http://localhost:8080/testServlet3/person">
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<th width="70">이름</th>
<td>
<input type="text" name="name" size="20" placeholder="이름입력"/>
</td>
</tr>
<tr>
<th>성별</th>
<td><!-- radio는 배열로 보내기 때문에 0, 1로 보내는게 좋다. -->
<input type="radio" name="gender" value="남자" checked/>남자
<input type="radio" name="gender" value="여자"/>여자
</td>
</tr>
<tr>
<th>색깔</th>
<td>
<select name="color" style="width: 100px;">
<optgroup label="색깔">
<option value="red" selected="selected">빨강</option>
<option value="green">초록</option>
<option value="blue">파랑</option>
<option value="magenta">보라</option>
<option value="cyan">하늘</option>
</optgroup>
</select>
</td>
</tr>
<tr>
<th>취미</th>
<td>
<label>
<input type="checkbox" name="hobby" value="독서"/>독서
</label>
<label>
<input type="checkbox" name="hobby" value="영화"/>영화
</label>
<label>
<input type="checkbox" name="hobby" value="음악"/>음악
</label>
<label>
<input type="checkbox" name="hobby" value="게임"/>게임
</label>
<label>
<input type="checkbox" name="hobby" value="운동"/>운동
</label>
</td>
</tr>
<tr>
<td align="center">과목</td>
<td colspan="2">
<label>
<select name="subject" multiple size="6" style="width: 120px;">
<option value="JAVA">JAVA</option>
<option value="Servlet">Servlet</option>
<option value="HTML" selected="selected">HTML</option>
<option value="Spring">Spring</option>
</select>
</label>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="SEND"/>
<input type="reset" value="CANCEL"/>
</td>
</tr>
</table>
</form>
</body>
</html>
PersonServlet.java
package com.person;
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 PersonServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//데이터 받기
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String color = request.getParameter("color");
String[] hobby = request.getParameterValues("hobby");
String[] subject = request.getParameterValues("subject");
//응답
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<ul>");
out.println("<li>");
out.println("<font color='" + color + "'>이름 : " + name + "</font>");
out.println("</li>");
out.println("<li>");
out.println("<font color='" + color + "'>성별 : " + gender + "</font>");
out.println("</li>");
out.println("<li>");
out.println("<font color='" + color + "'>색깔 : " + color + "</font>");
out.println("</li>");
out.println("<li>");
out.print("<font color='" + color + "'>취미 :</font>");
for(int i=0; i<hobby.length; i++) {
out.println("<font color='" + color + "'> " + hobby[i] + "</font>");
}
out.println("</li>");
out.println("<li>");
out.print("<font color='" + color + "'>과목 :</font>");
for(int i=0; i<subject.length; i++) {
out.println("<font color='" + color + "'> " + subject[i] + "</font>");
}
out.println("</li>");
out.println("</ul>");
out.println("</body>");
out.println("</html>");
}
}
font color를 ul에 주는 것이 편하다 !!
package com.person;
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 PersonServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//데이터 받기
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String color = request.getParameter("color");
String[] hobby = request.getParameterValues("hobby");
String[] subject = request.getParameterValues("subject");
//응답
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<ul style='color: " + color + ";'>");
out.println("<li>");
out.println("이름 : " + name);
out.println("</li>");
out.println("<li>");
out.println("성별 : " + gender);
out.println("</li>");
out.println("<li>");
out.println("색깔 : " + color);
out.println("</li>");
out.println("<li>");
out.print("취미 : ");
for(int i=0; i<hobby.length; i++) {
out.println(hobby[i]);
}
out.println("</li>");
out.println("<li>");
out.print("과목 : ");
for(int i=0; i<subject.length; i++) {
out.println(subject[i]);
}
out.println("</li>");
out.println("</ul>");
out.println("</body>");
out.println("</html>");
}
}
if(hobby != null) {
for(int i=0; i<hobby.length; i++) {
out.println(hobby[i]);
}
}else {
out.println("선택한 취미가 없습니다.");
}
hobby는 hobby.length 때문에 문제가 생기므로 if문 걸어야한다.
과목은 selected가 있기 때문에 if 문 걸지 않아도 상관없다.
for(int i=0; i<subject.length; i++) {
out.println(subject[i]);
}
for(String data : subject) {
out.println(data);
}
밑에 방식으로 적어도 된다.
'HOMEWORK' 카테고리의 다른 글
DAY 47 - 미니프로젝트(member) HOMEWORK - 글목록 보기 (2024.09.06) (0) | 2024.09.09 |
---|---|
DAY 45 - JSP 미니프로젝트(member) HOMEWORK - 중복체크 / 회원가입 (2024.09.04) (0) | 2024.09.04 |
DAY 33 - JS HOMEWORK - 덧셈문제 (2024.08.19) (0) | 2024.08.22 |
DAY 35 - JS HOMEWORK - 2단~9단 / 연인날짜계산 / 로또 (0) | 2024.08.21 |
DAY 34 - JS HOMEWORK - 가위바위보 게임 (2024.08.20) (0) | 2024.08.20 |