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"
id="WebApp_ID" version="3.1">
<display-name>WebApp0004</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>testServlet006</servlet-name> <!-- 3.내용을 찾음 -->
<servlet-class>com.test.Test006</servlet-class> <!-- 서비스 호출 -->
</servlet>
<servlet-mapping>
<servlet-name>testServlet006</servlet-name> <!-- 2.위에 servlet과 반드시 이름이 같아야함 -->
<url-pattern>/test006</url-pattern> <!-- 1.요청이 들어옴 -->
</servlet-mapping>
<servlet>
<servlet-name>testServlet005</servlet-name> <!-- 3.내용을 찾음 -->
<servlet-class>com.test.Test005</servlet-class> <!-- 서비스 호출 -->
</servlet>
<servlet-mapping>
<servlet-name>testServlet005</servlet-name> <!-- 2.위에 servlet과 반드시 이름이 같아야함 -->
<url-pattern>/login</url-pattern> <!-- 1.요청이 들어옴 -->
<!-- Test005.jsp 페이지에서 form action="/WebApp0004/login"이기 때문 -->
</servlet-mapping>
<!-- 이 2쌍이 구조이다. servlet-name : 변수명 -->
<!-- ★★★★★★ web.xml을 수정하고 나면 반드시 서버 restart해주기 !! ★★★★★ -->
</web-app>
Test005.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<div>
<h1>JSP 관찰하기</h1>
<hr>
</div>
<div>
<h2>HttpServlet 관련 실습</h2>
<form action="/WebApp0004/login" method="get"> <!-- action속성은 데이터를 전송하면서 요청할 페이지를 말함
method는 데이터를 전송 및 페이지 요청 방식에 해당 즉, (1)get (2)post 를 등록 방식이 있다
get = 엽서 post = 편지 라고 생각하기 .(생략시 get)
-->
<table>
<tr>
<th>아이디</th>
<td>
<input type="text" name="userId" class="txt"
size="10" maxlength="10">
</td>
</tr>
<tr>
<th>패스워드</th>
<td>
<input type="password" name="userPwd" class="txt" size="10">
</td>
</tr>
<tr style="height:50px;">
<td colspan="2">
<input type="submit" value="로그인" class="btn" style="width:90px;">
<input type="reset" value="다시입력" class="btn" style="width:90px;">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
Test005.java (로그인을 클릭하면 정보를 화면에 뿌려주는 부분)
/*===========================
Test005.java
- Servlet 실습
============================*/
//HttpServlet 을 상속받는 클래스로 전환 → Servlet 생성
//Servlet의 역할을 수행하려면 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;
public class Test005 extends HttpServlet
{
private static final long serialVersionUID = 1L;
//get방식 처리 내용 (자동호출)
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGetPost(request, response);
}
//post방식 처리 내용 (자동호출)
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGetPost(request, response);
}
//get 방식의 요청이든 post 방식의 요청이든 둘다 처리할 수 있는 사용자 정의 메소드 구성 ( 그럼 한번만 코딩하면 됨 )
protected void doGetPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// 한글 깨짐 방지
request.setCharacterEncoding("UTF-8");
// 이전 페이지로부터 데이터 수신
String userId = request.getParameter("userId");
String userPwd = request.getParameter("userPwd");// 여기서 " "안의 내용은 name 값임
response.setContentType("text/html; charset=UTF-8");
String str = "아이디 : " + userId + ", 패스워드 : " + userPwd;
PrintWriter out = response.getWriter();
out.print("<html>");
out.print("<head>");
out.print("<title>");
out.print("Test005.java");
out.print("</title>");
out.print("</head>");
out.print("<body>");
out.print("<div>");
out.print("<h1>");
out.print("서블릿 관련 실습");
out.print("</h1>");
out.print("<hr>");
out.print("</div>");
out.print("<div>");
out.print("<h2>HttpServlet 클래스를 활용한 서블릿 테스트</h2>");
out.print("<p>" + str + "</p>");
out.print("<p>" + "method : " + request.getMethod() + "</p>");
out.print("<p>" + "클라이언트 IP Address : " + request.getRemoteAddr() + "</p>");
out.print("<p>" + "URI : " + request.getRequestURI() + "</p>");
out.print("</div>");
out.print("</body>");
out.print("</html>");
}
}