Thursday, July 5, 2018

Application of url-rewritting and session

Note =>>

A. Create a Dynamic Web Project in Eclipse IDE 

B. Create a folder inside WebContent folder, the name of this folder must be myfiles

C. Copy and paste some java files inside this folder. (i mean some .java files should be stored inside myfiles directory) 

D. Create two jsp files inside WebContent  folder

Name of jsp must be following
 1. index.jsp
2. show-file.jsp

Note: In these codes following classes are also used

1. File class (this class located in java.io package): This class is used to denote a path. Path is a location of resource inside the computer file system. resource may be a file or a directory.

2. FileInputStream class (this class is located inside java.io package): This is used to open a file in read mode, means, using this class we can fetch the data from the file.
file is just a group of some related information which is stored inside the hasr disk.

3. Integer class (this class is located inside java.lang package, which is implicitly imported) : This is a wrapper class, and it is mainly used to convert String into int datatype.


Code for index.jsp =>>

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

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

 <%--
  this is a jsp comment
  content of this comment will not be shown at client side
  --%>
  
  <!-- 
  this is a html comment
  content of this comment will be shown at client side (as html source code)
  bole toh as view source
   -->

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<center>
<h3 style="color : red">showing files inside the server</h3>

</center>

<table border="1" align="center">
<th>sno</th><th>filename</th><th>filesize(bytes)</th><th>choice1</th><th>choice2</th>
<%
 // get the path of server......
 // the getRealPath() is a method of ServletContext (a pre-defined-interface),
 // this method returns the path of server root folder.
 // 'application' is the object of ServletContext interface 
 String rootPath = application.getRealPath("/");

  // create the full path of 'myfiles' folder (this folder is inside WebContent folder)
 String folderPath = rootPath + "myfiles";

 // now denote this path using File class (inside java.io package)
 File file = new File(folderPath);

 // get the list of all the files in this path
 File[] array = file.listFiles();

 // store array of file class inside the object of session
 // myarray is just a name for the array
 session.setAttribute("myarray",array);

 // store folderpath inside the object of session
 // folpath is just a name for the folderPath
 session.setAttribute("folpath",folderPath);

 // fetch the data from the array of file class using for loop
 for(int i = 0; i < array.length; i++)
 {
  // show a table row
  out.println("<tr>");

   // get the name of file denoted by ith element of array of file class
  String filename = array[i].getName();

  // get the size of file denoted by ith element of array of file class
  long filesize = array[i].length();
%>
<%--
show columns in a row 
--%>

 <td><%=i + 1%></td>
 <td><%=filename%></td>
 <td><%=filesize%></td>

 <%--
 lets associate some data with url (this is url-rewritting)
 here variable name are 'id' and 'click'
  --%>
 <td><a href="show-file.jsp?id=<%=i%>&click=v">view</a></td>
 <td><a href="show-file.jsp?id=<%=i%>&click=d">download</a></td>

 </tr>
<% 
 }
%>
</table>
</body>
</html>

code for show-file.jsp =>>

<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%
// fetch the data associated with url (i mean the data associated to the url using
// url-rewritting)
String str = request.getParameter("id");
String choice = request.getParameter("click");

// convert str into id (reason index of array is always int)
int index = Integer.parseInt(str);

// fetch array of file and filepath from session
File[] files = (File[])session.getAttribute("myarray");
String filepath = (String)session.getAttribute("filepath");

// get the file name selected by the client
String myFileName = files[index].getName();

// specify the browser that in which format the data will be shown at the client
// side 
response.setContentType("text/plain");

if(choice.equals("v"))
{
  // here we are specifying the web browser that the data of this file will be displayed
 /  at the client   
response.setHeader("Content-Disposition"," inline; filename="+myFileName);
}
else
{

  // here we are specifying the web browser that the data of file will be downloaded
 // as an attachment rather than display at client side
response.setHeader("Content-Disposition"," attachment; filename="+myFileName);
}

// open this file in read mode
FileInputStream fi = new FileInputStream
(filepath+"/"+myFileName);

// get no of bytes available to read using read() method
int size = fi.available();

// create an array of byte to store file data
byte[] buffer = new byte[size];

// fetch file data and store it inside array of byte
fi.read(buffer);

// convert array of byte into string
String data = new String(buffer);

// concatenate the file name with the data of file
data = "## "+myFileName+" ##\n\n" + data; // send the data of file at web browser out.print(data);
%>

5 comments:

  1. mind blowing application of files...

    ReplyDelete
  2. Very easy to understand!
    Thank you sir!

    ReplyDelete
  3. How can someone make such a simplified explanation of such imp topic. Sir tusi great ho

    ReplyDelete
  4. sr very useful content....life bdal di isne

    ReplyDelete