#fileload.py #File Upload Servlet #This Servlet Uploads a file from the client machine #into the Directory in Server box where it runs #This File Upload Servlet works for both binary as well as text files from javax import servlet from javax.servlet import http from java.io import * from java.util import * from java.lang import * from jarray import array , zeros import string , pickle , os #Server IP and Port address SERVER_IP = "127.0.0.1" SERVER_PORT = "8080" #Global variables to have session details session_id = 0 session = 0 done = 0 #done is a flag to indicate that the request from the client has been processed def sub_array ( main_array , end_array ): #This function returns the index within main_array where the end_array #which is embedded within the main_array starts #If no such array is embedded , it returns -1 i = len (main_array) - 1 k = len (end_array) - 1 while 1: j = k while ( j >= 0 ): if ( end_array[j] == main_array[i] ): i = i - 1 j = j - 1 if ( j < 0 ): j = - 1 break if ( i >= 0 ): continue else: return (-1) else: break if ( j == -1 ): return ( i + 1 ) else: i = i - 1 if ( i >= 0 ): continue else: return (-1) class fileload (http.HttpServlet): def doGet(self, req, res): global session , session_id , done done = 0 #Get all Request parameters and Convert it to Java String format parameters = str(req.getParameterNames()) parameters = String(parameters) #Check if there are no parameters parameters = parameters.indexOf("EmptyEnumerator") #If there are no request parameters, the default Login screen is displayed if parameters != -1: res.setContentType("text/html") toclient = res.getWriter() toclient.println( "
File Upload Servlet" + " """) done = 1 #Finished with Processing the client #If User ID and Password were provided, the file Upload screen is displayed UID = req.getParameter("userid") password = req.getParameter("password") if (done == 0 and UID != None and password != None): if UID == "admin" and password == "admin": session = req.getSession() session_id = session.getId() res.setContentType("text/html") toclient = res.getWriter() toclient.println( " """) URL = "http://" + SERVER_IP + ":" + SERVER_PORT + req.getRequestURI() + "?logout=1" toclient.println("") toclient.println ("Logout") toclient.println("