Develop/JavaScript JQuery JSP

0809 JSP Servlet

포페PostFace 2022. 8. 10. 17:33

0809


ServletContext application = new ServletContext()
이 객체는 어플리케이션당 하나가 자동으로 만들어진다.
== 프로젝트당 하나가 생성된다.

특정 서블릿 내에서 지역변수 설정하기

Servlet 파일

Servlet 파일
@WebServlet(urlPatterns = {"/InitParameter.do"},
			initParams = {
						@WebInitParam(name="group", value="방탄소년단"),
						@WebInitParam(name="title",value="다이너마이트")
						}
			)


값은 config객체에 담긴다.
group = config.getInitParameter("group");

Web.xml

<servlet>
		<servlet-name>test</servlet-name>
		<servlet-class>myPkg.TestServlet</servlet-class>
		<init-param>
			<param-name>singer</param-name>
			<param-value>태연</param-value>
		</init-param>
	</servlet>


<servlet>설정 안에 <init-param>을 통해 설정할 수도 있다.



Servlet에서 주소 설정

//@WebServlet("*.do") //모든 .do 요청을 처리하겠다. /를 쓰면 안된다.
@WebServlet(urlPatterns = {"/insert.do","/update.do","/delete.do","/select.do"})
//중괄호로 감싸기는 필수, urlPatterns는 선택이다.


Web.xml에서 주소 설정

1.전체 통합

	<servlet>
		<servlet-name>do</servlet-name>
		<servlet-class>myPkg.DoServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>do</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>


2.각각 지정

	<servlet>
		<servlet-name>do</servlet-name>
		<servlet-class>myPkg.DoServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>do</servlet-name>
		<url-pattern>/insert.do</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>do</servlet-name>
		<url-pattern>/update.do</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>do</servlet-name>
		<url-pattern>/delete.do</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>do</servlet-name>
		<url-pattern>/select.do</url-pattern>
	</servlet-mapping>


3.각각 지정 생략

	<servlet>
		<servlet-name>do</servlet-name>
		<servlet-class>myPkg.DoServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>do</servlet-name>
		<url-pattern>/insert.do</url-pattern>
		<url-pattern>/update.do</url-pattern>
		<url-pattern>/delete.do</url-pattern>
		<url-pattern>/select.do</url-pattern>
	</servlet-mapping>



doGet 메소드에서 정보출력

String uri=request.getRequestURI();
StringBuffer url=request.getRequestURL();
String conPath=request.getContextPath();
int len= conPath.length();
String command = uri.substring(conPath.length());



결과
uri : /05_RequestDispatcher/update.do
url : http://localhost:8080/05_RequestDispatcher/update.do
conPath : /05_RequestDispatcher
len : 21
command : /update.do

uri.substring(conPath.length())
uri 주소에서 공통주소 이후의 주소를 가져와라
/05_RequestDispatcher/update.do에서
21번째 이후부터 가져온다.

즉 풀어쓰면
String command2=request.getRequestURI().substring(request.getContextPath().length());
이런식이다.

el의 객체
pageScope, requestScope, sessionScope, applicationScope
범위 작음  큼
pageScope : 해당 페이지 안에서만 사용할 객체
requestScope : request 객체 내에서

변수명 앞에 객체명을 정의해주지 않으면 좁은 범위부터 해당하는 변수가 있는지
확인하고 리턴한다.

'Develop > JavaScript JQuery JSP' 카테고리의 다른 글

0810 JSP Servlet MVC구조  (0) 2022.08.10
0808 JSP Servlet  (0) 2022.08.10
0805 XML,EL,JSTL  (0) 2022.08.10
0721 게시판 만들기  (0) 2022.07.22
0720 JSP JDBC로 만든 칼럼 추가 삭제  (0) 2022.07.22