How to create Dynamic Website?
What is a Dynamic Web Page?
A dynamic page displays different content for different users while retaining the same layout and design. Such pages, usually written in CGI, AJAX, ASP or ASP.NET, take more time to load than simple static pages. They’re frequently implemented to show information that changes frequently, e.g., weather updates or stock prices.
How are Dynamic Web Pages Processed?
When the web server receives a user request for a dynamic page, it does not send the page directly to the requesting browser as it would do with a static page. Instead, it passes the page to the application server which then completes three activities:
- Read the code on the page
- Finish the page according to the code’s instructions
- Remove the code from the page
Follow below steps to build a Dynamic Web Page:
Install httpd module
# yum install httpd
After installation this module creates 2 directories i.e., “/var/www/html” for HTML and “/var/www/cgi-bin” for python.
Create an index.html file where we write the code for dynamic content retrieval
<script>
function run() {
var cmd = document.getElementById('inp').value;
var xhr = new XMLHttpRequest();
xhr.open('GET','http://15.206.117.66/cgi-bin/cmd.py?cmd='+cmd);
xhr.send();
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
document.getElementById('outp').innerHTML = this.responseText;
}
}
}
</script>
<center>
<h1>Welcome to UR Company</h1>
root@ip-15-206-117-66]# <input id='inp' name='cmd'/><br><br>
<button onclick="run()">Run</button><br><br>
<div id='outp'>Data is coming</div>
</center>
Create an executive python file (cmd.py) to get the input from index.html
#!/usr/bin/python3
import subprocess
import cgiprint("content-type: text/html")
print()form = cgi.FieldStorage()
cmd = form.getvalue("cmd")
output = subprocess.getoutput(cmd)
print("<pre>" + output + "</pre>")
Note: change the mode of the file to executive mode using the below code
# chmod +x cmd.py
Now open the web page using the IP address i.e., “http://15.206.117.66"
Dynamic Content retrieval from cmd.py to index.html page.
Summary:
Here we had seen that in the index.html the output from the prompt is displaying in a HTML <div> tag without redirecting of web pages. which makes the Web Pages DYNAMIC