Pages

Thursday, 4 August 2022

Servlet - JSP Programs

JSP(Java Server Pages) Programs :
JavaServer Pages (JSP) is a Java standard technology that enables you
to write dynamic data-driven pages for your Java web applications.
JSP is built on top of the Java Servlet specification.
1Example on JSP step1: Create an index.html file <html> <body> <h1>WELCOME TO TOMCAT INSTALLATION<h1> <h2>WELCOME TO TOMCAT INSTALLATION<h2> <h3>WELCOME TO TOMCAT INSTALLATION<h3> <h4>WELCOME TO TOMCAT INSTALLATION<h4> <h5>WELCOME TO TOMCAT INSTALLATION<h5> </body> </html> step 2:Create a directory loginpage in webapp root@ubuntu:/opt/tomcat/webapps# mkdir loginpage root@ubuntu:/opt/tomcat/webapps/loginpage# gedit index.html step 3:Before going to run index.html file we need to do the following commands root@ubuntu:/opt/tomcat/bin# ./shutdown.sh root@ubuntu:/opt/tomcat/bin# ./startup.sh


2Example on JSP:
 

Step1: Create a file named by input.html

root@ubuntu:/opt/tomcat/webapps/loginpage# gedit index.html

<html>
<body>
<form action="Factorial.jsp">
Enter a value for n: <input type="text" name="val">
<input type="submit" value="Submit">
</form>
</body>
</html>

Step 2 :Create another file named by Factorial.jsp

root@ubuntu:/opt/tomcat/webapps/loginpage# gedit Factorial.jsp

<html>
<body>
<%!
long n, result;
String str;

long fact(long n) {
if(n==0)
return 1;
else
return n*fact(n-1);
}
%>
<%
str = request.getParameter("val");
n = Long.parseLong(str);
result = fact(n);
%>
<b>Factorial value: </b> <%= result %>
</body>
</html>
output:


3.Example on JSP:
//indian flag display 
<!doctype html>
<html>
<head>
<title> FLAG </title>
</head>
<body bgcolor="white">
<center>
<form>
<textarea name="myTextBox" cols="50" rows="5"   style="background-color:orange">
</textarea>
<br>
<img src="flag.png" width="100px" height="80px">
</img>
<br>
<textarea name="myTextBox" cols="50" rows="5" style="background-color:green">
</textarea>
<br>
</form>
</center>
 <body >
<hr color="orange" size="80"> 
<font face="algerian" color="blue" size="8">
<marquee scrolldelay="10" direction="left" behaviour="alternate" > Indian Flag </marquee> 
</font> 
<hr color="green" size="80"> 
</body>
</body>
</html>
 output:
 
 
Servlet Program
1Example on Servlet: 
Step 1: create a folder serapp on the desktop root@ubuntu:/home/vl/Desktop# mkdir serapp
Step 2: create 2 folders and 1 file i.e file index.html , src folder,WEB-INF folder root@ubuntu:/home/vl/Desktop# cd serapp root@ubuntu:/home/vl/Desktop/serapp#mkdir src root@ubuntu:/home/vl/Desktop/serapp#mkdir WEB-INF Step 3: create a file index.html in serapp folder root@ubuntu:/home/vl/Desktop/serapp#gedit index.html //index.html <html> <head> <title> TestServlet </title> </head> <body> <a href="/serapp/Demo"> Demo </a> </body> </html>
Step 4: create a file Demo.java in src folder
root@ubuntu:/home/vl/Desktop/serapp#cd src 
root@ubuntu:/home/vl/Desktop/serapp/scr#gedit Demo.java 
//Demo.java 

import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;

public class Demo extends HttpServlet {

public void doGet(HttpServletRequest request, 

HttpServletResponse response) throws 

IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title> Servlet Demo </title>");
out.println("</head>");
out.println("<body>");
out.println("<marquee>");
out.println("<hr>");
out.println("<h1>Welcome To The First Servlet Program </h1>");
out.println("<hr>");
out.println("</marquee>");
out.println("</body>");
out.println("</html>");
}
}

 

Step 5: create a folder classes and a file web.xml in WEB-INF 

root@ubuntu:/home/vl/Desktop/serapp/#cd WEB-INF  

root@ubuntu:/home/vl/Desktop/serapp/WEB-INF#mkdir classes 

root@ubuntu:/home/vl/Desktop/serapp/WEB-INF#gedit web.xml 

 

// web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app>

<web-app>
<servlet>
<servlet-name>Demo</servlet-name>
<servlet-class>Demo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Demo</servlet-name>
<url-pattern>/Demo</url-pattern>
</servlet-mapping>
</web-app>

Step 6: Now start tomcat Services

root@ubuntu:/opt/tomcat/bin# ./startup.sh

Step 7: Compile the Demo.java file as shown  
 
root@ubuntu:/home/vl/Desktop/serapp/WEB-INF#javac Demo.java 
-classpath /home/vl/apache-tomcat-10.0.23/lib/servlet-api.jar  
 
root@ubuntu:/home/vl/Desktop/serapp/WEB-INF# ls

Demo.class Demo.java  

//If we compile as below it will automatically generate

Demo.class in WEB-INF/classes  

root@ubuntu:/home/vl/Desktop/serapp/src#javac -d ../WEB-INF/

classes/ Demo.java -cp /home/vl/apache-tomcat-10.0.23/lib/servlet-api.jar  

Step 8: create a war file  

Note: JAR files allow us to package multiple files in order to 
use it as a library, plugin, or any kind of application. On the 
other hand,WAR files are used only for web applications. 

root@ubuntu:/home/vl/Desktop/serapp#jar cvf serapp.war *

added manifest
adding: index.html(in = 108) (out= 84)(deflated 22%)
adding: src/(in = 0) (out= 0)(stored 0%)
adding: src/Demo.class(in = 870) (out= 511)(deflated 41%)
adding: src/MyServlet.java(in = 460) (out= 246)(deflated 46%)
adding: src/HelloWorld.class(in = 877) (out= 507)(deflated 42%)
adding: src/HelloWorld.java(in = 639) (out= 257)(deflated 59%)
adding: src/Demo.java(in = 647) (out= 263)(deflated 59%)
adding: WEB-INF/(in = 0) (out= 0)(stored 0%)
adding: WEB-INF/web.xml(in = 308) (out= 154)(deflated 50%)
adding: WEB-INF/classes/(in = 0) (out= 0)(stored 0%)
adding: WEB-INF/classes/Demo.class(in = 870) (out= 511)(deflated 41%)
Step 9:now by using ls -R
root@ubuntu:/opt/tomcat/webapps/serapp# ls -R
.:
index.html src WEB-INF
./src:
Demo.class Demo.java
./WEB-INF:
classes web.xml
./WEB-INF/classes:
Demo.class

Output:




 
 

Tuesday, 2 August 2022

Installation of Tomcat 10 on Ubuntu

1. Install Java

Upgrade the system

$ sudo apt upgrade

Update system packages.

$ sudo apt update

Install Java runtime environment.

$ sudo apt install default-jdk -y

Verify Java installation.

$ java -version

How to find the OpenJDK directory with the following command:

$ readlink -f /usr/bin/javac

As you can have multiple versions of Java installed on your system, you can decide which one is the default one.First, run a command that shows all the installed versions on your computer:

$ sudo update-alternatives --config java

2. Install Tomcat

Download the latest version of Apache Tomcat. To find the latest Tomcat version, visit the official download page.

$ wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.23/bin/apache-tomcat-10.0.23.tar.gz

Extract the downloaded archive.

$ sudo tar xzvf apache-tomcat-10.0.23.tar.gz

Create an installation directory /opt/tomcat/.

$ sudo mkdir /opt/tomcat/

Move the extracted files to the installation directory.

$ sudo mv apache-tomcat-10.0.23/* /opt/tomcat/

Since you have already created a user, you can now grant tomcat ownership over the extracted installation by running:

  1. sudo chown -R tomcat:tomcat /opt/tomcat/
  2. sudo chmod -R u+x /opt/tomcat/bin
Edit conf/tomcat-users.xml file to configure an administrator and manager 
account for Apache Tomcat.
$ sudo nano /opt/tomcat/conf/tomcat-users.xml

Add the code below within the <tomcat-users> tag. Change the password for administrator and manager access by changing the value StrongPassword below with a high secure password.

<!-- user manager can access only manager section -->
<role rolename="manager-gui" />
<user username="manager" password="manager" roles="manager-gui" />

<!-- user admin can access manager and admin section both -->
<role rolename="admin-gui" />
<user username="admin" password="admin" roles="manager-gui,admin-gui" />

Enable remote access to Apache Tomcat by editing manager and host-manager configuration files. Edit manager application context.xml file:

$ sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml

Comment out the IP addresses section as shown below. Then, save and close the file.

<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

Edit host manager application context.xml file:

$ sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml

Comment out the IP addresses section as shown below. Then, save and close the file.

<!--<Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

Create a systemd unit file for Apache Tomcat.

$ sudo nano /etc/systemd/system/tomcat.service

Add the code below to the file. Then, save and close the file.

[Unit]
Description=Tomcat
After=network.target

[Service]
Type=forking

User=root
Group=root

Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64
Environment=JAVA_OPTS=-Djava.security.egd=file:///dev/urandom
Environment=CATALINA_BASE=/opt/tomcat
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

Reload the system daemon service to apply changes.

$ sudo systemctl daemon-reload

Start Apache Tomcat service.

$ sudo systemctl start tomcat

Enable the service to start up on system boot.

$ sudo systemctl enable tomcat

Check the status of the service.

$ sudo systemctl status tomcat 
Note: press ctrl +c after running the above coomand 
Note:Bydefault localhost:8080 if it does not start then we need 
to change the localhost:8081 and repeat the above 4 commands 
i.e reload start,enable,check
root@ubuntu:/opt/tomcat# cd conf/
root@ubuntu:/opt/tomcat/conf# ls
Catalina context.xml logging.properties tomcat-users.xsd
catalina.policy jaspic-providers.xml server.xml web.xml
catalina.properties jaspic-providers.xsd tomcat-users.xml
root@ubuntu:/opt/tomcat/conf# gedit server.xml

3. Access Apache Tomcat Web Interface

Go to your browser address bar to access the web interface and type in http://ServerIPaddress:8080 for SuiteCRM to access the web install wizard. For example:

localhost:8081/
Note:If still it is not working then we need to use the following coomands
root@ubuntu:/opt/tomcat/bin# ./shutdown.sh 
root@ubuntu:/opt/tomcat/bin# ./startup.sh

 
 

Servlet - JSP Programs

JSP(Java Server Pages) Programs : JavaServer Pages (JSP) is a Java standard technology that enables you to write dynamic data-driven pages f...