WEB/JSP

5. Servlet예제 - doGet, doSet

choi121xx 2013. 4. 18. 14:12

- 서블릿 사용 시 service메서드가 아닌  doGet, doSet 메소드 사용 예


- GET방식으로 요청이 전송되어 올 경우    doGet(HttpServletRequest req, HttpServletResponse resp)


- POST방식으로 요청이 전송되어 올 경우   doPost(HttpServletRequest req, HttpServletResponse resp)


- doGet, doPost를 따로 호출하여 사용해야 할때 사용한다.
단, service메소드가 오버라이딩되면 doGet,doPost메소드는 자동호출되지 않음


- 어노테이션 ( annotation )

java 5.0부터 지원되는 기술로, 기존 설정파일(web.xml)에서 제공하는 설정내용들을 설정파일에서 설정하지 않아도 해당 소스내에 설정방법을 제공하여 처리하는 기능

@WebServlet("/")



0. web.xml

1. main.html --> MyLoginServlet.java  ( <a>태그 Get방식으로 서블릿 전송 )

2. MyLoginServlet.java

3. Login.html --> LoginServlet.java ( submit 으로 Post방식으로 서블릿 전송 )

4. LoginServlet.java




[ web.xml ]


<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

id="WebApp_ID" version="3.0">

<display-name>day02_servlet01</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>login</servlet-name>

<servlet-class>test.servlet.LoginServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>login</servlet-name>

<url-pattern>/login.do</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>mylogin</servlet-name>

<servlet-class>test.servlet.MyLoginServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>mylogin</servlet-name>

<url-pattern>/mylogin.do</url-pattern>

</servlet-mapping>

</web-app>


[main.html]

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<a href="mylogin.do">로그인하기</a>
</body>
</html>




[ MyLoginServlet.java ]


package test.servlet;

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 MyLoginServlet extends HttpServlet{


    //GET방식으로 요청시 호출->로그인페이지로 이동하기
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //페이지이동하기
        resp.sendRedirect("login.html");
    }


    //POST방식으로 요청시 호출->로그인처리
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setCharacterEncoding("euc-kr");
        String id=req.getParameter("id");
        String pwd=req.getParameter("pwd");
        resp.setContentType("text/html;charset=euc-kr");
        //응답하기위한 출력스트림 얻어오기
        PrintWriter pw=resp.getWriter();
        pw.println("<html>");
        pw.println("<head></head>");
        pw.println("<body>");
        if(id.equals("song") && pwd.equals("1234")){
            pw.println(id+"님 로그인 되셨습니다.");
        }else{
            pw.println("아이디 또는 비밀번호가 맞지 않습니다.");
        }
        pw.println("</body>");
        pw.println("</html>");
        pw.close();   
       
    }
}




[log.html]


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="mylogin.do">
아이디 <input type="text" name="id"/><br/>
비밀번호 <input type="text" name="pwd"/><br/>
<input type="submit" value="로그인"/>
</form>
</body>
</html>





Ex 2> 

[ LoginServlet.java ]


- service메소드는 get이든 post방식이든 무조건 호출됨.

- service메소드가 override되면 doGet, doPost는 호출되지 않는다.



package test.servlet;

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 LoginServlet extends HttpServlet{
    //GET방식이든 POST방식이든 무조건 호출됨
    //service메소드가 오버라이딩되면 doGet,doPost메소드는 자동호출되지 않음
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html;charset=euc-kr");
        //응답하기위한 출력스트림 얻어오기
        PrintWriter pw=resp.getWriter();
        pw.println("<html>");
        pw.println("<head></head>");
        pw.println("<body>");
        pw.println("<h2>Service메소드 호출!</h2>");
        pw.println("</body>");
        pw.println("</html>");
        pw.close();   
    }
    //get방식으로 요청되었을때 호출됨
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html;charset=euc-kr");
        //응답하기위한 출력스트림 얻어오기
        PrintWriter pw=resp.getWriter();
        pw.println("<html>");
        pw.println("<head></head>");
        pw.println("<body>");
        pw.println("<h2>GET방식으로 요청되었어요!</h2>");
        pw.println("</body>");
        pw.println("</html>");
        pw.close();   
    }
    //post방식으로 요청되었을때 호출됨
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html;charset=euc-kr");
        //응답하기위한 출력스트림 얻어오기
        PrintWriter pw=resp.getWriter();
        pw.println("<html>");
        pw.println("<head></head>");
        pw.println("<body>");
        pw.println("<h2>POST방식으로 요청되었어요!</h2>");
        pw.println("</body>");
        pw.println("</html>");
        pw.close();   
    }
}