Solved

upload file and write content to form: Java Servlet

Posted on 2007-11-25
8
243 Views
Last Modified: 2010-03-30
hello
I am trying to learn my few new servlet programming and have difficult how to upload the text file content back to the form
Please help


package dtrand;
 
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.lang.*;
import java.net.*;
import java.util.*;
 
public class dtrandH8 extends HttpServlet {
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
 
            // begin JS           
            out.println("          <SCRIPT type=\"text/javascript\" >");
 
            out.println("function setFocus(){");
            out.println("form1.title.focus();");
            out.println("}");
 
            out.println("function validate(){");
            out.println("if (form1.value == null) {");
            out.println("return false;");
            out.println("}");
            out.println("return true;");
            out.println("}");
            // NAVIGATION
            out.println("//---------------------------- NAVGIGATION");
            out.println("var countNav = 1;");
            out.println("function addRowNav(theLink){");
            out.println("theRow = theLink.parentNode.parentNode;");
            out.println("theBod = theRow.parentNode;");
            out.println("var rowId = theBod.rows[theBod.rows.length - 1].cells[0].firstChild.id;");
            out.println("rowId = rowId.substring(0, 3) + (parseInt(rowId.substring(3, rowId.length)) + 1);");
            out.println("theTable = theRow.parentNode;");
            out.println("newRow = theRow.cloneNode(true);");
            out.println("theBod.appendChild(newRow);");
            out.println("newRow.cells[0].firstChild.id = rowId;");
            out.println("var tempName = theBod.rows[0].cells[0].firstChild.name;");
            out.println("newRow.cells[0].firstChild.name = tempName;");
            out.println("countNav++;");
            out.println("document.forms.form1.hiddenField1.value=countNav.toString();");
            out.println("return false;");
            out.println("}");
 
            out.println("function removeRowNav(theLink){");
            out.println("theRow = theLink.parentNode.parentNode;");
            out.println("theBod = theRow.parentNode;");
            out.println("theBod.removeChild(theRow)");
            out.println("countNav--;");
            out.println("document.forms.form1.hiddenField1.value=countNav.toString();");
            out.println("}");
            // COL
            out.println("//---------------------------- COL");
            out.println("var countCol = 1;");
 
            out.println("function addRowCol(theLink){");
            out.println("theRow = theLink.parentNode.parentNode;");
            out.println("theBod = theRow.parentNode;");
            out.println("var rowId = theBod.rows[theBod.rows.length - 1].cells[0].firstChild.id;");
            out.println("rowId = rowId.substring(0, 3) + (parseInt(rowId.substring(3, rowId.length)) + 1);");
            out.println("theTable = theRow.parentNode;");
            out.println("newRow = theRow.cloneNode(true);");
            out.println("theBod.appendChild(newRow);");
            out.println("newRow.cells[0].firstChild.id = rowId;");
            out.println("var tempNameCol = theBod.rows[0].cells[0].firstChild.name;");
            out.println("newRow.cells[0].firstChild.name = tempNameCol;");
            out.println("countCol++;");
            out.println("document.forms.form1.hiddenField2.value=countCol.toString();");
            out.println("return false;");
            out.println("}");
 
            out.println("function removeRowCol(theLink){");
            out.println("theRow = theLink.parentNode.parentNode;");
            out.println("theBod = theRow.parentNode;");
            out.println("theBod.removeChild(theRow)");
            out.println("countCol--;");
            out.println("document.forms.form1.hiddenField2.value=countCol.toString();");
            out.println("}");
// ROW
            out.println("//---------------------------- ROW");
            out.println("var countRow = 1;");
 
            out.println("function addRowRow(theLink){");
            out.println("theRow = theLink.parentNode.parentNode;");
            out.println("theBod = theRow.parentNode;");
            out.println("var rowId = theBod.rows[theBod.rows.length - 1].cells[0].firstChild.id;");
            out.println("rowId = rowId.substring(0, 3) + (parseInt(rowId.substring(3, rowId.length)) + 1);");
            out.println("theTable = theRow.parentNode;");
            out.println("newRow = theRow.cloneNode(true);");
            out.println("theBod.appendChild(newRow);");
            out.println("newRow.cells[0].firstChild.id = rowId;");
            out.println("var tempNameRow = theBod.rows[0].cells[0].firstChild.name;");
            out.println("newRow.cells[0].firstChild.name = tempNameRow;");
            out.println("countRow++;");
            out.println("document.forms.form1.hiddenField3.value=countRow.toString();");
            out.println("return false;");
            out.println("}");
 
            out.println("function removeRowRow(theLink){");
            out.println("theRow = theLink.parentNode.parentNode;");
            out.println("theBod = theRow.parentNode;");
            out.println("theBod.removeChild(theRow)");
            out.println("countRow--;");
            out.println("document.forms.form1.hiddenField3.value=countRow.toString();");
            out.println("}");
 
            out.println("</SCRIPT>");
            // end script
 
            out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
            out.println("<title>Data Entry HTML Forms</title>");
            out.println("<style type=\"text/css\">");
            out.println("<!--");
            out.println("a:link {    text-decoration: none; }");
            out.println("a:visited {    text-decoration: none;}");
            out.println("a:hover {    text-decoration: none;}");
            out.println("a:active {    text-decoration: none;}");
            out.println("-->");
            out.println("</style>");
            out.println("</head>");
            out.println("<body id='myBody' onLoad=\"setFocus()\">");
            out.println("<table width=\"90%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">");
            out.println("<tr>");
            out.println("<td><h1 align=\"center\" class=\"style1\">Content Management Data Entry</h1>");
            out.println("<form name=\"form1\" method=\"post\" action=\"dtrandH8\" onSubmit=\"validate()\">");
            // TITLE FIELD
            out.println("<h3>1. Title of the page : ");
            out.println("<input name=\"title\" type=\"text\" id=\"title\" size=\"100\">");
            // Top NAVIGATION
            out.println("</h3>");
            out.println("<h3>2. Top Navigation </h3>");
            out.println("<p>Please specify the link you wanted in the text field as link follows by full URL (http://). Hit the + to add more field and - to delete</p>");
            out.println("<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\">");
            out.println("<tr>");
            out.println("<td><input type=\"text\" size=\"40\" value=\"Link Descrition\" name=\"navTxt\"></td>");
            out.println("<td><input type=\"text\" size=\"60\" value=\"http://\" name=\"navTxtLink\"></td>");
            out.println("<td><a href=\"#\" onClick=\"addRowNav(this);\">+</a></td>");
            out.println("<td><a href=\"#\" onClick=\"removeRowNav(this);\">-</a></td>");
            out.println("</tr>");
            out.println("</table>");
            out.println("<p>");
            out.println("<input type=\"hidden\" name=\"hiddenField1\" id=\"hiddenField1\" value=\"1\">");
            out.println("</p>");
            // COL HEADER
            out.println("<h3>3. Column Headers</h3>");
            out.println("<p>Please specify the column header here. Hit the + to add more field and - to delete</p>");
            out.println("<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\">");
            out.println("<tr>");
            out.println("<td><input type=\"text\" size=\"30\" value= \"Width\" name=\"colTxtWidth\" /></td>");
            out.println("<td><input type=\"text\" size=\"70\" value= \"Content\" name=\"colTxt\" /></td>");
            out.println("<td><a href=\"#\" onclick=\"addRowCol(this);\">+</a></td>");
            out.println("<td><a href=\"#\" onclick=\"removeRowCol(this);\">-</a></td>");
            out.println("</tr>");
            out.println("</table>");
            out.println("<p>");
            out.println("<input type=\"hidden\" name=\"hiddenField2\" id=\"hiddenField2\" value=\"1\">");
            out.println("</p>");
            // ROW DATA
            out.println("<h3>4. Row Data</h3>");
            out.println("<p>Please specify the row data for each column here</p>");
            out.println("<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\">");
            out.println("  <tr>");
            out.println("<td><input type=\"text\" size=\"70\" value =\" row data here\" name=\"rowTxt\" /></td>");
            out.println("<td><a href=\"#\" onclick=\"addRowRow(this);\">+</a></td>");
            out.println("<td><a href=\"#\" onclick=\"removeRowRow(this);\">-</a></td>");
            out.println("</tr>");
            out.println("</table>");
            out.println("<p>");
            out.println("<input type=\"hidden\" name=\"hiddenField3\" id=\"hiddenField3\" value=\"1\">");
            out.println("</p>");
// FOOTER INFO PART            
            out.println("<h3>5. Footer info</h3>");
            out.println("  <label>");
            out.println("  <input name=\"footerInfoPlacement\" type=\"text\" id=\"footerInfoPlacement\" value=\"left,center,right\" size=\"30\" />");
            out.println("  <input name=\"footerInfoData\" type=\"text\" id=\"footerInfoData\" value=\"Please enter the data you wanted\" size=\"70\" />");
            out.println("  </label>");
            out.println("  <label>");
            out.println("  <input name=\"footerLink\" type=\"text\" id=\"footerLink\" value=\"Please enter additional footer link if you want.  \" size=\"40\" />");
            out.println("  <input name=\"footerLinkURL\" type=\"text\" id=\"footerLinkURL\" value=\"Enter in full form: http://www \" size=\"60\" />");
            out.println("</label>");
            out.println("<h3>6. Load File</h3>");
            out.println("<P>");
            out.println("<STRONG>Upload your file: </STRONG>");
            out.println("<INPUT Type=file Name=myfile >");
            out.println("</P>");
            out.println("<input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Submit\">");
            out.println("</p>");
            out.println("<p>&nbsp;</p>");
            out.println("</form>");
            out.println("  </tr>");
            out.println("</table>");
            out.println("<p>This site is created by Duc Tran as homework for SWE 432 Fall 2007</p>");
            out.println("<p>Please contact me at <a href=\"mailto:dtrand@gmu.edu\">dtrand@gmu.edu</a></p>");
            out.println("</body>");
            out.println("</html>");
 
        } finally {
            out.close();
        }
 
    }
    // Implement doPost()
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("TEXT/HTML");
        PrintWriter out = response.getWriter();
 
 
        out.println("<head>");
        out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
 
        // TITLE PART
        String title = (String) request.getParameter("title");
 
        out.println("<title>" + title + "</title>");
        out.println("</head>");
        out.println("<body>");
 
        // NAV PART
        String navCountString = (String) request.getParameter("hiddenField1");
 
        int navCount = Integer.parseInt(navCountString);
 
        String[] navTxtA = new String[200];
        String[] navTxtLinkA = new String[200];
        navTxtA = request.getParameterValues("navTxt");
        navTxtLinkA = request.getParameterValues("navTxtLink");
        out.println("<p align=\"center\">");
        for (int i = 0; i < navCount; i++) {
            out.println("<A href=" + navTxtLinkA[i] + ">" + navTxtA[i] + "</A>");
            out.println("&nbsp;&nbsp;&nbsp;");
        }
        out.println("</p>");
        out.println("<br>");
// MAIN TABLE PART
// GETTING INFO
 
        String numberOfColString = (String) request.getParameter("hiddenField2");
        int numberOfCol = Integer.parseInt(numberOfColString);
        String numberOfRowString = (String) request.getParameter("hiddenField3");
        int numberOfRow = Integer.parseInt(numberOfRowString);
        String[] colTxtWidthA = new String[200];
        String[] colTxtA = new String[200];
        String[] rowTxtA = new String[200];
        colTxtWidthA = request.getParameterValues("colTxtWidth");
        colTxtA = request.getParameterValues("colTxt");
        rowTxtA = request.getParameterValues("rowTxt");
// DRAW TABLE
 
        out.println("<table align=\"center\" width=\"80%\" border=\"1\">");
        out.println("<THEAD>");
        out.println("<TR>");
        for (int i = 0; i < numberOfCol; i++) {
            out.println("<TH align=\"center\" width=" + colTxtWidthA[i] + "><B>" + colTxtA[i] + "</B>");
        }
        out.println("</TR>");
 
        out.println("<TBODY>");
 
        int count = 0;
        out.println("<TR>");
        while (count < numberOfRow) {
 
            for (int j = 0; j < numberOfCol; j++) {
                out.println("<TD align=\"center\">" + rowTxtA[count]);
            }
            out.println("</TR>");
            count++;
 
        }
        out.println("</TBODY>");
        out.println("</table>");
// END TABLE
// FOOTER PART
        String footerInfoPlacementString = (String) request.getParameter("footerInfoPlacement");
        String footerInfoDataString = (String) request.getParameter("footerInfoData");
        String footerLinkString = (String) request.getParameter("footerLink");
        String footerLinkURLString = (String) request.getParameter("footerLinkURL");
        out.println("<p align=" + footerInfoPlacementString + ">" + footerInfoDataString + "</p>");
        out.println("<A href=" + footerLinkURLString + ">" + footerLinkString + "</A>");
        out.println("<p>This page is created by Duc Tran for SWE 432 as homework 8</p>");
        out.println("</body>");
        out.println("</html>");
        FileWriter outfile = new FileWriter("/Users/dtrand/Documents/GMU/swe432/BiServlet-1/build/web/myfile");
        for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            outfile.write(name + "=" + request.getParameter(name));
            outfile.write("\n");
        }
        outfile.close();
    }
}

Open in new window

0
Comment
Question by:dminh01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 17

Expert Comment

by:contactkarthi
ID: 20348088
below are the links to read from a property file

http://www.exampledepot.com/egs/java.util/Props.html
http://www.exampledepot.com/egs/java.util/GetSetProps.html?l=rel



Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("filename.properties"));
    } catch (IOException e) {
    }
String string = properties.getProperty("a");

so you should be able to set the value   :)
0
 

Author Comment

by:dminh01
ID: 20348119
what does it mean by a property file. do i have to write using that property file

thanks
0
 
LVL 17

Expert Comment

by:contactkarthi
ID: 20348138
property file is nothing but a simple text file

if you have the values in the text file like

username=scott
password=tiger

and lets assume the name of the file is data.txt


Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("data.txt"));
    } catch (IOException e) {
    }
String string1 = properties.getProperty("username");
String string2= properties.getProperty("password");
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

Author Comment

by:dminh01
ID: 20348149
so since I use like this to write to myfile, can I use your method to load the file?
Also I have added a button upload file to my servlet. How should I code that button so that it will upload the textfile content to my form

thanks i am so new to java here
package dtrand;
 
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.lang.*;
import java.net.*;
import java.util.*;
 
public class dtrandH8 extends HttpServlet {
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
 
            // begin JS           
            out.println("          <SCRIPT type=\"text/javascript\" >");
 
            out.println("function setFocus(){");
            out.println("form1.title.focus();");
            out.println("}");
 
            out.println("function validate(){");
            out.println("if (form1.value == null) {");
            out.println("return false;");
            out.println("}");
            out.println("return true;");
            out.println("}");
            // NAVIGATION
            out.println("//---------------------------- NAVGIGATION");
            out.println("var countNav = 1;");
            out.println("function addRowNav(theLink){");
            out.println("theRow = theLink.parentNode.parentNode;");
            out.println("theBod = theRow.parentNode;");
            out.println("var rowId = theBod.rows[theBod.rows.length - 1].cells[0].firstChild.id;");
            out.println("rowId = rowId.substring(0, 3) + (parseInt(rowId.substring(3, rowId.length)) + 1);");
            out.println("theTable = theRow.parentNode;");
            out.println("newRow = theRow.cloneNode(true);");
            out.println("theBod.appendChild(newRow);");
            out.println("newRow.cells[0].firstChild.id = rowId;");
            out.println("var tempName = theBod.rows[0].cells[0].firstChild.name;");
            out.println("newRow.cells[0].firstChild.name = tempName;");
            out.println("countNav++;");
            out.println("document.forms.form1.hiddenField1.value=countNav.toString();");
            out.println("return false;");
            out.println("}");
 
            out.println("function removeRowNav(theLink){");
            out.println("theRow = theLink.parentNode.parentNode;");
            out.println("theBod = theRow.parentNode;");
            out.println("theBod.removeChild(theRow)");
            out.println("countNav--;");
            out.println("document.forms.form1.hiddenField1.value=countNav.toString();");
            out.println("}");
            // COL
            out.println("//---------------------------- COL");
            out.println("var countCol = 1;");
 
            out.println("function addRowCol(theLink){");
            out.println("theRow = theLink.parentNode.parentNode;");
            out.println("theBod = theRow.parentNode;");
            out.println("var rowId = theBod.rows[theBod.rows.length - 1].cells[0].firstChild.id;");
            out.println("rowId = rowId.substring(0, 3) + (parseInt(rowId.substring(3, rowId.length)) + 1);");
            out.println("theTable = theRow.parentNode;");
            out.println("newRow = theRow.cloneNode(true);");
            out.println("theBod.appendChild(newRow);");
            out.println("newRow.cells[0].firstChild.id = rowId;");
            out.println("var tempNameCol = theBod.rows[0].cells[0].firstChild.name;");
            out.println("newRow.cells[0].firstChild.name = tempNameCol;");
            out.println("countCol++;");
            out.println("document.forms.form1.hiddenField2.value=countCol.toString();");
            out.println("return false;");
            out.println("}");
 
            out.println("function removeRowCol(theLink){");
            out.println("theRow = theLink.parentNode.parentNode;");
            out.println("theBod = theRow.parentNode;");
            out.println("theBod.removeChild(theRow)");
            out.println("countCol--;");
            out.println("document.forms.form1.hiddenField2.value=countCol.toString();");
            out.println("}");
// ROW
            out.println("//---------------------------- ROW");
            out.println("var countRow = 1;");
 
            out.println("function addRowRow(theLink){");
            out.println("theRow = theLink.parentNode.parentNode;");
            out.println("theBod = theRow.parentNode;");
            out.println("var rowId = theBod.rows[theBod.rows.length - 1].cells[0].firstChild.id;");
            out.println("rowId = rowId.substring(0, 3) + (parseInt(rowId.substring(3, rowId.length)) + 1);");
            out.println("theTable = theRow.parentNode;");
            out.println("newRow = theRow.cloneNode(true);");
            out.println("theBod.appendChild(newRow);");
            out.println("newRow.cells[0].firstChild.id = rowId;");
            out.println("var tempNameRow = theBod.rows[0].cells[0].firstChild.name;");
            out.println("newRow.cells[0].firstChild.name = tempNameRow;");
            out.println("countRow++;");
            out.println("document.forms.form1.hiddenField3.value=countRow.toString();");
            out.println("return false;");
            out.println("}");
 
            out.println("function removeRowRow(theLink){");
            out.println("theRow = theLink.parentNode.parentNode;");
            out.println("theBod = theRow.parentNode;");
            out.println("theBod.removeChild(theRow)");
            out.println("countRow--;");
            out.println("document.forms.form1.hiddenField3.value=countRow.toString();");
            out.println("}");
 
            out.println("</SCRIPT>");
            // end script
 
            out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
            out.println("<title>Data Entry HTML Forms</title>");
            out.println("<style type=\"text/css\">");
            out.println("<!--");
            out.println("a:link {    text-decoration: none; }");
            out.println("a:visited {    text-decoration: none;}");
            out.println("a:hover {    text-decoration: none;}");
            out.println("a:active {    text-decoration: none;}");
            out.println("-->");
            out.println("</style>");
            out.println("</head>");
            out.println("<body id='myBody' onLoad=\"setFocus()\">");
            out.println("<table width=\"90%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">");
            out.println("<tr>");
            out.println("<td><h1 align=\"center\" class=\"style1\">Content Management Data Entry</h1>");
            out.println("<form name=\"form1\" method=\"post\" action=\"dtrandH8\" onSubmit=\"validate()\">");
            // TITLE FIELD
            out.println("<h3>1. Title of the page : ");
            out.println("<input name=\"title\" type=\"text\" id=\"title\" size=\"100\">");
            // Top NAVIGATION
            out.println("</h3>");
            out.println("<h3>2. Top Navigation </h3>");
            out.println("<p>Please specify the link you wanted in the text field as link follows by full URL (http://). Hit the + to add more field and - to delete</p>");
            out.println("<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\">");
            out.println("<tr>");
            out.println("<td><input type=\"text\" size=\"40\" value=\"Link Descrition\" name=\"navTxt\"></td>");
            out.println("<td><input type=\"text\" size=\"60\" value=\"http://\" name=\"navTxtLink\"></td>");
            out.println("<td><a href=\"#\" onClick=\"addRowNav(this);\">+</a></td>");
            out.println("<td><a href=\"#\" onClick=\"removeRowNav(this);\">-</a></td>");
            out.println("</tr>");
            out.println("</table>");
            out.println("<p>");
            out.println("<input type=\"hidden\" name=\"hiddenField1\" id=\"hiddenField1\" value=\"1\">");
            out.println("</p>");
            // COL HEADER
            out.println("<h3>3. Column Headers</h3>");
            out.println("<p>Please specify the column header here. Hit the + to add more field and - to delete</p>");
            out.println("<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\">");
            out.println("<tr>");
            out.println("<td><input type=\"text\" size=\"30\" value= \"Width %\" name=\"colTxtWidth\" /></td>");
            out.println("<td><input type=\"text\" size=\"70\" value= \"Content\" name=\"colTxt\" /></td>");
            out.println("<td><a href=\"#\" onclick=\"addRowCol(this);\">+</a></td>");
            out.println("<td><a href=\"#\" onclick=\"removeRowCol(this);\">-</a></td>");
            out.println("</tr>");
            out.println("</table>");
            out.println("<p>");
            out.println("<input type=\"hidden\" name=\"hiddenField2\" id=\"hiddenField2\" value=\"1\">");
            out.println("</p>");
            // ROW DATA
            out.println("<h3>4. Row Data</h3>");
            out.println("<p>Please specify the row data for each column here</p>");
            out.println("<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\">");
            out.println("  <tr>");
            out.println("<td><input type=\"text\" size=\"70\" value =\" row data here\" name=\"rowTxt\" /></td>");
            out.println("<td><a href=\"#\" onclick=\"addRowRow(this);\">+</a></td>");
            out.println("<td><a href=\"#\" onclick=\"removeRowRow(this);\">-</a></td>");
            out.println("</tr>");
            out.println("</table>");
            out.println("<p>");
            out.println("<input type=\"hidden\" name=\"hiddenField3\" id=\"hiddenField3\" value=\"1\">");
            out.println("</p>");
// FOOTER INFO PART            
            out.println("<h3>5. Footer info</h3>");
            out.println("  <label>");
            out.println("  <input name=\"footerInfoPlacement\" type=\"text\" id=\"footerInfoPlacement\" value=\"left,center,right\" size=\"30\" />");
            out.println("  <input name=\"footerInfoData\" type=\"text\" id=\"footerInfoData\" value=\"Please enter the data you wanted\" size=\"70\" />");
            out.println("  </label>");
            out.println("  <label>");
            out.println("  <input name=\"footerLink\" type=\"text\" id=\"footerLink\" value=\"Please enter additional footer link if you want.  \" size=\"40\" />");
            out.println("  <input name=\"footerLinkURL\" type=\"text\" id=\"footerLinkURL\" value=\"Enter in full form: http://www \" size=\"60\" />");
            out.println("</label>");
            out.println("<h3>6. Load File</h3>");
            out.println("<P>");
            out.println("<STRONG>Upload your file: </STRONG>");
            out.println("<INPUT Type=file Name=myfile >");
            out.println("<INPUT  Name=\"submit\" Type=\"submit\" Value=\"UPLOAD\">");
            out.println("</P>");
            out.println("<input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Submit\">");
            out.println("</p>");
            out.println("<p>&nbsp;</p>");
            out.println("</form>");
            out.println("  </tr>");
            out.println("</table>");
            out.println("<p>This site is created by Duc Tran as homework for SWE 432 Fall 2007</p>");
            out.println("<p>Please contact me at <a href=\"mailto:dtrand@gmu.edu\">dtrand@gmu.edu</a></p>");
            out.println("</body>");
            out.println("</html>");
 
        } finally {
            out.close();
        }
 
    }
    // Implement doPost()
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("TEXT/HTML");
        PrintWriter out = response.getWriter();
        FileWriter outfile = new FileWriter("/Users/dtrand/Documents/GMU/swe432/BiServlet-1/build/web/myfile");
 
        out.println("<head>");
        out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
 
        // TITLE PART
        String title = (String) request.getParameter("title");
        outfile.write(title);
        out.println("<title>" + title + "</title>");
        out.println("</head>");
        out.println("<body>");
 
        // NAV PART
        String navCountString = (String) request.getParameter("hiddenField1");
        outfile.write(navCountString);
        int navCount = Integer.parseInt(navCountString);
 
        String[] navTxtA = new String[200];
        String[] navTxtLinkA = new String[200];
        navTxtA = request.getParameterValues("navTxt");
        navTxtLinkA = request.getParameterValues("navTxtLink");
        out.println("<p align=\"center\">");
        for (int i = 0; i < navCount; i++) {
            out.println("<A href=" + navTxtLinkA[i] + ">" + navTxtA[i] + "</A>");
            out.println("&nbsp;&nbsp;&nbsp;");
            outfile.write(navTxtLinkA[i]);
            outfile.write(navTxtA[i]);
        }
        out.println("</p>");
        out.println("<br>");
// MAIN TABLE PART
// GETTING INFO
 
        String numberOfColString = (String) request.getParameter("hiddenField2");
        outfile.write(numberOfColString);
        int numberOfCol = Integer.parseInt(numberOfColString);
        String numberOfRowString = (String) request.getParameter("hiddenField3");
        outfile.write(numberOfRowString);
        int numberOfRow = Integer.parseInt(numberOfRowString);
        String[] colTxtWidthA = new String[200];
        String[] colTxtA = new String[200];
        String[] rowTxtA = new String[200];
        colTxtWidthA = request.getParameterValues("colTxtWidth");
        colTxtA = request.getParameterValues("colTxt");
        rowTxtA = request.getParameterValues("rowTxt");
// DRAW TABLE
 
        out.println("<table align=\"center\" width=\"80%\" border=\"1\">");
        out.println("<THEAD>");
        out.println("<TR>");
        for (int i = 0; i < numberOfCol; i++) {
            out.println("<TH align=\"center\" width=" + colTxtWidthA[i] + "%><B>" + colTxtA[i] + "</B>");
            outfile.write(colTxtWidthA[i]);
            outfile.write(colTxtA[i]);
        }
        out.println("</TR>");
 
        out.println("<TBODY>");
 
        int count = 0;
        out.println("<TR>");
        while (count < numberOfRow) {
 
            for (int j = 0; j < numberOfCol; j++) {
                out.println("<TD align=\"center\">" + rowTxtA[count]);
                outfile.write(rowTxtA[count]);
            }
            out.println("</TR>");
            count++;
 
        }
        out.println("</TBODY>");
        out.println("</table>");
// END TABLE
// FOOTER PART
        String footerInfoPlacementString = (String) request.getParameter("footerInfoPlacement");
        outfile.write(footerInfoPlacementString);
        String footerInfoDataString = (String) request.getParameter("footerInfoData");
        outfile.write(footerInfoDataString);
        String footerLinkString = (String) request.getParameter("footerLink");
        outfile.write(footerLinkString);
        String footerLinkURLString = (String) request.getParameter("footerLinkURL");
        outfile.write(footerLinkURLString);
 
        out.println("<p align=" + footerInfoPlacementString + ">" + footerInfoDataString + "</p>");
        out.println("<A href=" + footerLinkURLString + ">" + footerLinkString + "</A>");
        out.println("<p>This page is created by Duc Tran for SWE 432 as homework 8</p>");
        out.println("</body>");
        out.println("</html>");
        outfile.close();
     /*   
        PrintWriter out2 = new PrintWriter(new FileWriter("formvals.properties"));
        for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            out.println(name + "=" + request.getParameter(name));
        }
        out2.close();
      */ 
    }
}		

Open in new window

0
 
LVL 17

Expert Comment

by:contactkarthi
ID: 20348245
instead of writing like

outfile.write(navCountString);
        outfile.write(numberOfColString);
        outfile.write(numberOfRowString);
            outfile.write(colTxtWidthA[i]);
            outfile.write(colTxtA[i]);

write it like

outfile.write("navCountString="+navCountString);
        outfile.write("numberOfColString="+numberOfColString);
        outfile.write("numberOfRowString="+numberOfRowString);
            outfile.write("colTxtWidthA["+i+"]="+colTxtWidthA[i]);
            outfile.write("colTxtA["+i+"]="+colTxtA[i]);

so it will beasy to read like
       

Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("/Users/dtrand/Documents/GMU/swe432/BiServlet-1/build/web/myfile"));
    } catch (IOException e) {
    }

String navCountString1 = properties.getProperty("navCountString");
String numberOfColString1 = properties.getProperty("numberOfColString");

and so on....
0
 

Author Comment

by:dminh01
ID: 20348268
I got this part. However one more problem arise is how do I program the UPLOAD button to execute the Java Code to load the file and print it out. Do I have to use JavaScript to call?If so how. Where to put the load file part? doGet or doPost?

Thanks
0
 
LVL 17

Accepted Solution

by:
contactkarthi earned 500 total points
ID: 20350020
you cannot do it with javascript.

you can have a new servlet class for that. based on the form method.. you can write the code in doGet or doPost.. i see for method=post in your code so you put the code in doPost
0
 

Author Comment

by:dminh01
ID: 20351739
hello
I try to use onclick="dtrandLoadFile();"
then write the JS like this
  // LOAD FILE
            out.println("function loadFile(){");
            out.println("document.forms.form1.action=dtrandLoadFile;");
            out.println("}");
            // SAVE FILE
            out.println("function saveFile(){");
            out.println("document.forms.form1.action=dtrandSaveFile;");
            out.println("}");
but it still does not write the file. I have not tried to upload yet. Here is the code for Save File

Please help

package dtrand;
 
import java.io.*;
import java.net.*;
 
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
 
/**
 *
 * @author dtrand
 */
public class dtrandSaveFile extends HttpServlet {
 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        FileWriter outfile=new FileWriter("/Users/dtrand/Documents/GMU/swe432/HW9/build/web/savedfile");
        for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            outfile.write(name + "=" + request.getParameter(name));
            outfile.write("\n");
        }
        outfile.close();
    }
 
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
 
    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
 
    /** 
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question