1. Servlet Parameter
서블릿 파라미터는 web.xml에 init param 서블릿이 초기화 될때 파라미터를 만들어주고 그것을 Servlet에서 서블릿이 만들어 질때 getInitParamater() 메서드로 불러와서 사용을 하는 것입니다.
web.xml에 init-param태그로 데이터를 저장한 것을 서블릿에서 config인 getServletConfig()메서드를 이용해서 가져오게 됩니다.
package com.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 ServletEx extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String adminId = getServletConfig().getInitParameter("adminId");
String adminPw = getServletConfig().getInitParameter("adminPw");
PrintWriter out = response.getWriter();
out.print("<h1>servlet에서 데이터 받기</h1>");
out.print("<p>adminId : " + adminId + "</p>");
out.print("<p>adminPw : " + adminPw + "</p>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>lec08Pjt001</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>servletEx</servlet-name>
<servlet-class>com.servlet.ServletEx</servlet-class>
<init-param>
<param-name>adminId</param-name>
<param-value>admin</param-value>
</init-param>
<init-param>
<param-name>adminPw</param-name>
<param-value>1234</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>servletEx</servlet-name>
<url-pattern>/se</url-pattern>
</servlet-mapping>
</web-app>
web.xml에 데이터를 저장해두었습니다.
이를 jsp가 아닌 servlet에서도 데이터를 받아올수 있을까요??
jsp에서는 config 객체를 사용했었는데요 servlet도 마찬가지로 config객체를 사용합니다.
2. Context Parameter
context parameter도 jsp에서의 application객체와 동일합니다.
<context-param>태그를 이용하여 데이터를 저장하면 어플리케이션 내에있는 모든 jsp 파일에서 데이터를 받아올수 있습니다.
package com.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 ServletEx extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String imgDir = getServletContext().getInitParameter("imgDir");
String testServerIp = getServletContext().getInitParameter("testServerIp");
PrintWriter out = response.getWriter();
out.print("<p>imgDir : " + imgDir + "</p>");
out.print("<p>testServerIp : " + testServerIp + "</p>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>lec08Pjt001</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>
<context-param>
<param-name>imgDir</param-name>
<param-value>/upload/img</param-value>
</context-param>
<context-param>
<param-name>testServerIp</param-name>
<param-value>127.0.0.1</param-value>
</context-param>
<servlet>
<servlet-name>servletEx</servlet-name>
<servlet-class>com.servlet.ServletEx</servlet-class>
<init-param>
<param-name>adminId</param-name>
<param-value>admin</param-value>
</init-param>
<init-param>
<param-name>adminPw</param-name>
<param-value>1234</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>servletEx</servlet-name>
<url-pattern>/se</url-pattern>
</servlet-mapping>
</web-app>
마찬가지로 getServletContext()를 이용하여 데이터를 가져올수가 있습니다.
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletEx extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
getServletContext().setAttribute("connectedIp", "165.62.58.33");
getServletContext().setAttribute("connectedUser", "User");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/seg")
public class ServletGet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String connectedIp = (String)getServletContext().getAttribute("connectedIp");
String connectedUser = (String)getServletContext().getAttribute("connectedUser");
PrintWriter out = response.getWriter();
out.print("<p>connectedIp : " + connectedIp + "</p>");
out.print("<p>connectedUser : " + connectedUser + "</p>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
왼쪽의 se로 매핑된 서블릿에 들어갔다가 오른쪽 seg경로로 이동하면 어떻게 될까요??
데이터가 잘 저장되어서 출력이 되네요!!
하지만 실행시킨후에 se를 들리지 않고 바로 seg만 방문한다면 값이 잘 나오지 않고 null이 나올것입니다.
데이터를 저장하는거는 se인데 저장되지 않은채로 seg를 들려버리면 출력하고자 하는 데이터가 무엇인지 알수 없기 때문이죠!
이 글은 실전 JSP (renew ver.) - 신입 프로그래머를 위한 강좌를 수강하며 공부한 내용을 정리한 글입니다.
https://www.inflearn.com/course/실전-jsp_renew/dashboard
'Java 관련 > JSP & Servlet' 카테고리의 다른 글
[JSP & Servlet] Session (1) | 2022.08.20 |
---|---|
[JSP & Servlet] Cookie (0) | 2022.08.19 |
[JSP & Servlet] JSP 내장객체(config, application, out) (0) | 2022.08.17 |
[JSP & Servlet] request객체, response객체 (0) | 2022.08.16 |
[JSP & Servlet] JSP 스크립트<%! %>, 스크립트릿<% %>, 주석 <%-- --%> (0) | 2022.08.14 |