当前位置: 代码迷 >> Java Web开发 >> jsp 写文件!!
  详细解决方案

jsp 写文件!!

热度:219   发布时间:2008-10-23 19:48:33.0
jsp 写文件!!
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<HTML>
<BODY>
<P> 在下面的表格输入成绩:
<FORM action="4_10.jsp" method=post name=form>
  <Table align="CENTER" Border>
     <TR>
            <TH  width=50> 姓名</TH>
            <TH  width=50> 数学</TH>
            <TH  width=50>英语</TH>
     </TR>
    <% int i=0;
       while(i<=6)
         {out.print("<TR>");
             out.print("<TD>");
                 out.print("<INPUT type=text name=name value=姓名>");
             out.print("</TD>");
             out.print("<TD>");
                 out.print("<INPUT type=text name=math value=0>");
             out.print("</TD>");
             out.print("<TD>");
                 out.print("<INPUT type=text name=english value=0>");
             out.print("</TD>");
          out.print("</TR>");
          i++;
        }
     %>
     <TR>
        <TD>
         <INPUT type=submit name="g" value="写入成绩" >
        </TD>
        <TD> Math</TD>
        <TD> English</TD>
     </TR>  
   </Table>
  </FORM>
  <%
     String name[]=request.getParameterValues("name");
     String math[]=request.getParameterValues("math");
     String english[]=request.getParameterValues("english");
    try{ File f=new File("C:\\Tomcat 5.0\\webapps\\ROOT\\examples\\wx\\chap4\\Students","student.txt");
         FileOutputStream o=new FileOutputStream(f);
         DataOutputStream DataOut=new DataOutputStream(o);
         for(int k=0;k<name.length;k++)
          {DataOut.writeUTF(name[k]);
           DataOut.writeUTF(math[k]);
           DataOut.writeUTF(english[k]);
          }
        DataOut.close();
        o.close();
      }
    catch(IOException e)
      {      }
    catch(NullPointerException ee)
     {     }
  %>
<P><BR>查看成绩单:
  <A href=showresult.jsp><BR> 连接到成绩单页面>
</BODY>
</HTML>
高手来看下,为什么我运行这个程序的时候不可以写入TXT 文件中的,是不是我的代码有什么错误的~!!!!!解答下啊!!谢谢!!
搜索更多相关主题的帖子: jsp  文件  

----------------解决方案--------------------------------------------------------
抛异常了,还有 如果是中文写入文件的是乱码
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ page import="java.io.*" %>
<!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=gb2312">
<title>写文件</title>
</head>
<body>
<%
    String name[] = request.getParameterValues("name");
    String math[] = request.getParameterValues("math");
    String english[] = request.getParameterValues("english");
    try {
        File f = new File("E:\\", "student.txt");
        FileOutputStream o = new FileOutputStream(f);
        DataOutputStream dataout = new DataOutputStream(o);
        if(name != null && math != null && english != null) {
            for (int k = 0; k < name.length; k++) {
                dataout.writeUTF(new String(name[k].getBytes("iso8859-1"))
);                dataout.writeUTF(math[k]);
                dataout.writeUTF(english[k]);
            }
        }
        dataout.close();
        o.close();
    } catch (IOException e) {
        e.printStackTrace() ;
    } catch (NullPointerException ex) {
        ex.printStackTrace() ;
    }
%>
在下面的表格输入成绩:
<form action="index.jsp" method="post" name="form">
<table align="center" border>
    <tr>
        <th width="50">姓名</th>
        <th width="50">数学</th>
        <th width="50">英语</th>
    </tr>
    <%
        int i = 0;
        while (i <= 6) {
            out.print("<tr>");
            out.print("<td>");
            out.print("<input type=text name=name value=姓名>");
            out.print("</td>");
            out.print("<td>");
            out.print("<input type=text name=math value=0>");
            out.print("</td>");
            out.print("<td>");
            out.print("<input type=text name=english value=0>");
            out.print("</td>");
            out.print("</tr>");
            i++;
        }
    %>
    <tr>
        <td><input type="submit" name="g" value="写入成绩"></td>
        <td>math</td>
        <td>English</td>
    </tr>
</table>
</form>
<p><br>查看成绩单: <a href="showresult.jsp">连接到成绩单页面</a>
</body>
</html>
----------------解决方案--------------------------------------------------------
回复 2# guoxhvip 的帖子
谢谢,以后多多指教!!!
----------------解决方案--------------------------------------------------------
  相关解决方案