This time, I will explain ** "JSP" **, which is indispensable for creating web applications.
--What is JSP? --JSP components --Create and execute a JSP file
I will explain the above three points step by step.
"JSP (Java Server Pages)" is a server-side program technology similar to Servlet. Use ** JSP files ** instead of Servlet classes. JSP files are ** converted to Servlet classes when requested **, so what you can do with Servlet classes can also be done with JSP files. The advantage of using a JSP file is that ** it is possible to make HTML output very easy **. The JSP file is ** created by embedding Java code in HTML **. Embed Java code? I don't know if you say that, so let's see how to do it while looking at the following components of the JSP.
The JSP file consists of ** HTML ** and ** Java code **. The part written in HTML is ** template ** The part written in Java code is called ** script **.
From here, it is a necessary element to create a basic JSP file ** ① "JSP comment" **, ** ② "page directive" **, ** ③ "scriptlet" **, ** ④ Learn more about "script expressions" **. Take a look at the sample code below.
sample
<%--① JSP comment--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><%--② page directive--%>
<%
String name="Yamazaki";
int age=24;
%><%--③ Scriptlet--%>
<!DOCTYPE html><%--template--%>
<html>
<head>
<meta charset="UTF-8">
<title>sample</title>
</head>
<body>
my name is<%=name %>.. Age is<%=age %>I'm talented.<%--④ Script expression--%>
</body>
</html>
<%-- ... --%>
python
<%--① JSP comment--%>
However, when writing comments in scriptlets, it follows the Java syntax.
python
<%--① JSP comment--%>
<%
//Declare a variable
%>
<% @ page Attribute name = "value"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
You can use the page directive to make various settings related to the JSP file. Frequently used attributes are the contentType and import attributes.
<% Java code%>
python
<%
String name="Yamazaki";
int age=24;
%>
Scriptlets allow you to embed Java code in JSP files.
<% = Java code%>
python
<body>
my name is<%=name %>.. Age is<%=age %>I'm talented.
</body>
You can use script expressions to output variables, method return values, and so on.
To create a JSP file in Eclipse, select the dynamic web project for which you want to create an HTML file, right-click and select "New"-> "JSP File".
Specify the save location and file name of the file and press "Finish". By default, the "WebContent" directory is selected as the save location.
If you create a JSP file by the above method, Eclipse will write the minimum necessary contents such as ** page directive. ** **
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
//<%Java code added here%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
//<p>Write a paragraph here</p>
</body>
</html>
Recommended Posts