1. TTfile
  2. JSP File

JSP File

Java Server Pages (JSP) is a Java technology that helps software developers serve dynamically generated web pages based on HTML, XML, or other document types.

JSP was released in 1999 as Java’s answer to ASP and PHP. It was designed to address the perception that the Java programming environment did not provide developers with enough support for the Web.

Architecturally, JSP may be viewed as a high-level abstraction of Java servlets. JSP pages are loaded in the server and operated from a structured special installed Java server packet called a Java EE Web Application, often packaged as a .war or .ear file archive.

JSP allows Java code and certain pre-defined actions to be interleaved with static web markup content, with the resulting page being compiled and executed on the server to deliver an HTML or XML document. The compiled pages and any dependent Java libraries use Java byte code rather than a native software format, and must therefore be executed within a Java virtual machine (JVM) that integrates with the host operating system to provide an abstract platform-neutral environment.

The JSP file extension indicates that the file is a Java Server Page file. Java Server Pages are scripted HTML code pages interpreted and executed by a java application server. JSPs enable dynamic web content. However, the JSP file itself is simply a text file with a .jsp extension. A JSP file is executed by uploading the file onto a java application web server and accessing the file through a browser. However, the contents of a JSP file may be read with a simple text application such as Notepad.

aSave a copy of the JSP file to the hard drive.

bRight-click on the JSP file.

cSelect the Open With... option from the context menu.

1

dScroll down to the Notepad option.

eClick the Choose Program... if no Notepad option is listed in the context menu.

fClick the Browse button, and then select Notepad from the list of programs.

2

gUncheck the option to Always open the selected program to open this type of file.

4

hClick the OK button. The JSP file will open in the Notepad application and the contents of the file will be visible and editable.

3

One of the benefits of Java Server Pages (JSP) is that you can let your Web server do some of the work traditionally left to your visitor's Web browser. In traditional static HTML, CSS (cascading) stylesheets are referenced using the "link" tag, requiring your visitor's Web browser to fetch the stylesheet before rendering your Web page. Using JSP, you can include the CSS directly, making faster Web pages for your visitors.

0

Instructions

Step 1

Open your JSP file in your favorite editor.

Step 2

Locate the header section of the document (between the and tags near the top of the file).

Step 3

Start a style block on a new line by adding

8.

Hit the Enter key to add a new line below the style tag.

Step 4

Add a reference to your CSS stylesheet using the JSP include directive by adding

<%@include file="style.css" %>.

Use the correct path and filename of your stylesheet in place of style.css. Hit the Enter key to add a new line below the JSP tag.

Step 5

Finish the style block by adding

9.

Step 6

Save the file and test it. When your JSP file is executed, it will copy the contents of your CSS stylesheet into the style block of the HTML generated by the JSP.

Tips & Warnings

If your JSP executes without errors, but the page is not styled according to your CSS stylesheet, make sure the directory path and file name are correct. The directory path is relative to the JSP file. On some Web servers, the file name may be case-sensitive. Make certain the path and file name in the JSP are spelled exactly as they appear on the server.

JSP allows you to embed snippets of the Java programming language into HTML tags to create interactive content. HTML pages are static in that each time a user loads the page, the content will appear the same. JSP allows for dynamic content to be added to HTML pages, so that each time the user reloads the page, the content changes. For example, we will go through a sample where every time a user reloads the page, the date and time are changed automatically to reflect the current date and time.

0

Instructions

Step 1

Open a new page in Notepad. Type the beginning and closing HTML tags with some space in between both tags. This specifies that the content will be in HTML.

1

Step 2

Type the opening body tag in the line below 6 and the closing body tag in the line directly above 7. Keep some empty lines between the opening and closing body tags. The rest of the lines between these two tags will specify the action the website will follow:

2

Step 3

Insert the following line at the top before the <HTML> tag:

3

This line declares that you are importing the java.util package because you will be using the date function. Java packages consist of classes of related functions.

Step 4

Insert the following tag after the <BODY> tag:

4

The 5 are JSP tags that specify an action needs to be taken with the function that is provided.

Step 5

Save the page with a .jsp extension.

Java doesn't offer a neat and pretty one-liner for copying files. However, Java's file input-output (I/O) classes make it fairly easy to write your own file copying functionality. Get started by writing the file copy functionality as scriptlet code directly in your Java ServerPages (JSP) page.

The Basics and Catch Clause

Step 1

Import the Java classes you'll need for reading and writing files using the page directive in your JSP page:

<%@ page import="java.io.*" %>

Step 2

Create a try-catch block in your JSP page to handle IOException:

<%

try {}

catch (IOException ex) {}

%>

Step 3

Handle IOException errors inside the catch clause as needed for the JSP page to fail gracefully. Print the exception message in glaring colors into the JSP page by breaking out of the scriptlet code:

catch (IOException ex) { %>

<%=ex.getMessage()%>

<% }

The Try Block

Step 1

Open the source file (the file you want to copy) and the destination file (where the copy will be written to) inside the try-block. SrcFileName and dstFileName are string variables containing the path and file name of each file:

File srcFile = new File(srcFileName);

File dstFile = new File(dstFileName);

Step 2

Check that the source file exists, and throw an IOException if it doesn't:

if (!srcFile.exists()) {

throw new IOException("No source file: " + srcFileName);

}

Step 3

Check that the destination file exists and is writable. Throw an IOException if it isn't:

if (dstFile.exists()) {

if (!dstFile.canWrite()) {

throw new IOException("Destination read-only: " + dstFileName);

}

}

else {

throw new IOException("Destination not created: " + dstFileName);

}

Step 4

Open source and destination file streams:

FileInputStream srcStrm = new FileInputStream(srcFile);

FileOutputStream dstStrm = new FileOutputStream(dstFile);

Step 5

Create a byte array to hold data:

byte[] buf = new byte[4096];

Step 6

Read from the source stream and write to the destination stream in a while loop that continues until all the data have been read from the source file:

int len;

while ((len = srcStrm.read(buf)) > 0) {

dstStrm.write(buf, 0, len);

}

Step 7

Close the file streams:

srcStrm.close();

dstStrm.close();