상세 컨텐츠

본문 제목

jsp & 서블릿] web.xml 파라미터 갖고오기

관리X 과거글

by 까먹기전에 2015. 3. 5. 15:47

본문

반응형

=============================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"

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

id="WebApp_ID" version="2.5">

<display-name>uuu</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>

<!-- ㅡㅡㅡㅡㅡㅡㅡㅡㅡa.b.Test에서만 이 init-param을 사용할수 있음ㅡㅡㅡㅡㅡㅡㅡㅡ  -->

<servlet>


<description></description>

<display-name>Test</display-name>

<servlet-name>Test</servlet-name>

<servlet-class>a.b.Test</servlet-class>

<init-param>

<param-name>

zzz

</param-name>

<param-value>

          yyy

          </param-value>

</init-param> 

<load-on-startup>1</load-on-startup>

</servlet>

<!-- ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡdo주소를 만나면 a.b.Test1로 보냄ㅡㅡㅡㅡㅡㅡㅡㅡㅡ  -->

<servlet-mapping>

<servlet-name>Test</servlet-name>

<url-pattern>/Test</url-pattern>

</servlet-mapping>

<servlet>

<description></description>

<display-name>Test1</display-name>

<servlet-name>Test1</servlet-name>

<servlet-class>a.b.Test1</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Test1</servlet-name>

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

</servlet-mapping>

<!-- ㅡㅡㅡㅡㅡㅡㅡㅡ모든 jsp , 서블릿에서 이 param을 사용가능함ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ  -->

<context-param>

<param-name>

xxx

</param-name>

<param-value>

www

</param-value>

</context-param>

</web-app>


===================================서블릿======================================

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

// TODO Auto-generated method stub

 

request.setCharacterEncoding("EUC-KR");

response.setContentType("text/html; charset=EUC-KR");

ServletContext application=getServletContext(); 

application.getInitParameter("xxx"); // web.xml Context-param 을 불러옴 

ServletConfig config = getServletConfig();

config.getInitParameter("zzz");  // web.xml init-param을 불러옴

List<MemberBean> list = MemberDAOImpl.getInstance().selectMemberList();

RequestDispatcher rd = request.getRequestDispatcher("b.jsp"); // 넘어갈 주소를 인자값으로!! 

/*

* request 는 주소가 바뀌면 사라지기 때문에, ReqestDispatcher를 써서 forward를 써서 파라미터를 넘길수 있다. 

*/

request.setAttribute("list", list); //setAttribute를 이용해서 회원목록이 저장되어있는 list를 넘긴다.

rd.forward(request, response);    //주소변환없이 페이지 이동

}

관련글 더보기