JSP
JSP 기초
carrot62
2020. 8. 3. 21:24
JSP란 Java Server Pages의 동적 웹 페이지를 만드는 웹 어플리케이션 도구이다. Servlet technology의 확장된 버전으로 이전에 Servlet에서 사용하던 모든 기능들을 그대로 사용 할 수 있다.
jsp 파일 형식:
- php서는 <? ?>를 사용하지만, jsp에서는 <% %>를 사용한다.
예) out.print(2*5);
<% java source code %>
JSP API
JSP API is a set of classes and interfaces that can be used to make a JSP page.
STS에서 jsp 파일 서버에 구동하는 방법
프로젝트 오른쪽 마우스 클릭 → Run As → Run on Server → choose tomcat server → addAll → finish
Scipting elements
- scriplet tag
- expression tag
- declaration tag
JSP Scriplet tag
출력할 때는
- out.print("");
JSP로 form에서 입력한 값을 받아오는 방법
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname"><br/>
<input type="submit" value="go">
</form>
</body>
</html>
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</form>
</body>
</html>
JSP expression tag
내용을 출력할 때 사용된다. php에서 <?= ?>를 사용해 변수 값을 출력할 때와 비슷하다.
<%= statement %>
<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>
expression tag 끝에는 semi-colon을 붙이지 않도록 주의하자!
- 시간 출력:
Current Time: <%= java.util.Calendar.getInstance().getTime() %>
- Form에서 받아온 데이터 출력
사용 예시) 여기서는 변수 이름을 uname으로 사용하였다
<%= request.getParameter("uname") %>
JSP Declaration tag
This tag is used to dclare fields and methods
형식은 다음과 같다
<%! field or method declaration %>
- 변수 사용하기 - a JSP declaration tag that declares field
<html>
<body>
<%! int data=50; %>
<%= "Value of the variable is:"+data %>
</body>
</html>
- 함수 사용하기 - a JSP declaration tag that declares method
<html>
<body>
<%!
int cube(int n){
return n*n*n*;
}
%>
<%= "Cube of 3 is:"+cube(3) %>
</body>
</html>
꼭 알아두기: JSP Implicit Objects
JSP Implicit Objects
1. out
Servlet | JSP |
PrintWriter out=response.getWriter(); | <% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %> |
2. request
예시)
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
3. response
이 object를 사용하면 submit 버튼을 누를때 지정한 url 위치로 이동하게 되다
<%
response.sendRedirect("http://www.google.com");
%>
4. config
Generally, it is used to get initialization parameter from the web.xml file.
web.xml 파일:
<web-app>
<servlet>
<servlet-name>sonoojaiswal</servlet-name>
<jsp-file>/welcome.jsp</jsp-file>
<init-param>
<param-name>dname</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>sonoojaiswal</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
<%
out.print("Welcome "+request.getParameter("uname"));
String driver=config.getInitParameter("dname");
out.print("driver name is="+driver);
%>
5. application
<%
out.print("Welcome "+request.getParameter("uname"));
String driver=application.getInitParameter("dname");
out.print("driver name is="+driver);
%>
6. session
7. pageContext
8. page
Servlet이었다면:
<% (HttpServlet)page.log("message"); %>
<% this.log("message"); %>
9. exception
<%@ page isErrorPage="true" %>
<html>
<body>
Sorry following exception occured:<%= exception %>
</body>
</html>