Jsp-Servlet-Applet tutorial

<< Prev
Next >>

Introduction

JSP, Servlet and Applet are techniques for creating web pages using Java codes.

JSP (Java Server Pages)

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.


Elementele unei pagini JSP

Scriptlets

Scriptlets are Java code snippets. These are inserted into the HTML page between the tags <% si %>.


JSP declarations

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


JSP expressions

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


JSP comments

There are lines of text ignored by the JSP container. These are inserted into the HTML page between the tags <%-- si --%>.


JSP directives

Affects the general structure of the servlet class. These are inserted into the HTML page between the tags <%@ si %>.


Install the Tomcat web server

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:


Tomcat

JSP pages must be placed in the directory: tomcat/webapps/ROOT. Open the Xampp control panel and start the Tomcat web server.


Tomcat

Now we open a web browser and type:


http://localhost:8080


If the Tomcat webserver is installed correctly, we will see in the browser:


Tomcat

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:


Tomcat

Deschidem browserul web si o accesam:


Tomcat

Servlets

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.


J2EE

We will write a HelloWorld servlet and save it as HelloWorld.java


J2EE

If classpath is set correctly for J2EE and Java JDK, we will open the command prompt and for compilation we will type:


javac HelloWorld.java


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:


<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>HelloWorld</url-pattern>
</servlet-mapping>


Now we will open a web browser and we will type:


http://localhost:8080/HelloWorld


and we will see in the browser:


J2EE

Applets

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">
</applet>


<< Prev
Next >>

Visit my websites:

https://www.jwebsaints.com
https://www.jwebplants.com