In my program I want to upload a file along with other details.
I've dowloaded org.apache.commons.fileupload.* jar files and everything works fine only if I give
<INPUT TYPE='file' NAME='file1'>
i.e the file is uploaded
But if I add any other field it becomes blank
Title : <INPUT id=text1 maxlength="30" name=GHN1 >
the value of GHN1 becomes blank. what cud be the problem.
Here is my program
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*,java.text.SimpleDateFormat.*,java.text.ParseException.*,java.text.*,java.text.DateFormat.*,java.util.*,java.util.Date.*,com.gh.db.*,org.apache.commons.fileupload.*,org.apache.commons.fileupload.servlet.ServletFileUpload,org.apache.commons.fileupload.disk.DiskFileItemFactory,org.apache.commons.io.FilenameUtils,java.io.*,java.io.File,java.lang.Exception" errorPage="Error.jsp" %>
<%
String TACm1="";
String TASid,Nme="",Cm2="",GHN1="",GHN2="",GHN3="",Sid="";
TACm1=((request.getParameter("TAIdCombo"))!=null?(request.getParameter("TAIdCombo")):"");
DBconnection TApool = DBconnection.getInstance();
Connection con2 = TApool.getConnection();
con2.setAutoCommit(false);
Statement TAst1 = con2.createStatement();
ResultSet TArs1 = null,rs2=null,rs1=null;
Cm2=((request.getParameter("TMIdCombo2"))!=null?(request.getParameter("TMIdCombo2")):"");
java.util.Date d1;
java.sql.Date d2;
SimpleDateFormat format=new SimpleDateFormat("dd/MM/yyyy");
%>
<html>
<head>
<TITLE>Guest Houses of DOS</TITLE>
</head>
<%@ include file="StdValidations.js" %>
<%@ page buffer="1094kb"%>
<script Language="JavaScript">
</script>
<body onload="window.history.forward(1);">
<FORM ENCTYPE='multipart/form-data' name="GHInfoDetails1"
method='POST' action="GHInfoDetails1.jsp">'<br><br><br>
<center>
<%
String LoginName= "";
String Name= "";
String SectionName= "";
try
{
LoginName = session.getAttribute("LoginName").toString();
Name = session.getAttribute("Name").toString();
SectionName = session.getAttribute("SectionName").toString();
}
catch(NullPointerException npe)
{
%>
<jsp:forward page="LoginHere.jsp" >
<jsp:param name="SessionMode" value="Session Expired try again to log on" />
</jsp:forward>
<%
}
%>
<%
GHN1 = request.getParameter("GHN1");
if (GHN1 == null) GHN1 = "";
GHN2 = request.getParameter("GHN2");
if (GHN2 == null) GHN2 = "";
GHN3 = request.getParameter("GHN3");
if (GHN3 == null) GHN3 = "";
%>
<table width="100%" bgcolor="#F7EDF7">
<tr >
<td align="center">
<img src="Images/Title2.gif">
</td>
</tr>
</table>
<table width="100%" bgcolor="#F7EDF7">
<tr>
<td >
<b><font face="Times New Roman, Times, serif" color="#402180" size="+1">Logged in as: <%= Name %></font></b>
</td>
<td >
<b><font face="Comic Sans MS" color="#402180" size="+1">Other Details of Guest House</font></b>
</td>
<td align="right">
<b>
<font face="Times New Roman, Times, serif" color="#402180" size="+1">Logout</font></b>
</td>
</tr>
</table>
<table width="100%" bgcolor="#B6C7E5" border="1">
<tr>
<td valign="top">
</br>
<p><img src="Images/bullet.gif">
<b><font face="Times New Roman, Times, serif" color="#402180" size="+0">Defining Guest House Information</font></b></p>
<p><img src="Images/bullet.gif">
<b><font face="Times New Roman, Times, serif" color="#402180" size="+0">Cancelling Guest House Information</font></b></p>
</td>
<td align="left"></br>
<table width="100%" bgcolor="#B6C7E5">
<tr>
<td width="60%" align="right"></br>
<P >Category : <select onChange="TAMAssignCombo2();" name="TMIdCombo2" style="WIDTH: 120px" >
<option value="-">Select</option>
<% try
{
PreparedStatement ps6=con2.prepareStatement("Select CategoryName from TableCategory order by CategoryName");
rs2 = ps6.executeQuery();
while(rs2.next())
{
Sid=rs2.getString(1);
%>
<option value = "<% out.write(Sid); %>"><% out.write(Sid); %></option>
<%
}
}
catch(Exception e)
{
e.printStackTrace();
%>
<Script>alert("Exception Occured : <%= e %>");</Script>
<%
}
%>
</select>
</P>
</td>
</tr>
<tr align="center">
<td>
Title : <INPUT id=text1 maxlength="30" name=GHN1 onKeyPress="if (((event.keyCode > 32) && (event.keyCode < 48)) || ((event.keyCode > 57) && (event.keyCode < 65)) || ((event.keyCode > 90) && (event.keyCode < 97))) event.returnValue=false;">
</td>
</tr>
<tr align="center">
<td>
Reference No : <INPUT id=text1 maxlength="100" name=GHN2 onKeyPress="if (((event.keyCode > 32) && (event.keyCode < 48)) || ((event.keyCode > 57) && (event.keyCode < 65)) || ((event.keyCode > 90) && (event.keyCode < 97))) event.returnValue=false;">
</td>
</tr>
<tr align="center">
<td>
Date : <INPUT id=text1 maxlength="30" name=GHN3 >
</td>
</tr>
<tr align="center">
<td >
</br>
File Name: <INPUT TYPE='file' NAME='file1'></p>
</td></tr>
<tr>
<td align="center">
<INPUT TYPE='submit' VALUE='Submit' ></br>
<b><font face="Times New Roman, Times, serif" color="#402180" size="3">(Information like distance,direction from central places such as Railway station, Airport, Bus Terminal....etc can be added in a file of windows format(such as word, pdf...) and can be uploaded through this screen. This will be displayed for the ISRO Guests.</font></b>)
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="100%" bgcolor="#B6C7E5">
<tr>
<td align="center">
</br>
<img src="Images/Home.jpg">
</td>
</tr>
</table>
<% if (ServletFileUpload.isMultipartContent(request))
{
GHN1 = request.getParameter("GHN1");
if (GHN1 == null) GHN1 = "";
GHN2 = request.getParameter("GHN2");
if (GHN2 == null) GHN2 = "";
GHN3 = request.getParameter("GHN3");
if (GHN3 == null) GHN3 = "";
DiskFileUpload fileUpload = new DiskFileUpload();
List list = null;
try
{
list = fileUpload.parseRequest(request);
}
catch(FileUploadException ex)
{
throw new ServletException("Wrapped",ex);
}
Iterator iter = list.iterator();
while (iter.hasNext())
{
FileItem item = (FileItem) iter.next();
if (!item.isFormField())
{
File itemFile = new File(item.getName());
Nme=itemFile.getName();
%>
<%
File destDir = new File(getServletContext().getRealPath("/")
+"stored");
if(!destDir.exists())
{
destDir.mkdirs();
}
File destFile = new File(getServletContext().getRealPath("/")
+"stored"+File.separator+itemFile.getName());
try
{
item.write(destFile);
}
catch(Exception ex)
{
//throw new ServletException("Wrapped",ex);
}
}
}
TACm1=SectionName;
%><script>alert("<%=GHN1%>");</script><%
try
{
PreparedStatement ps26= con2.prepareStatement("select CategoryNo from TableCategory where CategoryName=?");
ps26.setString(1,Cm2);
rs1=ps26.executeQuery();
while (rs1.next())
{
PreparedStatement ps16=con2.prepareStatement("inset into TableMetaData (CategoryNo,Title,RefernceNo,Date,FileName) values(?,?,?,?,?)");
ps16.setString(1,rs1.getString(1));
ps16.setString(2,GHN1);
ps16.setString(3,GHN2);
d1=format.parse(GHN3);
d2=new java.sql.Date(d1.getTime());
ps16.setDate(4,d2);
ps16.setString(5,Nme);
int NoOfRows=ps16.executeUpdate();
con2.commit();
}
}
catch(Exception e)
{
e.printStackTrace();
%>
<script> alert("Error : <%= e %>"); </script>
<%
}
}
%>
<%
if (TAst1 != null) TAst1.close();
if (con2 != null)
{
con2.rollback();
con2.setAutoCommit(true);
TApool.returnConnection(con2);
}
%>
<input name="HomePage" type="hidden">
</form>
</body>
</html>