Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

Everything has an expiration date

★★★ [JSP & Servlet] 20231207 [프로그램소스 (JSP → Servlet)] - jsptest005, web.xml, Test005 본문

[JSP & Servlet]/Program source (JSP & Servlet)

★★★ [JSP & Servlet] 20231207 [프로그램소스 (JSP → Servlet)] - jsptest005, web.xml, Test005

Jelly-fish 2023. 12. 7. 13:44

 

 

WebApp04


 

jspTest005.jsp

 

 

jspTest005.jsp

 

<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsptest005.jsp</title>

<link rel="stylesheet" type="text/css" href="css/main.css">


<!--
	  <form> : 데이터 전송 및 페이지 요청
   			  이를 JSP에서 이용하여
 			  Servlet 에 전달. (submit)
 			  
 	  JSP 에서 아이디/패스워드를 <form></form> 태그에 넣고
 	  Servlet 에 전달하여 뒷 서버 처리를 수행하도록 해보자.
 	  
 	  Server에서의 식별 : name 이다.
 	  Script 제어였다면, Id 도 썼겠지만, 현재는 서버에서만 처리하므로...
 	  
 	  입력 컨트롤을 서버에 전달하려면, 반드시 <form> 태그 안에 넣어주어야 한다.
 -->

</head>
<body>

<div>
	<h1>JSP 관찰하기</h1>
	<hr>
</div>

<div>
	<h2>HttpServlet 관련 실습</h2>
	
		<!--  [개인 필기]  -->
		<!--================================================================================================-->
		<!-- (action 속성을 통해 데이터를 어디로 전달할 것인지를 명시해 주어야 한다.) -->
		<!-- (action="" 과 같이 액션 속성을 기재하지 않으면, 나 자신을 의미하는 것이다. 나에게 전달.) -->
		<!-- 데이터 전송 : get(), post() 
					       ◎ get()  : 엽서에 데이터 적어서 보내기. 어떤 내용인지 어디로 가는지 확인가능
					       ◎ post() : 편지에 데이터를 넣어서 보내는 방식. 어디로 가는지만 확인 가능.
			 
			 【어떤 방식으로 보낼지 결정 → method 속성.】
			 
			 action : 폼 내부에 데이터를 보내는 목적지 url을 지정한다.

			 submit 액션 : 페이지에 요청을 보내는 방식이다.
			 Submit · <form> 태그의 모든 요소를 서버측으로 보낸다는 뜻
			 
		-->
		<!--================================================================================================-->


		<!-- ※ 입력 컨트롤을 활용하여 서버로 데이터를 전송하기 위해서는 form 필요 -->
		<!-- ※ form 태그의 action 속성은
		  		데이터 전송 및 페이지 요청을 해야하는
		   		대상 페이지를 등록하는 기능을 수행한다. 
		   		(생략 시 데이터 전송 및 페이지 요청을 하게 되는 대상은 자기 자신) -->
		<!-- ※ form 태그의 method 속성은 데이터 전송 및 페이지 요청에 대한 방식
			    (생략 시 데이터 전송 및 페이지 요청을 하게되는 방식은 get) -->
		
		<form action="/WebApp04/login" method="get">
			<table>
				<tr>
					<th>아이디</th>
					<td>
						<!-- ※ name 속성 check~!!! -->
						<input type="text" name="userId" size="10" maxlength="10" class="txt">
					</td>
				</tr>
				<tr>
					<th>패스워드</th>
					<td>
						<!-- ※ name 속성 check~!!! -->
						<input type="password" name="userPwd" size="10" class="txt">
						
					</td>
				</tr>
				<tr>
					<td colspan="2" style="text-align: center;">
						<!-- ※ submit 액션 → form 데이터 전송 및 페이지 요청 -->
						<input type="submit" value="로그인" class="btn control"
						style="width: 48%">
						<input type="reset" value="다시입력" class="btn control"
						style="width: 48%">
					</td>
				</tr>
			</table>
		</form>
	
	
	
</div>

</body>
</html>

 


 

 

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_3_1.xsd" version="3.1">
  <display-name>WebApp04</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
  		<servlet-name>Test004</servlet-name>
  		<servlet-class>com.test.Test004</servlet-class>
  </servlet>
  
  <servlet-mapping>
  		<servlet-name>Test004</servlet-name>
  		<url-pattern>/test004</url-pattern>
  </servlet-mapping>
  
  <servlet>
  		<servlet-name>Test005</servlet-name>			<!-- 사이트 이름 -->
  		<servlet-class>com.test.Test005</servlet-class>	<!-- 기능해야 하는 클래스 -->
  </servlet>
  
  <servlet-mapping>
  		<servlet-name>Test005</servlet-name>			<!-- 사이트 이름(<servlet>태그와 동일) -->
  		<url-pattern>/login</url-pattern>				<!-- 사용자가 해당 url을 입력했을 때 연결 -->
  </servlet-mapping>
    
</web-app>

 

 


 

 

Test005.java

 

 

 

Test005.java

/*========================
 	Test005.java
 	- Servlet 관련 실습
=========================*/

// 현재... 자바의 기본 클래스 Test005
// 이를... Servlet 으로 구성하는 방법

// HttpServlet 을 상속받는 클래스로 설계 → Servlet

package com.test;

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;

// Test004.java (Servlet) 에서는
// GenericServlet 을 extends 를 상속하자마자 에러가 났었지만,
// HttpServlet 을 extends 하겠다고 했을 땐, 에러가 나지 않았다.
// ★ 추상 클래스이지만 추상 메소드를 갖고 있지 않은 것이다!!


// [GenericServlet] → service() Overriding
// [HttpServlet] → doGet(), doPost() Overriding



public class Test005 extends HttpServlet
{
	// ◇ serialVersionUID 를 추가하여 경고를 없앤다.
	private static final long serialVersionUID = 1L;

	
	// jsptest005 에서 <form> 태그의 method 타입이 무엇이느냐에 따라
	// 사용자의 요청 (submit)이 달라지므로
	// doGet() 메소드와, doPost() 메소드 중 하나로
	// Servlet Container 의 메소드 호출이 달라진다.
	
	
	// 사용자의 http 프로토콜 요청이 GET 방식일 경우 호출되는 메소드
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		// ※ 어떤 방식의 요청이 들어오더라도 일괄 처리하도록 구성한 것.
		doGetPost(request, response);
		
	}

	// 사용자의 http 프로토콜 요청이 POST 방식일 경우 호출되는 메소드
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		// ※ 어떤 방식의 요청이 들어오더라도 일괄 처리하도록 구성한 것.
		doGetPost(request, response);
		
	}
	
	// 사용자 정의 메소드
	protected void doGetPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		// GET 방식이든 POST 방식이든
		// 어떤 방식의 요청에도 모두 처리할 수 있는 사용자 정의 메소드
		
		// request → 요청 객체
		// response → 응답 객체
		String id = request.getParameter("userId");
		String pw = request.getParameter("userPwd");
		
		response.setContentType("text/html; charset=UTF-8");
		
		String str = "아이디 : " + id + ", 패스워드 : " + pw;
		
		// 응답을 출력 스트림으로 구성하기 위한 준비
		
		PrintWriter out = response.getWriter();
		
		// 출력 스트림을 활용해서 페이지 렌더링
		out.print("<!DOCTYPE html>");
		out.print("<html>");
		out.print("<head>");
		out.print("<meta charset=\"UTF-8\">");
		out.print("<title>Test005.java</title>");
		out.print("</head>");
		out.print("<body>");
		out.print("");
		out.print("<div>");
		out.print("	<h1>서블릿 관련 실습</h1>");
		out.print("</div>");
		out.print("");
		out.print("<div>");
		out.print("	<h2>HttpServlet 클래스를 활용한 서블릿 테스트</h2>");
		out.print("	");
		out.print("	<p>" + str + "</p>");
		out.print("</div>");
		out.print("");
		out.print("</body>");
		out.print("</html>");

		
		//===================================================
		// getParameter()
		// 요청 객체 안에 담겨있는 내용을 얻을 수 있다.
		// getParameter("userId")
		// name="userId" 에 해당하는 객체의 내용을 얻어온다.
		
	}
	
}