Skip to main content

Using @include directive in JSP

The following example illustrates using @include directive in JSP.
       This directive is used to include a file (typically jsp or html) into a JSP page. Let us now see a simple example of how this works.


Syntax

<%@include file="myfile.jsp"%>

As you can see, you are passing an attribute called file with takes the file to be embedded.

JSP file - index.jsp


<html>
    <body>
    This is in the html body

    <%
    out.println("This is in scriptlet");
    %>
   
    <%@include file="sample.html"%>
    </body>
</html>

sample.html


<html>
    <body>
    <h2>This is a h2 heading</h2>
    <p>This is a paragraph in sample.html</p>
    </body>
</html>

In this way you can also include as many files you want regardless of their file type as far as the web browser can show it to the user.

Comments

Popular posts from this blog

Setting Background Image in JFrame - Swing

Here is sample tutorial, a simple trick that enables you to set background image for JFrame.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BackgroundImageJFrame extends JFrame
{
JButton b1;
JLabel l1;
public BackgroundImageJFrame()
{
setTitle("Background Color for JFrame");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
/*
One way
-----------------
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
add(background);
background.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
background.add(l1);
background.add(b1);
*/
// Another way
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")));
setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am …

Add two numbers in JSP easily

Here is an example on adding up two numbers in JSP. This is very simple as you see below. This requires very small piece of code. All i have done here is created a HTML file containing the input fields which contain the numbers to add up.Now the values in the input field are of type String, so i have done type casting and converted them into int using Integer.parseInt(). See how easy it looks.



Catch The Rat: Simple Game in Java

A simple, shortest and easiest game that could be written in Java using Swing components and event handling.