본문 바로가기

servlet

url pattern 실습

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

testa.jsp 입니다

</body>

</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

testb.jsp입니다

</body>

</html>


 


controllerEx01

jsp를 직접 호출하지 않고 서블릿으로 호출한다.

url pattern 방식

 

package servlet;

 

import java.io.IOException;

import java.io.PrintWriter;

import java.io.UnsupportedEncodingException;

 

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

/**

 * Servlet implementation class ControllerEx01

 */

@WebServlet("/board/*")

public class ControllerEx01 extends HttpServlet {

     private static final long serialVersionUID = 1L;

      

    /**

     * @see HttpServlet#HttpServlet()

     */

    public ControllerEx01() {

        super();

        // TODO Auto-generated constructor stub

    }

 

     /**

      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

      */

     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

           // TODO Auto-generated method stub

           doProcess(request, response);

     }

 

     /**

      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

      */

     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

           // TODO Auto-generated method stub

           doProcess(request, response);

     }

 

     protected void doProcess(HttpServletRequest request, HttpServletResponse response){

           try {

                request.setCharacterEncoding("utf-8");

                          

                String path = request.getRequestURI().replaceAll(request.getContextPath(), "");

               

                String url ="";

                if(path.equals("/board/*")||path.equals("/board/testa")){

                     url = "/testa.jsp";

                } else if(path.equals("board/testb")){

                     url = "/testb.jsp";

                } else {

                    

                }

                //Servlet에서 원하는 jsp 페이지를 호출해다 전송시키는 객체

                if(!url.equals("")){

                     System.out.println(url);

                     RequestDispatcher dispatcher = request.getRequestDispatcher(url);

                     dispatcher.forward(request, response);

                }

          

                          

           } catch (UnsupportedEncodingException e) {

                // TODO Auto-generated catch block

                System.out.println(e.getMessage());

           } catch (IOException e) {

                // TODO Auto-generated catch block

                System.out.println(e.getMessage());

           } catch (ServletException e) {

                // TODO Auto-generated catch block

                System.out.println(e.getMessage());

           }

     }

}

 

 

'servlet' 카테고리의 다른 글

Parameter 방식  (0) 2013.03.15