Annotation을 이용한 서블릿 매핑

Chris·2019년 8월 20일
0

http://localhost:8080/pro06/login.html로 html 페이지 확인
Login 버튼을 클릭후 전송된 데이터 url로 확인 가능하다.
http://localhost:8080/pro06/login?user_id=lee&user_pw=1233

WebContent/login.html 작성

<form name="frmLogin" method="get" action="login" encType="UTF-8">
	<div><label for="idIpt">id:</label><input type="text" name="user_id"></div>
	<div><label for="pwdIpt">pw:</label><input type="password" name="user_pw"></div>
	<input type="submit" value="Login"><input type="reset" value="Reset">
</form>

LoginServlet.java 작성

package sec01.ex01;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// annotation: WebServlet
@WebServlet("/login") 
public class LoginServlet  extends HttpServlet {
   public void init() throws ServletException {
      System.out.println("call init()");
   }

   protected void doGet(
		HttpServletRequest req, 
		HttpServletResponse res
   ) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8"); 	
		String user_id = req.getParameter("user_id");
		String user_pw = req.getParameter("user_pw"); 
		System.out.println("id: " + user_id);
		System.out.println("pw:" + user_pw);
   }

   public void destroy() {
		System.out.println("call destroy()");
   }
}

Tomcat의 server.xml에 추가

<Context docBase="pro06" path="/pro06" reloadable="true" source="org.eclipse.jst.jee.server:pro06"/>

위 작성글은 [자바 웹을 다루는 기술]에서 간단히 정리한 것입니다.

profile
올드보이 코더

0개의 댓글