본문 바로가기

servlet

Parameter 방식

 

 

<%@ 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>

 

 

 

jsp를 직접 호출하지 않고 모두 서블릿으로 호출함 (parameter 방식) 


package Servlet;

 

import java.io.IOException;

 

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 action = request.getParameter("action");

               

                String url = "";

                if(action==null || action.equals("testa")){

                     url = "/testa.jsp";

                } else if(action.equals("testb")){

                     url = "/testb.jsp";

                }

               

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

                     RequestDispatcher dispatcher = request.getRequestDispatcher(url);

                     dispatcher.forward(request, response);

                }

           } catch (UnsupportedEncodingException e) {

                // TODO Auto-generated catch block

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

           } catch (ServletException e) {

                // TODO Auto-generated catch block

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

           } catch (IOException e) {

                // TODO Auto-generated catch block

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

           }

          

     }

    

}

 

'servlet' 카테고리의 다른 글

url pattern 실습  (0) 2013.03.15