Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Receives null value  RSS feed

 
varkala prabhakar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai all,
while i try to upload image file to server i dont get the getParameter values of name1 and image1. Could any one tell me where can be my mistake. Upload of the image works fine.
regards
prabhakar.
My Servlet Prog
~~~~~~~~~~~~~~~
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import com.oreilly.servlet.*;
public class SaveImageFile extends HttpServlet
{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter out=res.getWriter();
res.setContentType("text/html");
System.out.println("Name Received is:"+req.getParamete("name1"));
out.println("<html><body>");

try{
MultipartRequest m1=new MultipartRequest(req,"c:\\image");
String e1=m1.getParameter("image1");
System.out.println(" File Name :\t "+e1);
System.out.println("ContentType:\t"+m1.getContentType("image1"));
out.println("Execution successfull");
}
catch(Exception e){
out.println("<html><body>");
System.out.println(" Error :\t"+e);
out.println("Error during Processing");
out.println("</body></html>");
out.close();
}

out.println("</body></html>");
out.close();
}
}// End of SaveImageFile
My html file.
~~~~~~~~~~~~~
<html><head><title> Sample testing</title></head>
<BODY BGCOLOR="#ccffcc">
<form ENCTYPE="multipart/form-data" action="http://202.141.81.233:8080/servlet/SaveImageFile" method="post">
<table>
<tr>
<td> Enter the name :
<input type="text" name="name1" size=10 ></td>
</tr>
<tr>
<td> Choose the image file
<input type="file" name="image1"></td>
</tr>
<tr>
<td><input type="submit" value="submit"></td>
</tr>
</table>
</form>
</head>
</title>
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not be using the req object until after you have sent it to the MultipartRequest constructor.

So ... do not retrieve "name1" until after you send it to the constructor. Also, you can retrieve it from your m1 object.
 
varkala prabhakar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear mike,
my problem is solved for only one request. I get array index out of bounds for nam array. is the code written for retriving for image name is wrong?.
____________________________________________________________
try{
MultipartRequest m1=new MultipartRequest(req,"c:\\image");

System.out.println(" Content Type:\t"+m1.getContentType("image1"));

String []enu= m1.getParameterValues("name1");
String []nam = m1.getParameterValues("image1");

System.out.println("First eke :"+enu[0]);
System.out.println("File Name :"+nam[0]);
out.println("Execution successfull");
}
_____________________________________________________________

thanx
prabhakar.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!