Difference Between .Jsp and .Jspx File Extensions
-
the
.Jsp
File Extension -
XML Script With
.Jspx
File Extension -
the Difference Between
.Jsp
and.Jspx
File Extension
The .JSPX
files represent the XHTML (XML and HTML) script. They help create .jsp
files enabling the separation between viewing layer in the file formats.
Also, the JSPX files are easy to manipulate, understand and render, but they are not ideal for code that contains functions, methods, and complex numerical data.
This article will create files and execute them on the Apache Tomcat 10.0 Server. Also, we will change the .jsp
extension to .JSPX
to show you the live difference.
the .Jsp
File Extension
Check the following .jsp
code.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>A demo .JSP file</title>
<style>
#win * {color:gold;}
#lose * {color:red;}
</style>
</head>
<body>
<h4> We will run a program that will give you random numbers each time you click on new number </h4>
<%
double ran = Math.random();
if (ran > 0.51) {
%>
<div id="win">
<h4> You made it!</h4>
<p> Lucky you winner! <%= ran %> </p>
</div>
<%} else {
%>
<div id="lose">
<p> You got: <%= ran %> </p>
<p> Better luck next time! </p>
<%
}
%>
<a href="<%= request.getRequestURI() %>"><b> Lets do it again! </b></a>
</div>
</body>
</html>
Output:
We aim to differentiate file formats (extensions), not to understand the XML and JS syntax. However, we still made it clean with comments before each significant script section.
XML Script With .Jspx
File Extension
This is a clean XML script. We saved this as .JSPX
to show the real-time implementation of a complete XML file as JSPX on Apache Server.
<!-- A simple XML script using JS -->
<!--
Note we are not learning JS and XML logical flow, this code is for the demonstration purpose of how to run XML script as a .JSPX fie on Apache Tomcat 10.0 -->
<!DOCTYPE html>
<html>
<body>
<h4>A demo XML/JS script to run as .JSPX file extension</h4>
<div>
<span id="val1"></span><br>
<span id="val2"></span><br>
<b>Equal to:</b> <span id="what"></span>
</div>
<script>
/* Storing is values in JS variable */
var txt, parser, xmlDoc;
txt = "<note>" +
"<val1>2 +</val1>" +
"<val2>2</val2>" +
"<heading>Equal to</heading>" +
"<body>4</body>" +
"</note>";
/* using parse function */
parser = new DOMParser();
xmlDoc = parser.parseFromString(txt,"text/xml");
document.getElementById("val1").innerHTML =
xmlDoc.getElementsByTagName("val1")[0].childNodes[0].nodeValue;
document.getElementById("val2").innerHTML =
xmlDoc.getElementsByTagName("val2")[0].childNodes[0].nodeValue;
document.getElementById("what").innerHTML =
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
</script>
</body>
</html>
Output:
A demo XML/JS script to run as .JSPX file extension
2 +
2
Equal to: 4
the Difference Between .Jsp
and .Jspx
File Extension
We will run the my.jsp
file by changing it to my.JSPX
to show you what happens.
Check it here:
Back to the main point:
JSPX files reflect the XML format and enhance JSP pages dynamically because JSPX allows you to separate the code and the view layer into different files.
In a nutshell, we can create .JSPX
files to build XHTML pages, but a JSP file is what we need for Java functions, math, and algorithmic content.
In some circumstances, writing XML format code is preferable to writing native .JSP
code.
Sarwan Soomro is a freelance software engineer and an expert technical writer who loves writing and coding. He has 5 years of web development and 3 years of professional writing experience, and an MSs in computer science. In addition, he has numerous professional qualifications in the cloud, database, desktop, and online technologies. And has developed multi-technology programming guides for beginners and published many tech articles.
LinkedIn