본문 바로가기

Servlet

송장정보// 출력

 

 

index

<title>Insert title here</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</head>
<body>
<div style="border: 2px solid skyblue; margin-left : 20;" >
<form action="post" id="frm" method="post">
<h2>송부정보의 입력</h2>
이     름 :&nbsp; <input type="text" name="name" id="name"><br><br>
우편번호 :&nbsp; <input type="text" size="2" name="address1">
			-<input type="text" size="2" name="address2">
			<button type="button">주소변환</button>
			<br> <br>
주     소 :&nbsp; <textarea rows="3" cols="60" name="area" id="area"></textarea> <br><br>
전화번호 :&nbsp; <input type="text" size="2" name="tell1">
			-<input type="text" size="2" name="tell2">
			-<input type="text" size="2" name="tell3"><br><br>
배달시간 :&nbsp; <select name="time" multiple="multiple">
			<option value="지정하지 않음" selected="selected">지정하지 않음</option>
			<option value="10:00 ~ 12:00">10:00 ~ 12:00</option>
			<option value="12:00 ~ 15:00">12:00 ~ 15:00</option>
			<option value="15:00 ~ 17:00">15:00 ~ 17:00</option>
			<option value="17:00 ~ 20:00">17:00 ~ 20:00</option>
		</select><br><br><br><br>
영수증요청 :&nbsp; <input type="checkbox" name="che" value="요청"><br><br>
메일 매거진을 수신 :&nbsp; <input type="radio" name="rd" value="신청">신청
				<input type="radio" name="rd" value="신청하지 않음">신청하지 않음  <br>
<br>
<button id="send">확인화면으로 진행</button>
<br><br><br><br>
</form>
</div>

<script type="text/javascript">
$(document).ready(function() {
	
 	$('*').focus(function () {
 		$(this).css('background-color', '#00ff00');
	});
 	$('*').blur(function () {
		$(this).css('background-color', '#ffffff');
	}); 

	//버튼클릭
	$("#send").click(function () {
		//alert('send');
		$('#frm').submit();
	});
});
</script>
</body>

 

 

xml

  <servlet>
  <servlet-name>post</servlet-name>
  <servlet-class>sample01.postInput</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>post</servlet-name>
  <url-pattern>/post</url-pattern>
  </servlet-mapping>
  
   <servlet>
  <servlet-name>result</servlet-name>
  <servlet-class>sample01.view</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>result</servlet-name>
  <url-pattern>/result</url-pattern>
  </servlet-mapping>

 

postInput.java

public class postInput extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//이름
		
		req.setCharacterEncoding("utf-8");
		
//		String name = new String(req.getParameter("name").getBytes("8859_1"),"KSC5601");	
		String name = req.getParameter("name");
		//System.out.println("이름 : " +  URLEncoder.encode(name));
		System.out.println("이름 : " +  name);

		//우편번호
		String address1 = req.getParameter("address1");
		String address2 = req.getParameter("address2");
		System.out.println("우편번호 : "+address1+"-"+address2);

		//주소
		String area = req.getParameter("area");
		System.out.println("주소 : "+area);

		//전화번호
		String tell1 = req.getParameter("tell1");
		String tell2 = req.getParameter("tell2");
		String tell3 = req.getParameter("tell3");
		System.out.println("전화번호 : "+ tell1 + "-" + tell2 + "-" + tell3);

		//배달시간 
		String time[] = req.getParameterValues("time");
		for(int i=0; i<time.length; i++){
			System.out.println("배달시간 : "+time[i]);
		}

		//영수증
		String che = req.getParameter("che");
		System.out.println("영수증요청여부 : "+ che);

		//매거진
		String rd[] = req.getParameterValues("rd");
		for(int i=0; i<rd.length; i++){
			System.out.println("매거진 수신여부 : "+ rd[i]);
			
		}	
		//데이터 모으기
		PostDto dto = new PostDto(name, address1, address2, area, tell1, tell2, tell3, time, che, rd);
			
		//데이터 취합
		req.setAttribute("view", dto);
		
		req.getRequestDispatcher("result").forward(req, resp);
	}
}

 

view.java

public class view extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
				
		PostDto dto = (PostDto) req.getAttribute("view");
		System.out.println(dto.toString());
		
		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("<h1>입력하신 송장 정보입니다</h1>");
		
		pw.println("이름:<input type='text' value="+dto.getName()+"><br><br>");
		pw.println("우편번호:<input type='text' value="+dto.getAddress1()+">");
		pw.println("  -  <input type='text' value="+dto.getAddress2()+"><br><br>");
		
		pw.println("주소:<input type='text' value="+dto.getArea()+"><br><br>");
		
		pw.println("전화번호:<input type='text' value="+dto.getTell1()+">");
		pw.println("  - <input type='text' value="+dto.getTell2()+">");
		pw.println("  - <input type='text' value="+dto.getTell3()+"><br><br>");
		
		String time[] = dto.getTime();
		for (int i = 0; i < dto.getTime().length; i++) {
			pw.println("배달시간 : "  +time[i]+"<br><br>");
		}
		
		pw.println("영수증요청:<input type='text' value="+dto.getChe()+"><br><br>");

		String radio[] = dto.getRd();
		for (int i = 0; i < dto.getRd().length; i++) {
			pw.println("매거진 수신여부 :  " +radio[i]);

		}
		pw.println("</body>");
		pw.println("</html>");		
		pw.close();
	}
}

 

 

PostDto.java

public class PostDto implements Serializable {

	private String name;
	private String address1;
	private String address2;
	private String area;
	
	private String tell1;
	private String tell2;
	private String tell3;
	
	private String time[];
	private String che;
	private String rd[];
	
	public PostDto(String name, String address1, String address2, String area, String tell1, String tell2, String tell3,
			String[] time, String che, String[] rd) {
		super();
		this.name = name;
		this.address1 = address1;
		this.address2 = address2;
		this.area = area;
		this.tell1 = tell1;
		this.tell2 = tell2;
		this.tell3 = tell3;
		this.time = time;
		this.che = che;
		this.rd = rd;
	}


	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress1() {
		return address1;
	}
	public void setAddress1(String address1) {
		this.address1 = address1;
	}
	public String getAddress2() {
		return address2;
	}
	public void setAddress2(String address2) {
		this.address2 = address2;
	}
	public String getArea() {
		return area;
	}
	public void setArea(String area) {
		this.area = area;
	}
	public String getTell1() {
		return tell1;
	}
	public void setTell1(String tell1) {
		this.tell1 = tell1;
	}
	public String getTell2() {
		return tell2;
	}
	public void setTell2(String tell2) {
		this.tell2 = tell2;
	}
	public String getTell3() {
		return tell3;
	}
	public void setTell3(String tell3) {
		this.tell3 = tell3;
	}
	public String[] getTime() {
		return time;
	}
	public void setTime(String[] time) {
		this.time = time;
	}
	public String getChe() {
		return che;
	}
	public void setChe(String che) {
		this.che = che;
	}
	public String[] getRd() {
		return rd;
	}
	public void setRd(String[] rd) {
		this.rd = rd;
	}
	@Override
	public String toString() {
		return "PostDto [name=" + name + ", address1=" + address1 + ", address2=" + address2 + ", area=" + area
				+ ", tell1=" + tell1 + ", tell2=" + tell2 + ", tell3=" + tell3 + ", time=" + Arrays.toString(time)
				+ ", che=" + che + ", rd=" + Arrays.toString(rd) + "]";
	}
}