import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jini.FileList;
public class FileServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = (String) request.getParameter("path");
System.out.println("PATH => " + path);
ServletContext context = request.getServletContext();
String realPath = context.getRealPath("/");
StringBuilder realPathBuilder = new StringBuilder();
realPathBuilder.append(realPath);
realPathBuilder.append("<br/>");
System.out.println("ACTUAL PATH were this servlet exists => " + realPath);
if(null != path && path.length() == 0 ){
existOnError(response);
return;
}
File[] fileList= FileList.FileList(path);
if(fileList == null){
existOnError(response);
return;
}
System.out.println("File List .................... " );
StringBuilder filebuilder= new StringBuilder();
for(int i=0; i<fileList.length;i++){
File file = fileList[i];
// System.out.println(file.toString());
filebuilder.append("<a href=\"");
filebuilder.append(file.toString());
filebuilder.append("\">");
filebuilder.append(file.toString());
filebuilder.append("</a>");
filebuilder.append("<br/>");
//<a href="url">Link text</a>
} // end of for
filebuilder.append("<br/>");
System.out.println("Directory List ....................... " );
StringBuilder dirbuilder= new StringBuilder();
fileList= FileList.DirectoryList(path);
for(int i=0; i<fileList.length;i++){
File file = fileList[i];
// System.out.println(file.toString());
dirbuilder.append(file.toString());
dirbuilder.append("<br/>");
} // end of for
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("================== SERVLET ACTUAL PATH ==================" + "<br/>");
out.println(realPathBuilder.toString());
out.println("=================== FILE LIST ==================" + "<br/>");
out.println(filebuilder.toString());
out.println("=================== DIRECTORY LIST ==================" + "<br/>");
out.println(dirbuilder.toString());
} // end of doGet()
void existOnError(HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("=================== Invalid or Empty Path ==================" + "<br/>");
}
}
=============================
<servlet>
<servlet-name>FileServlet</servlet-name>
<servlet-class>com.jini.FileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileServlet</servlet-name>
<url-pattern>/filelist</url-pattern>
</servlet-mapping>
============================
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jini.FileList;
public class FileServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = (String) request.getParameter("path");
System.out.println("PATH => " + path);
ServletContext context = request.getServletContext();
String realPath = context.getRealPath("/");
StringBuilder realPathBuilder = new StringBuilder();
realPathBuilder.append(realPath);
realPathBuilder.append("<br/>");
System.out.println("ACTUAL PATH were this servlet exists => " + realPath);
if(null != path && path.length() == 0 ){
existOnError(response);
return;
}
File[] fileList= FileList.FileList(path);
if(fileList == null){
existOnError(response);
return;
}
System.out.println("File List .................... " );
StringBuilder filebuilder= new StringBuilder();
for(int i=0; i<fileList.length;i++){
File file = fileList[i];
// System.out.println(file.toString());
filebuilder.append("<a href=\"");
filebuilder.append(file.toString());
filebuilder.append("\">");
filebuilder.append(file.toString());
filebuilder.append("</a>");
filebuilder.append("<br/>");
//<a href="url">Link text</a>
} // end of for
filebuilder.append("<br/>");
System.out.println("Directory List ....................... " );
StringBuilder dirbuilder= new StringBuilder();
fileList= FileList.DirectoryList(path);
for(int i=0; i<fileList.length;i++){
File file = fileList[i];
// System.out.println(file.toString());
dirbuilder.append(file.toString());
dirbuilder.append("<br/>");
} // end of for
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("================== SERVLET ACTUAL PATH ==================" + "<br/>");
out.println(realPathBuilder.toString());
out.println("=================== FILE LIST ==================" + "<br/>");
out.println(filebuilder.toString());
out.println("=================== DIRECTORY LIST ==================" + "<br/>");
out.println(dirbuilder.toString());
} // end of doGet()
void existOnError(HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("=================== Invalid or Empty Path ==================" + "<br/>");
}
}
=============================
<servlet>
<servlet-name>FileServlet</servlet-name>
<servlet-class>com.jini.FileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileServlet</servlet-name>
<url-pattern>/filelist</url-pattern>
</servlet-mapping>
============================
No comments:
Post a Comment