As in other languages, there are a few main tags in JSP.
Scriptlet tag
A scriptlet tag is used to execute java source code in JSP.
<!-- Scriptlet Tag -->
<%
// Basic variables
int i = 30;
double d = 3.14;
char c1 = 'A';
char c2 = '자';
boolean b1 = true;
boolean b2 = false;
// Reference variables
// 1. Class
String str1 = "JSP";
String str2 = new String("JSP");
// 2. Array
String[] str = { "Java", "JSP", "Oracle", "HTML", "Python" };
for (int j = 0; j < str.length; j++) {
out.println(str[j] + "\t"); // println doesn't have a function of printing out in a new line.
}
out.println("<br>");
%>
<%
for (String s : str) {
%>
<%=s%>
<br>
<%
}
%>
<%
// 3. Interface : List
List list = new ArrayList(); // Upcasting
// You need to import with the page tag.
list.add(50);
list.add(50.33);
list.add('A');
list.add(true);
list.add("JSP");
for (int j = 0; j < list.size(); j++){
out.println(list.get(j)+"\t");
}
%>
<br>
Declaration tag
It is used to declare fields and methods.
The code written inside the JSP declaration tag is placed outside the service() method of the auto-generated Servlet.
<!-- Declaration tag -->
<!-- To declare methods -->
<%!
public int add(int a, int b){
int c = a + b;
return c;
}
public int subtract(int a, int b){
int c = a - b;
return c;
}
public int multiply(int a, int b){
int c = a * b;
return c;
}
%>
<%
int result1 = add(3, 9); // To call add method
int result2 = subtract(3, 9); // To call add method
int result3 = multiply(3, 9); // To call add method
%>
3 + 9 = <%=result1 %> <br>
3 - 9 = <%=result2 %> <br>
10 * 25 = <%=result3 %> <br>
10 * 25 = <%=multiply(10, 25) %> <br>
Wait, what is the Servlet?
According to Oracle, "A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes."
The main difference between Servlet and JSP is that Servlet is java based, whereas JSP is HTML based.
Let's get back to the tags then.
Expression tag
It is to the output stream of the response.
<!-- Expression Tag -->
Print out :
<%="Print Success"%>
<br>
Result :
<%=1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10%>
<br>
i =
<%=i%>
<br>
d =
<%=d%>
<br>
c1 =
<%=c1%>
<br>
c2 =
<%=c2%>
<br>
b1 =
<%=b1%>
<br>
b2 =
<%=b2%>
<br>
str1 =
<%=str1%>
<br>
str2 =
<%=str2%>
<br>
Directive tag
Directive tag gives special instruction to Web Container at page translation time. There are three types of directive tags : page, include and taglib.
Page tag, is what you always see on the top of every JSP file, also to import, we use the page tags.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<!-- To import all in java.util package, you can use this code below -->
<%@ page import= "java.util.*" %>
We will study more about import in the next post.
Directive tag | Description |
<%@ page ... %> | defines page dependent properties such as language, session, errorPage etc. |
<%@ include ... %> | defines file to be included. |
<%@ taglib ... %> | declares tag library used in the page |
Action tag
Action tags are used to control the flow between pages and to use Java Bean. The JSP action tags are given below.
Action tag | Description |
jsp:forward | forwards the request and response to another resource. |
jsp:include | includes another resource. |
jsp:useBean | creates or locates bean object. |
jsp:setProperty | sets the value of property in bean object. |
jsp:getProperty | prints the value of property of the bean. |
jsp:plugin | embeds another components such as applet. |
jsp:param | sets the parameter value. It is used in forward and include mostly. |
jsp:fallback | can be used to print the message if plugin is working. It is used in jsp:plugin. |
'Java > JSP' 카테고리의 다른 글
JSP) Implicit Objects 1 - Request Object (0) | 2022.08.26 |
---|---|
JSP) Scopes - Basics / Application (0) | 2022.08.26 |
JSP) Comment (0) | 2022.08.26 |
JSP) Importing / Classes (0) | 2022.08.25 |
JSP (0) | 2022.08.25 |