Could you help me to create a file with JSP?

J

Jenny

Hi,

I have a html file and JSP file. The JSP file opens a c:\hi.txt file
and write a string into it. But when I click the submit button on the
html file to run the JSP file, it does not create the file. (The code
worked in a Java application, so there are no errors). Could you tell
me why it did not create the file with JSP? I am using tomcat 4 and
windows 2000 server on my PC.

Thanks a lot.

Below is html code:

<HTML>
<HEAD><TITLE>Credit Card Information Form</TITLE></HEAD>
<BODY>
<FORM ACTION="creditCardForm.jsp" METHOD="POST">
Credit Card Information
<P> Name: <INPUT TYPE="TEXT" NAME="name" SIZE="25">
<P> Credit Card Number: <INPUT TYPE="TEXT" NAME="number" SIZE="25">
<P> Credit Card Type:
<SELECT NAME="type">
<OPTION>Visa</OPTION>
<OPTION>Master Card</OPTION>
<OPTION SELECTED>Amex</OPTION>
</SELECT>
<P> Expiration Date:
<SELECT NAME="month">
<OPTION SELECTED>01</OPTION> <OPTION>02</OPTION>
<OPTION>03</OPTION> <OPTION>04</OPTION>
<OPTION>05</OPTION> <OPTION>06</OPTION>
<OPTION>07</OPTION> <OPTION>08</OPTION>
<OPTION>09</OPTION> <OPTION>10</OPTION>
<OPTION>11</OPTION> <OPTION>12</OPTION>
</SELECT>
<SELECT NAME="year">
<OPTION>2000</OPTION> <OPTION SELECTED>2001</OPTION>
<OPTION>2002</OPTION> <OPTION>2003</OPTION>
<OPTION>2004</OPTION> <OPTION>2005</OPTION>
</SELECT>
<P>
<INPUT TYPE="RESET" NAME="Reset" VALUE="Reset">
<INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Submit"></TD>
</FORM>
</BODY>

Below is JSP file:


<HTML><HEAD> <TITLE>Credit Card Processing JSP</TITLE>
</HEAD>
<BODY>
Verify Credit Card Information:
<%@ page import="java.io.*"%>
<%

try {
File f = new File("c://hell.txt");
FileOutputStream file = new FileOutputStream(f);
byte[] b = new byte[40];
b="Hello World.".getBytes();
file.write(b);
file.close();
} catch (Exception e) {
System.out.println("Error: " + e.toString());
}
%>
<UL>
<LI>Name = <%= request.getParameter("name") %>
<LI>Number = <%= request.getParameter("number") %>
<LI>Type = <%= request.getParameter("type") %>
<LI>Date = <%= request.getParameter("month") %>/
<%= request.getParameter("year")%>
</UL>
</BODY></HTML>
 
A

Anton Spaans

Jenny said:
Hi,

I have a html file and JSP file. The JSP file opens a c:\hi.txt file
and write a string into it. But when I click the submit button on the
html file to run the JSP file, it does not create the file. (The code
worked in a Java application, so there are no errors). Could you tell
me why it did not create the file with JSP? I am using tomcat 4 and
windows 2000 server on my PC.

Thanks a lot.

Below is html code:

<HTML>
<HEAD><TITLE>Credit Card Information Form</TITLE></HEAD>
<BODY>
<FORM ACTION="creditCardForm.jsp" METHOD="POST">
Credit Card Information
<P> Name: <INPUT TYPE="TEXT" NAME="name" SIZE="25">
<P> Credit Card Number: <INPUT TYPE="TEXT" NAME="number" SIZE="25">
<P> Credit Card Type:
<SELECT NAME="type">
<OPTION>Visa</OPTION>
<OPTION>Master Card</OPTION>
<OPTION SELECTED>Amex</OPTION>
</SELECT>
<P> Expiration Date:
<SELECT NAME="month">
<OPTION SELECTED>01</OPTION> <OPTION>02</OPTION>
<OPTION>03</OPTION> <OPTION>04</OPTION>
<OPTION>05</OPTION> <OPTION>06</OPTION>
<OPTION>07</OPTION> <OPTION>08</OPTION>
<OPTION>09</OPTION> <OPTION>10</OPTION>
<OPTION>11</OPTION> <OPTION>12</OPTION>
</SELECT>
<SELECT NAME="year">
<OPTION>2000</OPTION> <OPTION SELECTED>2001</OPTION>
<OPTION>2002</OPTION> <OPTION>2003</OPTION>
<OPTION>2004</OPTION> <OPTION>2005</OPTION>
</SELECT>
<P>
<INPUT TYPE="RESET" NAME="Reset" VALUE="Reset">
<INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Submit"></TD>
</FORM>
</BODY>

Below is JSP file:


<HTML><HEAD> <TITLE>Credit Card Processing JSP</TITLE>
</HEAD>
<BODY>
Verify Credit Card Information:
<%@ page import="java.io.*"%>
<%

try {
File f = new File("c://hell.txt");
FileOutputStream file = new FileOutputStream(f);
byte[] b = new byte[40];
b="Hello World.".getBytes();
file.write(b);
file.close();
} catch (Exception e) {
System.out.println("Error: " + e.toString());
}
%>
<UL>
<LI>Name = <%= request.getParameter("name") %>
<LI>Number = <%= request.getParameter("number") %>
<LI>Type = <%= request.getParameter("type") %>
<LI>Date = <%= request.getParameter("month") %>/
<%= request.getParameter("year")%>
</UL>
</BODY></HTML>

Should this

File f = new File("c://hell.txt");

not be changed into

File f = new File("c:\\hell.txt");

?

-- Anton.
 
J

Jenny

Both should work under windows. This is not the problem. I was told
that for some OS, such as UNIX, "\\" would fail. But I do not have
UNIX to test.
 
W

Wendy S

Jenny said:
I have a html file and JSP file. The JSP file opens a c:\hi.txt file
and write a string into it. But when I click the submit button on the
html file to run the JSP file, it does not create the file. (The code
worked in a Java application, so there are no errors). Could you tell
me why it did not create the file with JSP? I am using tomcat 4 and
windows 2000 server on my PC.

Much as it pained me to do so, (scriptlets!) I pasted your code (actually
just the part that writes the file) and it worked fine. I now have a
'hell.txt' file on my C drive containing 'Hello World.' Are you certain the
JSP code is getting executed? Try deleting everything under the 'work'
directory so Tomcat will re-compile your JSP. Is there anything on the
Tomcat console or in the log files? Make sure that the user Tomcat is
running as has write permissions to the directory you're writing to.
Refresh the directory listing to make sure the file really isn't there.
 
A

anonymous

Jenny said:
Hi,

I have a html file and JSP file. The JSP file opens a c:\hi.txt file
and write a string into it. But when I click the submit button on the
html file to run the JSP file, it does not create the file. (The code
worked in a Java application, so there are no errors). Could you tell
me why it did not create the file with JSP? I am using tomcat 4 and
windows 2000 server on my PC.

Thanks a lot.

Below is html code:

<HTML>
<HEAD><TITLE>Credit Card Information Form</TITLE></HEAD>
<BODY>
<FORM ACTION="creditCardForm.jsp" METHOD="POST">
Credit Card Information
<P> Name: <INPUT TYPE="TEXT" NAME="name" SIZE="25">
<P> Credit Card Number: <INPUT TYPE="TEXT" NAME="number" SIZE="25">
<P> Credit Card Type:
<SELECT NAME="type">
<OPTION>Visa</OPTION>
<OPTION>Master Card</OPTION>
<OPTION SELECTED>Amex</OPTION>
</SELECT>
<P> Expiration Date:
<SELECT NAME="month">
<OPTION SELECTED>01</OPTION> <OPTION>02</OPTION>
<OPTION>03</OPTION> <OPTION>04</OPTION>
<OPTION>05</OPTION> <OPTION>06</OPTION>
<OPTION>07</OPTION> <OPTION>08</OPTION>
<OPTION>09</OPTION> <OPTION>10</OPTION>
<OPTION>11</OPTION> <OPTION>12</OPTION>
</SELECT>
<SELECT NAME="year">
<OPTION>2000</OPTION> <OPTION SELECTED>2001</OPTION>
<OPTION>2002</OPTION> <OPTION>2003</OPTION>
<OPTION>2004</OPTION> <OPTION>2005</OPTION>
</SELECT>
<P>
<INPUT TYPE="RESET" NAME="Reset" VALUE="Reset">
<INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Submit"></TD>
</FORM>
</BODY>

Below is JSP file:


<HTML><HEAD> <TITLE>Credit Card Processing JSP</TITLE>
</HEAD>
<BODY>
Verify Credit Card Information:
<%@ page import="java.io.*"%>
<%

try {
File f = new File("c://hell.txt");
FileOutputStream file = new FileOutputStream(f);
byte[] b = new byte[40];
b="Hello World.".getBytes();
file.write(b);
file.close();
} catch (Exception e) {
System.out.println("Error: " + e.toString());
}
%>
<UL>
<LI>Name = <%= request.getParameter("name") %>
<LI>Number = <%= request.getParameter("number") %>
<LI>Type = <%= request.getParameter("type") %>
<LI>Date = <%= request.getParameter("month") %>/
<%= request.getParameter("year")%>
</UL>
</BODY></HTML>
try to send the exception getMessage() result back to the browser to see
if there is a problem writing to the file (out.println(e.getMessage())

Does your environment allow single step debugging? If so, try it!
 
J

Jenny

Can you tell me an environment allow single step debugging? I have
Ideal 4.0. But I do not use it for JSP. I use DW.
 
R

Ryan Stewart

Jenny said:
Both should work under windows. This is not the problem. I was told
that for some OS, such as UNIX, "\\" would fail. But I do not have
UNIX to test.
Using File.pathSeparator would still be much better than hardcoding it.
 
A

anonymous

Jenny said:
Can you tell me an environment allow single step debugging? I have
Ideal 4.0. But I do not use it for JSP. I use DW.
I use Oracle JDeveloper
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top