다이나믹 프로젝트 생성
JSP 생성 : parameter.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>파라미터 전송</title>
</head>
<body>
<form action="parameter.do" method="get">
이름 : <input type="text" name="name"/><br>
취미 <br>
<input type="checkbox" name="hobby" value="축구"/>축구
<input type="checkbox" name="hobby" value="농구"/>농구
<input type="checkbox" name="hobby" value="야구"/>야구
<input type="checkbox" name="hobby" value="음악감상"/>음악감상
<input type="checkbox" name="hobby" value="독서"/>독서
<input type="submit" value="get방식 전송">
</form>
<br>
<form action="parameter.do" method="post">
이름 : <input type="text" name="name"/><br>
취미 <br>
<input type="checkbox" name="hobby" value="축구"/>축구
<input type="checkbox" name="hobby" value="농구"/>농구
<input type="checkbox" name="hobby" value="야구"/>야구
<input type="checkbox" name="hobby" value="음악감상"/>음악감상
<input type="checkbox" name="hobby" value="독서"/>독서
<input type="submit" value="post방식 전송">
</form>
</body>
</html>
서블릿 생성 : Parameter.java
package parameter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/parameter.do")
public class Parameter extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String[] hobby = request.getParameterValues("hobby");
System.out.println("get");
System.out.println(name);
for(String e : hobby) {
System.out.print(e + " ");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String[] hobby = request.getParameterValues("hobby");
System.out.println("post");
System.out.println(name);
for(String e : hobby) {
System.out.print(e + " ");
}
}
}
GET방식 요청
Post방식 요청
실행결과 한글이 깨진다.
post방식에서는 다음의 코드를 doPost()에 추가 하여야 함
request.setCharacterEncoding("utf-8");
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8"); //한글 처리를 위해 추가
String name = request.getParameter("name");
String[] hobby = request.getParameterValues("hobby");
System.out.println("post");
System.out.println(name);
for(String e : hobby) {
System.out.print(e + " ");
}
}
실행결과 아주 잘 됨
JSTL <c:forEach> <c:forToken> 태그 (0) | 2019.11.14 |
---|---|
JSTL <c:if> <c:choose> <c:when> <c:otherwise> (0) | 2019.11.14 |
JSTL <c:set> (0) | 2019.11.14 |
서블릿(Servlet) 생명주기(LifeCyle) (0) | 2019.11.10 |
서블릿(Servlet) : Dynamic Web module 2.5 와 3.1 (0) | 2019.11.10 |
댓글 영역