본문 바로가기

Java/java 기초

Servlet 기초 응용

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
</head>

<body>
    <!-- index.html
name text
age select
성별 radio
취미 check

HelloServlet.java
Dto name age 취미

Dto <- name age 취미 (attribute)

-- 남성 옷사이트로 이동
Dto <- name age 취미 -> session.setAttribute
response.sendRedirect

-- 여성 옷사이트로 이동
Dto <- name age 취미 -> request.setAttribute

forward


정보출력
ManServlet.java
WomanServlet.java
 -->
    <h2>남녀 옷 사이트 연결 사이트</h2>
    <form action="hello" method="GET">
        <p>이름: <input type="text" name="name"> </p>
        <p>
            나이: <select name="age" id="">
        <option value="ten">10대</option>
        <option value="twenty">20대</option>
        <option value="thirty">30대</option>
        <option value="forty">40대</option>
      </select>
        </p>
        남자:<input type="radio" name="gender" value="man"> 여자:
        <input type="radio" name="gender" value="woman">
        <br>
        <br>
        <div>
            게임: <input type="checkbox" name="hobby" value="game"> 쇼핑: <input type="checkbox" name="hobby" value="shopping"> 영화보기: <input type="checkbox" name="hobby" value="movie">
        </div>
        <input type="submit">
    </form>
</body>

</html>

 

DTO 생성하기 

 

package dto;

import java.util.Arrays;

public class InformationDto {
	private String name;
	private String age;
	private String[] hobby;
	
	public InformationDto() {
		
	}

	public InformationDto(String name, String age, String[] hobby) {
		super();
		this.name = name;
		this.age = age;
		this.hobby = hobby;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String[] getHobby() {
		return hobby;
	}

	public void setHobby(String[] hobby) {
		this.hobby = hobby;
	}

	@Override
	public String toString() {
		return "InformationDto [name=" + name + ", age=" + age + ", hobby=" + Arrays.toString(hobby) + "]";
	}
	
	
	
	
	
}

 

HelloServlet

 

package work001;

import java.io.IOException;
import java.io.PrintWriter;

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 javax.servlet.http.HttpSession;

import dto.InformationDto;

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		String hobby[] = req.getParameterValues("hobby");

		InformationDto informationDto = new InformationDto(name, age, hobby);

		resp.setContentType("text/html; charset=utf-8");
		

		if (gender.equals("man")) {
			HttpSession session = req.getSession();
			session.setAttribute("informationDto", informationDto);
			resp.sendRedirect("man");

		} else if (gender.equals("woman")) {
			req.setAttribute("informationDto", informationDto);
			RequestDispatcher requestDispatcher = req.getRequestDispatcher("woman");

			requestDispatcher.forward(req, resp);
		}

		PrintWriter pw = resp.getWriter();

		pw.println("<html>");

		pw.println("<head>");
		pw.println("<title>제목</title>");
		pw.println("</head>");

		pw.println("<body>");

		pw.println("<p>Hello Servlet</p>");
		// pw.println("<p>" + informationDto.toString() + "</p>");

		// pw.println("<a href='woman'>여성 옷 사이트</a>");

		pw.println("</body>");

		pw.println("</html>");

		pw.close();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

	}

}

 

ManServlet 

package work001;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

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 javax.servlet.http.HttpSession;

import dto.InformationDto;

@WebServlet("/man")
public class ManServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html; charset=utf-8");
		PrintWriter pw = resp.getWriter();
		
		
		pw.println("<html>");

		pw.println("<head>");
		pw.println("<title>제목</title>");
		pw.println("</head>");
		
		pw.println("<body>");
		
		pw.println("<p>Man Servlet</p>");
		HttpSession session = req.getSession();
		InformationDto informationDto = (InformationDto)session.getAttribute("informationDto");
		pw.println("<p>" + informationDto.toString() +"</p>");
		
		pw.println("</body>");

		pw.println("</html>");

		pw.close();
	}
	
}

WomanServlet

 

package work001;

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;

import dto.InformationDto;

@WebServlet("/woman")
public class WomanServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html; charset=utf-8");
		PrintWriter pw = resp.getWriter();
		
		InformationDto informationDto = (InformationDto) req.getAttribute("informationDto");
		
		pw.println("<html>");

		pw.println("<head>");
		pw.println("<title>제목</title>");
		pw.println("</head>");
		
		pw.println("<body>");
		pw.println("<p>+"+ informationDto.toString() +"</p>");
		
		pw.println("<p>Woman Servlet</p>");
		
		
		pw.println("</body>");

		pw.println("</html>");

		pw.close();
	}

}

 

첫 화면

 

 

성별이 남자일때 클릭했을때 

 

성별이 여자일때

 

'Java > java 기초' 카테고리의 다른 글

jsp 내장객체  (0) 2020.01.20
jsp 기초  (0) 2020.01.20
Servlet session 넘기기및 삭제  (0) 2020.01.17
Servlet 쿠키값 넘기기  (0) 2020.01.17
servlet a 태그 받아오기  (0) 2020.01.17