2022022_Fri
jst ->servlet -> jsp -> servlet -> jsp...
1.jsp에서 페이지 이동하는 방법
-a태그
-form태그
2.jsp에서 데이터 넘기는 방법
3.servlet에서 페이지 이동하는 방법
4.servlet에서 데이터 넘기는 방법
클래스 만들기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request. setCharacterEncoding("UTF-8");
response.setContentType("text.html;charset=UTF-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="regStudent" method="post">
이름 :<input type="text" name="name"><br>
나이 :<input type="text" name="age"><br>
학년 :<input type="text" name="grade"><br>
국어점수 :<input type="text" name="score"><br>
<input type="submit" value ="등록">
</form>
</body>
</html>
2.Class 파일 만들기
public class StudentDTO {
//leg_student.jsp파일 내 name변수명을 반드시 맞춰야 함.
private String name;
private int age;
private String grade;
private int score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
3.servlet 만들기
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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("/regStudent")// servlet명 일치시켜주기
public class stuServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public stuServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request. setCharacterEncoding("UTF-8");
response.setContentType("text.html;charset=UTF-8");
//(문자열)데이터 받아오기
String name = request.getParameter("name");
// String age= request.getParameter("age");
String grade = request.getParameter("grade");
// String score = request.getParameter("score");
// 정수로 변수받은 데이터 문자로 전환해주기.
int score = Integer.parseInt(request.getParameter("score"));
int age = Integer.parseInt(request.getParameter("age"));
//-> age라는 이름으로 데이터를 받는데 이를 정수형으로 바꿔서 받겠따.
//데이터 보내주기
//위에 4개의 데이터를 가질 보따리 하나 만들기(객체생성)
StudentDTO student = new StudentDTO();
student.setName(name);
student.setGrade(grade);
student.setScore(score);
student.setAge(age);
request.setAttribute("stu", student);
RequestDispatcher dispatcher = request.getRequestDispatcher("student_show.jsp");
//student_show.jsp 로 이동하겠다.
dispatcher.forward(request, response);
//밑에 두줄을 한 줄로 만들어버리기.
//String age= request.getParameter("age");
//int age1 = Integer.parseInt(age);
}
}
4. jsp파일 만들기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request. setCharacterEncoding("UTF-8");
response.setContentType("text.html;charset=UTF-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--클래스에 담긴 데이터는 (.)을 사용하면 된다. -->
<!-- stu.getName(); 을 호출하는 것과 사실상 같다. -->
<!-- getter/setter 설정 중요하다!!! 특히 이름을 잘 맞춰야한다. -->
${stu.name }<br>
${stu.grade }<br>
${stu.age }<br>
${stu.score }
</body>
</html>
[결과]
Insert title here 이름 :배달 주문 프로그램만들기
jsp -> servlet(주문정보/doPost()메소드 이용) ->jsp -> servlet(주문정보,배달정보/doPost()메소드 이용) ->jsp -> servlet
jsp 파일 3개 + 클래스 파일 2개 + servlet 파일 3개 만들기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request. setCharacterEncoding("UTF-8");
response.setContentType("text.html;charset=UTF-8");
%>
추가할게요(추가시 1000원)
양념 추가
머스타드 추가
콜라 추가
요청사항
<%
request. setCharacterEncoding("UTF-8");
response.setContentType("text.html;charset=UTF-8");
%>
<input type ="hidden" value ="${orders.chicken}" name = "chicken">
<input type ="hidden" value ="${orders.number}" name = "number">
<input type ="hidden" value ="${orders.request}" name = "request">
<!-- jsp사용하기 -->
<!--양념추가 머스타드추가 콜라추가 -->
<c:forEach var="extra" items="${orders.extra }">
<input type= "hidden" value= "${extra }" name="extra">
<!-- hidden으로 option데이터를 던지겠다 -->
</c:forEach>
<div class = "container">
<div class = "head"><h1>배달 정보</h1></div>
<div class = "body">
<div>주문자</div>
<div>
<input type="text" name="name" >
</div>
<div>연락처</div>
<div>
<input type="text" name="phone" placeholder="010-0000-0000형태로 입력" >
</div>
<div>배달 주소</div>
<div>
<input type="text" name="address" >
</div>
</div>
<div class= "btn">
<input style="text-align: center;" type ="submit" value ="주문">
</div>
</div>
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dto.OrderDTO;
@WebServlet("/order")
public class ChickenOrder extends HttpServlet {
private static final long serialVersionUID = 1L;
public ChickenOrder() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request. setCharacterEncoding("UTF-8");
response.setContentType("text.html;charset=UTF-8");
OrderDTO order= new OrderDTO();
order.setChicken(request.getParameter("chicken"));
order.setNumber(Integer.parseInt(request.getParameter("number")));
order.setExtra(request.getParameterValues("extra")); //옵션은 여러개이기때문에
order.setRequest(request.getParameter("request"));
request.setAttribute("orders", order);
RequestDispatcher dispatcher = request.getRequestDispatcher("delivery.jsp");
dispatcher.forward(request, response);
}
}
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dto.DeliveryDTO;
import dto.OrderDTO;
@WebServlet("/deliverys")
public class Delivery extends HttpServlet {
private static final long serialVersionUID = 1L;
public Delivery() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request. setCharacterEncoding("UTF-8");
response.setContentType("text.html;charset=UTF-8");
String chicken =request.getParameter("chicken");
int number = Integer.parseInt(request.getParameter("number"));
String[] extra =request.getParameterValues("extra");
String req = request.getParameter("request");
int money = 10000* number;
OrderDTO orders = new OrderDTO(chicken,req,extra,money);
DeliveryDTO delivery = new DeliveryDTO();
delivery.setAddress("address");
delivery.setName("name");
delivery.setPhone("phone");
request.setAttribute("delivery", delivery);
request.setAttribute("orders", orders);
request.setAttribute("money", money);
RequestDispatcher dispatcher = request.getRequestDispatcher("order_info.jsp");
dispatcher.forward(request, response);
}
}
6.
package dto;
//Data Transfer Object
//데이터를 이동시켜주는 객체(데이터 전달목적)
//데이터 싣고나라는 트럭과 같다.
public class DeliveryDTO {
private String name;
private String phone;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public class OrderDTO {
private String chicken;
private String request;
private String[] extra;
private int number;
public OrderDTO() { }
public OrderDTO(String chicken, String request, String[] extra, int number) {
super();
this.chicken = chicken;
this.request = request;
this.extra = extra;
this.number = number;
}
public String getChicken() {
return chicken;
}
public void setChicken(String chicken) {
this.chicken = chicken;
}
public String getRequest() {
return request;
}
public void setRequest(String request) {
this.request = request;
}
public String[] getExtra() {
return extra;
}
public void setExtra(String[] extra) {
this.extra = extra;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
servlet 1개만 들어가는 여러jsp파일 만들기
MVCProject 프로젝트 생성
회원등록 프로그램 페이지
a.jsp 실행시켰을 때 결과 (콘솔창)
requestURI = /MVCProject/aaa.do
contextPath = /MVCProject
command = /aaa.do
b.jsp 실행시켰을 때 결과 (콘솔창)
requestURI = /MVCProject/bbb.do
contextPath = /MVCProject
command = /bbb.do
->알수있는 점
'command =' 뒤 코드를 보면 어떤페이지에서 왔냐에 따라서 구분지을 수 있다.