Nitasha Gupta

Greenhorn
+ Follow
since Jun 17, 2006
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Nitasha Gupta

hello,

I am trying to write a code that
calls servlet running on the local server from the java class using URL
I used the following code for it.

DataInputStream dis;
try {
String urlString = "http://localhost/Project/downloadServlet?fileName=hello.txt";
URL url = new URL(urlString);

//url.openConnection();


dis = new DataInputStream(url.openConnection().getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(dis));

String total = "";
String line = "";
while ((line = in.readLine()) != null) {
total += line;
}
in.close();

} catch (Exception e) {
System.out.println(e);
}


In the URL I am passing the request to servlet to download this file from a particular loaction for ex: "www.ibm.com" and store it into local file system.


My Servlet Code is like this:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE); //Set the content type
PrintWriter out = response.getWriter(); //Get the writer to the output stream

System.out.println("FileDownload Servlet fetching file...");
fileName = request.getParameter("fileName");
if ( fileName == null ) {
out.println("ERR: No fileName parameter given.");
return;
}

try {
System.out.println(" fileName: " + fileName);
URL url = new URL(www.ibm.com + "/"+fileName); //Open the URL

InputStream stream = url.openStream(); //Get the Input Stream for the URL
byte[] bytes = new byte[this.MAX_FILE_SIZE]; //Set max file size
int bytesRead = 0; //Variable for the file size
int b = 0; //Variable to hold the bytes read from the stream

while((b = stream.read()) != -1 && bytesRead <= this.MAX_FILE_SIZE) {
bytes[bytesRead++] = (byte)b; //Put the bytes into the array
}

//The byte array has a size of 2MB now this has to be trimmed to the
//actual file size before being written on to the filesystem.
byte[] outputBytes = new byte[bytesRead];
// Copy the relevant bytes to the output array
System.arraycopy(bytes, 0, outputBytes, 0, bytesRead);

// Get the path name from the URL. This value is the String
// that follows the hostname ort number
String file = url.getFile();

// Get the file name from the URL path name.
String fileName = file.substring(file.lastIndexOf("/") + 1, file.length());

// Write the bytes to the destination directory
FileOutputStream fos = new FileOutputStream("C:\"+fileName);
fos.write(outputBytes);

// Close the stream
fos.close();

// Output Servlet response
out.println("OK: " + fileName + "<br>");

System.out.println(" " + bytesRead + " bytes read");

} catch(Exception e) {
out.println("ERR: " + e); // For the log
System.out.println(" ERROR: " + e); // For the console
throw new ServletException(e.toString());
}
}

However when I run this code it gives me fileNotFoundException (assuming hello.txt is present in the webserver with url www.ibm.com/hello.txt)

Also If I comment the Italicized portion of the code, and uncomment the code written in the quoates, it gives me no error and also doesnot download the file into the local file system.

Please Help
15 years ago
Hi,

I want to invoke a servlet printing "hello world" from a simple java class.
I tried using following code fron the java class, but nothing happened.
URL url = new URL("http://hostname ortnumber/servlet path/servletname");

Could anyone tell me how to run a servlet from a simple java program.

Thanks
15 years ago
I want to write into the file in a Map format, like

Key Value
t1 hello
t2 hi

How can I write into a File in this format???
15 years ago
public class WrappersTest {
public static void main(String[] s) {
Integer i1 = new Integer(2);
Integer i2 = new Integer(2);
System.out.println(i1 == i2); // FALSE

Integer j1 = 2;
Integer j2 = 2;
System.out.println(j1 == j2); // TRUE

Integer k1 = 150;
Integer k2 = 150;
System.out.println(k1 == k2); // FALSE

Integer jj1 = 127;
Integer jj2 = 127;
System.out.println(jj1 == jj2); // TRUE

int jjj1 = 127;
Integer jjj2 = 127;
System.out.println(jjj1 == jjj2); // TRUE

Integer kk1 = 128;
Integer kk2 = 128;
System.out.println(kk1 == kk2); // FALSE

Integer kkk1 = 128;
int kkk2 = 128;
System.out.println(kkk1 == kkk2); // TRUE

Integer w1 = -128;
Integer w2 = -128;
System.out.println(w1 == w2); // TRUE

Integer m1 = -129;
Integer m2 = -129;
System.out.println(m1 == m2); // FALSE

int mm1 = -129;
Integer mm2 = -129;
System.out.println(mm1 == mm2); // TRUE

}
}
I know that certain primitives are always to be boxed into the same immutable wrapper objects. These objects are then CACHED and REUSED, with the expectation that these are commonly used objects. These special values are:The short and int values between -128 and 127.

Can you explain the part of code in Bold since they are out of range and give different outputs.
public class MyVector<E> {
E[] data = null;
public MyVector( int size ) {
data = new E[size];// Compile time error
}
public E get( int i ) {
return data[i];
}
public void set( int i, E val ) {
data[i] = val;
}
}

Why can't we create a generic array?
public class GenericStack<E> {
private int top;

private int size = 0;

E[] elements;

GenericStack() {
this(10);
}

GenericStack(int size) {
this.size = size;
top = -1;
elements = (E[]) new Object[size];
}

public <E> void push(E input) {
elements[++top] = input; //Compile time error
}
}

This code gives compile time error saying
Type mismatch: cannot convert from E to E

Could you explain this?

Also when I write the push method as:

public void push(E input) {
elements[++top] = input;
}
In this case there is no error.

What is the difference between the two syntaxes?
Object [] objArray =new Object[3];
int[] intArray =new int[10];

objArray=intArray; //Compile time error

Is there anyway to convert int[] to Object[] type?
Thanks for the reply but how can I refer to the variable AB of Interface A
in this example??
public interface A {
static int A = 1;
int AB = 1;
public void a();
}
public interface B {
static int B = 2;
int AB = 2;
public void b();
}
public interface C extends A,B {
int AB = 1;
public void c();
}
public class Cuse implements C {

public void a() {
System.out.println("CUse!a");
System.out.println("AB = " + A.AB);//compile time error
}

public void b() {
System.out.println("CUse!b");
}
public void c() {
System.out.println("CUse!c");
}

public static void main(String argv[]){
new Cuse().a();
System.out.println("A = " + A);
System.out.println("B = " + B);
}
}

I'm getting a compile time error saying The primitive type int of A does not have a field AB

Could you explain this error??
Hello there,
I wrote a piece of code in which I'm taking Input from Command line and writing this input to file in Eclipse with argument Hello.

However the code is able to create a file but unable to write any thing into it.

package WrapStreams;

import java.io.*;

public class BufferedWriteIntoFileFromStdInput {

public static void main(String[] args) {
File OutFileFromStdIn = new File("WrapStreams", "OutFileFromStd.txt");
BufferedReader bufRead = new BufferedReader(new InputStreamReader(
System.in));
BufferedWriter bufWrite;
try {
bufWrite = new BufferedWriter(new FileWriter(OutFileFromStdIn));

String line = null;

line = bufRead.readLine();
bufWrite.write(line);

bufRead.close();
bufWrite.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

When I run it on the command Line it gave me lots of errors.
Can any one help?
Hello there,
I found this piece of code in mock exams for SCJP5.0

abstract class Lcc{
String Leader;
public void getLeader(){
}
}
public class Ilea extends Lcc{
public static void main(String argv[]){
Ilea i = new Ilea();
i.lrb();
}
public void lrb(){
String s1= new String("one");
StringBuffer sb = new StringBuffer("one");
if(s1.equals(sb)){
System.out.println("equals");
}
}
}
Answer: No ouput

My Question is
Why isn't there any output when the strings are compared?
15 years ago
Hello there,
I found this piece of code on mock exams for SCJP5.0

abstract class Lcc{
String Leader;
public void getLeader(){
}
}
public class Ilea extends Lcc{
public static void main(String argv[]){
Ilea i = new Ilea();
i.lrb();
}
public void lrb(){
String s1= new String("one");
StringBuffer sb = new StringBuffer("one");
if(s1.equals(sb)){
System.out.println("equals");
}
}
}
Answer: No ouput

My Question is
Why isn't there any output when the strings are compared?
15 years ago
My question in when we use a synchronization block, the object on which the threads have to be synchronized should be static?

For Ex: In this code threads are synchronized on static object. So all the threads have common object monitor.
If I synchronize on object info then why aren't the threads notified when the count becomes equal to 3.

import java.util.Vector;

public class WaitAndNotify_2 extends Thread{

private String info;
static Integer monitor = new Integer(3);
static int count = 0;
static int max = 0;

public WaitAndNotify_2 (String info) {
this.info = info;
}

public void doTheJob() {
synchronized ( monitor ){
System.out.println(info + " is waiting");
count ++;
if ( count == 3 )
monitor.notifyAll();
else
try {
monitor.wait();
sleep(1000);
} catch ( Exception e ){
System.out.println(info +
": IllegalMonitorStateException");
}
System.out.println(info + " is awake!");
}
}


public void run () {
doTheJob();
}

public static void main (String args []) {
new WaitAndNotify_2("first").start();
new WaitAndNotify_2("second").start();
new WaitAndNotify_2("last").start();
}
}