[JAVA] What is JSP? ~ Let's know the basics of JSP !! ~

What is JSP

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.

What is JSP

"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.

JSP components

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>
    

① "JSP comment"

<%-- ... --%>

python


<%--① JSP comment--%> 

However, when writing comments in scriptlets, it follows the Java syntax.

python


<%--① JSP comment--%> 
<%
//Declare a variable
%>

② "page directive"

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

③ "Scriptlet"

<% Java code%>

python


 <%
    String name="Yamazaki";
    int age=24;
    %>

Scriptlets allow you to embed Java code in JSP files.

④ "Script expression"

<% = 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.

Create and run a JSP file

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". jsp_create1.jpg

Specify the save location and file name of the file and press "Finish". By default, the "WebContent" directory is selected as the save location. jsp_create2.jpg

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

What is JSP? ~ Let's know the basics of JSP !! ~
'% 02d' What is the percentage of% 2?
What is testing? ・ About the importance of testing
What is the data structure of ActionText?
Want to know what Ruby n is the power of 2? (Power judgment of 2)
What is the Java Servlet / JSP MVC model?
Docker monitoring-explaining the basics of basics-
What is the pluck method?
Understand the basics of docker
What is the BufferedReader class?
The basics of Swift's TableView
What is the constructor for?
[Java basics] What is Class?
What is the initialize method?
What is the representation of domain knowledge in the [DDD] model?
About the basics of Android development
What kind of StringUtil is good
The basics of SpringBoot + MyBatis + MySQL
What is it? ~ 3 types of "no" ~
What kind of method is define_method?
What is @Override or @SuppressWarnings ("SleepWhileInLoop") in front of the function? ?? ??
[Challenge CircleCI from 0] Learn the basics of CircleCI
Understand the basics of Android Audio Record
What to do if the prefix c is not bound in JSP
What is the right migration data type? ??
Now, I've summarized the basics of RecyclerView
I want to know the JSP of the open portlet when developing Liferay
What is the best file reading (Java)
What is the model test code testing
[day: 5] I summarized the basics of Java
What is the main method in Java?
What are the updated features of java 13
What is the Facade pattern useful for?
Looking back on the basics of Java
What is CHECKSTYLE: OFF found in the Java source? Checkstyle to know from
[Beginner] What is Docker in the first place? Easy-to-understand explanation from the basics!
Let's go with Watson Assistant (formerly Conversation) ① Introduction What is the idea and intention understanding of interactive apps?
[Ruby] What is the slice method? Let's solve the example and understand the difference from slice!
Retrieve the first day of week in current locale (what day of the week is it today?)
What is ... (3 dots) found in the Java source? Variadic arguments to know from
What is Pullback doing in The Composable Architecture
[Ruby] Summary of class definitions. Master the basics.
ActiveSupport underscore is not the inverse of camelize
The order of Java method modifiers is fixed
What is different from the PHP language. [Note]
I understood the very basics of character input
The basics of the App Store "automatic renewal subscription"
Let's check the feel of Spring Boot + Swagger 2.0
What is the volatile modifier for Java variables?
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
What are the advantages of DI and Thymeleaf?
The official name of Spring MVC is Spring Web MVC
What is Cubby
What is Docker?
What is null? ]
What is java
What is Keycloak
What is maven?
What is Jackson?
What is Docker
What is self