There are several ways to Import in JSP, you can directly write page tags or Ctrl + Space bar to import automatically.

<%@ 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.*" %>

Here are some classes that you need to import. 

1. Date / Timestamp Class

<%@page import="java.sql.Timestamp"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<%

// java.util.Date d = new java.util.Date();
Date d = new Date(); // Ctrl + Space bar to Import

SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEEEEE");

Timestamp ts = new Timestamp(System.currentTimeMillis());
%>

Time :
<%=d%>
<br>
Time_Korean_Version :
<%=sd.format(d)%>
<br>
Time :
<%=ts%>
<br>

2. Calendar Class

<%@page import="java.util.Calendar"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<%

Calendar c = Calendar.getInstance();

int y = c.get(Calendar.YEAR); // YEAR
int m = c.get(Calendar.MONTH) + 1; // MONTH(0~ 11)
int d = c.get(Calendar.DATE); // DATE

// 12-hour format 
int h1 = c.get(Calendar.HOUR);

// 24-hour format
int h2 = c.get(Calendar.HOUR_OF_DAY);

String h = "";
if (c.get(Calendar.AM_PM) == 0) { // AM_PM : 0 (AM)
	h = "AM"; // AM_PM : 1 (PM)
} else {
	h = "PM";
}

int mm = c.get(Calendar.MINUTE);
int s = c.get(Calendar.SECOND);


// Array : Sun = 1 , Mon = 2.... Sat = 7
int week = c.get(Calendar.DAY_OF_WEEK);  // Day of week(1 ~7)

String[] weekend = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri"};
%>

<%=week %> <br> <!-- 5 : Thursday -->

<!-- 12-hour format -->
<%=m%>-<%=d%>-<%=y%><%=weekend[week-1] %>
<%=h1%>:<%=mm%>:<%=s%><%=h%><br>
<!-- 24 hour format -->
<%=m%>-<%=d%>-<%=y%><%=weekend[week-1] %>
<%=h2%>:<%=mm%>:<%=s%><br>

3. Random Class

<%@page import="java.util.Random"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<%
	// Random Class
Random r = new Random();

int r1 = r.nextInt(10); // 0 ~ 9 

// Random number between 1 and 45
int r2 = r.nextInt(45) + 1;
%>

Your lucky number of today :
<%=r1%>
<br>

 

'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 Tags  (0) 2022.08.25
JSP  (0) 2022.08.25

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

JSP stands for Java Server Pages, which creates dynamic web pages by inserting JAVA code into HTML code
It's a web application program. When JSP runs, it is converted to a Java servlet and runs on the web application server.
Perform the necessary functions and respond to the client with the web page with the generated data.

The following is the basic structure of JSP. 

 There are two models in JSP: model 1 and model2, and the structures are slightly different.

Source : dotnettutorials.net
Source : dotnettutorials.net

To configure JSP, we will download the free source, Apache Tomcat. Please refer to the link below to download it.

Apache Tomcat® - Welcome!

 

Apache Tomcat® - Welcome!

The Apache Tomcat® software is an open source implementation of the Jakarta Servlet, Jakarta Server Pages, Jakarta Expression Language, Jakarta WebSocket, Jakarta Annotations and Jakarta Authentication specifications. These specifications are part of the

tomcat.apache.org

Other than Apache Tomcat, there is another free source called Jetty by Eclipse Foundation. 

If you want to download Jetty instead, please click the link below. 

https://www.eclipse.org/jetty/

 

Eclipse Jetty | The Eclipse Foundation

The Eclipse Foundation - home to a global community, the Eclipse IDE, Jakarta EE and over 415 open source projects, including runtimes, tools and frameworks.

www.eclipse.org

Once downloaded, Apache Tomcat has to be stopped running because Eclipse has to dominate the control. 

First, create a dynamic web project and name it jspproject

Next, create a new JSP file in the WebContent folder. Since they all have different usages, it is important to save the file in the WebContent folder, not in META-INF or WEB-INF.

JSP file will be looking like this:

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
My first JSP program
</body>
</html>

 

In the next post, we will discuss JSP's main tags, so don't worry if you can't understand the syntax above!

'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 Tags  (0) 2022.08.25

+ Recent posts