problem with ENCTYPE='multipart/form-data'

[nobr]If I add ENCTYPE='multipart/form-data' in the form the value Cm1 is becoming null. What cud be the reason.

<%@ 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="";

TACm1=((request.getParameter("TAIdCombo"))!=null?(request.getParameter("TAIdCombo")):"");

DBconnection TApool = DBconnection.getInstance();

Connection TAcon = TApool.getConnection();

TAcon.setAutoCommit(false);

Statement TAst1 = TAcon.createStatement();

ResultSet TArs1 =null;

%>

<html>

<head>

<TITLE>Guest Houses of DOS</TITLE>

</head>

<%@ include file="StdValidations.js" %>

<%@ page buffer="1094kb"%>

<script Language="JavaScript">

function TAAssignCombo1()

{

if (document.TypeAdd.TAIdCombo.selectedIndex != 0)

{

}

}

</script>

<body>

<FORM ENCTYPE='multipart/form-data' name="Test"

method='POST' action='Test.jsp'><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>

<%

}

%>

<table width="80%" bgcolor="#F6F8C4">

<tr >

<td align="center">

<img src="Images/Title.gif">

<img src="Images/gh1.jpg" ></br>

</td>

</tr>

</tr>

<tr>

<td>

</td>

<td align="right">

<a href="LogOut.jsp"><font face="Times New Roman, Times, serif" size="+1">Logout</font></a>

</td>

</tr>

</table>

<table width="80%" bgcolor="#B6C7E5">

<tr >

<td align="center">

<font face="Comic Sans MS" color="6D3C1E" size="+1">Accomodation Details</font>

</td>

</tr>

</table>

<table width="80%" bgcolor="#B6C7E5">

<tr >

<td >

<font face="Times New Roman, Times, serif" size="+1">Name: <%= Name %></font>

</td>

<td align="right">

<font face="Times New Roman, Times, serif" size="+1">SectionName: <%= SectionName %></font>

</td>

</tr>

</table>

<table width="80%" bgcolor="#B6C7E5">

<tr >

<td align="left">

<table width="80%" bgcolor="#B6C7E5">

<tr>

<td width="60%" align="center">

<P >Guest House Name :

<select onChange="TAAssignCombo1();" name="TAIdCombo" style="WIDTH: 90px">

<optionvalue="-">Select</option>

<%try

{

TArs1 = TAst1.executeQuery("select GHName from TableGuestHouse");

while(TArs1.next())

{

TASid = TArs1.getString("GHName");

%>

<option value ="<% out.write(TASid); %>" ><% out.write(TASid); %></option>

<%

}

}

catch(Exception e)

{

e.printStackTrace();

%>

<Script>alert("Exception Occured : <%= e %>");</Script>

<%

}

%>

</select></br>

</td>

</tr >

<tr align="center">

<td >

</br>

File Name: <INPUT TYPE='file' NAME='file1'>

</td></tr>

<tr>

<td align="center">

<INPUT TYPE='submit' VALUE='Save' >

</td>

</tr>

</table>

</td>

</tr>

</table>

<table width="80%" bgcolor="#B6C7E5">

<tr>

<td align="center">

</br>

<a href="AdminHome.jsp"><img src="Images/Home.jpg"></a>

</td>

</tr>

</table>

<%if (ServletFileUpload.isMultipartContent(request))

{

%><script>alert("<%=TACm1%>");</script><%

DiskFileUpload fileUpload =new DiskFileUpload();

List list =null;

try

{

list = fileUpload.parseRequest(request);

}

catch(FileUploadException ex)

{

thrownew 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();

%>

<script>alert("<%=itemFile.getName()%>");</script>

<%

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);

}

}

}

try

{

int noofrows=TAst1.executeUpdate("update TableGuestHouse set GeneralInfo='" + Nme +"' where GHName='" + TACm1 +"'");

TAcon.commit();

}

catch(Exception e)

{

e.printStackTrace();

%>

<script> alert("Error : <%= e %>"); </script>

<%

}

}

%>

<%

if (TAst1 !=null) TAst1.close();

if (TAcon !=null)

{

TAcon.rollback();

TAcon.setAutoCommit(true);

TApool.returnConnection(TAcon);

}

%>

<script>

<%if (TACm1!=null)

{%>

document.Test.TAIdCombo.value="<%=TACm1%>";

<%}%>

</script>

<input name="HomePage" type="hidden">

</form>

</body>

</html>

[/nobr]

[11307 byte] By [harinibiligiria] at [2007-11-26 19:01:39]
# 1

You cannot use the HttpServletRequest to fetch multipart fields, you need to do it with the FileUpload package. So it would be:

if(multipartcontent){

// use FileUpload

} else {

// use HttpServletRequest

}

gimbal2a at 2007-7-9 20:46:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Sir, I've not understood properly. WIll u pl explain a little bit in detail. I'm new to JSP.
harinibiligiria at 2007-7-9 20:46:33 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...