JSP is a technology that allows developers to generate dynamic web pages using Java code snippets that are embedded in HTML pages.
To manage and use these pages, we need a compatible server (eg Apache Tomcat) that has a JSP container (servlet container).
The pages created will have the .jsp extension and will contain html tags and jsp tags.
There is a JSP compiler implemented in the web server, which analyzes the JSP code, compiles it and turns it into an executable servlet.
The lifecycle of a JSP page consists of initializing, executing, and destroying it.
Scriptlets are Java code snippets. These are inserted into the HTML page between the tags <% si %>.
The variables and methods that will be used in the Java code on the JSP pages are declared. These are inserted into the HTML page between the tags <%! si %>.
There are elements that will be converted into strings and inserted into the JSP page where the expression appears. These are inserted into the HTML page between the tags <%= si %>.
There are lines of text ignored by the JSP container. These are inserted into the HTML page between the tags <%-- si --%>.
Affects the general structure of the servlet class. These are inserted into the HTML page between the tags <%@ si %>.
As described in the Tomcat web server section, after installing Xampp (which also includes the Tomcat web server), the Tomcat web server installation folder will be:
JSP pages must be placed in the directory: tomcat/webapps/ROOT. Open the Xampp control panel and start the Tomcat web server.
Now we open a web browser and type:
If the Tomcat webserver is installed correctly, we will see in the browser:
We write the code to display the date and time, save the file as test.jsp and put it in the directory: tomcat/webapps/ROOT:
Deschidem browserul web si o accesam:
Servlets are java programs that run on the server and the result is visualized in the browser. They use the classes in the javax.servlet and javax.servlet.http packages. These packages are not found in the Java JDK platform.
We will download and install the J2EE platform that contains these packages as well as a dedicated server.
We will write a HelloWorld servlet and save it as HelloWorld.java
If classpath is set correctly for J2EE and Java JDK, we will open the command prompt and for compilation we will type:
The javac.exe compiler will compile our file and will result the compiled file: HelloWorld.class.
We will put this compiled file (servlet) into the directory: tomcat/webapps/ROOT/WEB-INF/classes in the Tomcat server. In this directory we will put other servlets.
Also in the WEB-INF directory is the web.xml file. We edit this file and write:
Now we will open a web browser and we will type:
and we will see in the browser:
Applets are java programs that run directly in the browser. Write a java program (eg test.java). It compiles and gets the compiled file: test.class.
Suppose we want to present this applet on page: page.html. The files: page.html and test.class will need to be found in the same directory. The applet is called by writing to the file: page.html, the code:
<applet code="test.class" width="200" height="100">
Visit my websites:https://www.jwebsaints.com