Reputation: 646
This is my jsp form
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<jsp:include page="common_header.jsp" />
<center>
<form action="projects" method="post" enctype="multipart/form-data" name="productForm" id="productForm">
<div class="row grid_12" id="add_service">
<h2>Add Project</h2>
<div class="grid_6">
<label>Project Name</label>
<div class="clear"></div>
<input type="text" name="name" id="name" class="text_box">
</div>
<div class="clear"></div>
<div class="grid_6">
<label>Project Short Description</label>
<div class="clear"></div>
<textarea name="short_description" id="short_description" class="text_area"></textarea>
</div>
<div class="grid_6 last">
<label>Project Long Description</label>
<div class="clear"></div>
<textarea name="long_description" id="long_description" class="text_area"></textarea>
</div>
<div class="clear"></div>
<div class="grid_6">
<label>Image</label>
<div class="clear"></div>
<input type="file" id="file" name="file"/>
</div>
<div class="grid_6 last">
<label>Link</label>
<div class="clear"></div>
<input type="text" name="link" id="link" class="text_box">
</div>
<div class="clear"></div>
<div class="grid_6">
<label>Status</label>
<div class="clear"></div>
<select id="status" name="status">
<option value="1">In-Progress</option>
<option value="2">Completed</option>
<option value="3">Re-Opened</option>
<option value="4">Just Discussed</option>
</select>
</div>
<div class="grid_6 last">
<label>Client</label>
<div class="clear"></div>
<input type="text" name="client" id="client" class="text_box">
</div>
<div class="clear"></div>
<input type="submit" name="Submit" value="Submit">
</div>
</form>
</center>
<jsp:include page="/common_footer.jsp" />
i am submitting the from to projects
named servlet.
the code is as follows:
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import admin.project_info;
public class projects extends HttpServlet
{
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,java.io.IOException
{
String saveFile = "";
String contentType = req.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(req.getInputStream());
int formDataLength = req.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,
saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1, contentType
.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
FileInputStream fis;
File f = new File(saveFile);
fis = new FileInputStream(f);
String name = req.getParameter("name");
String short_description = req.getParameter("short_description");
String long_description = req.getParameter("long_description");
String link = req.getParameter("link");
String status = req.getParameter("status");
int astatus = Integer.parseInt(status);
String client = req.getParameter("client");
project_info p1 = new project_info(name, short_description, long_description, link, astatus, (InputStream) fis, (int) (f.length()), client);
p1.create();
}
}
}
My code for project_info
is as follows
package admin;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class project_info
{
String name, short_description, long_description, link, client;
int id, status, file_length;
InputStream image;
public project_info()
{
name = null;
short_description = null;
long_description = null;
link = null;
id = 0;
status = 0;
image = null;
file_length = 0;
}
public project_info(String aname, String sdescription, String ldescription, String alink, int astatus, InputStream apath,int afile_length, String aclient)
{
name = aname;
short_description = sdescription;
long_description = ldescription;
link = alink;
status = astatus;
image = apath;
file_length = afile_length;
client = aclient;
}
//function for create entry in database
public int create()
{
int flag = 0;
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/site?user=root&password=root");
String query = "INSERT INTO projects(name, short_description, long_description, status, link, image, client) values(?,?,?,?,?,?,?)";
PreparedStatement statement = con.prepareStatement(query);
statement.setString(1, name);
statement.setString(2, short_description);
statement.setString(3, long_description);
statement.setInt(4, status);
statement.setString(5, link);
statement.setBinaryStream(6,image, file_length);
statement.setString(7, client);
statement.executeUpdate();
statement.close();
con.close();
flag = 1;
}
catch (Exception e)
{
e.printStackTrace();
flag = 0;
}
return flag;
}
}
last 3 days i am getting same error..!
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
projects.doPost(projects.java:21)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.
Apache Tomcat/6.0.20
I am not getting where i am mistaking...!
Full StackTrace is as follows
Dec 30, 2011 11:13:37 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:Admin Website' did not find a matching property.
Dec 30, 2011 11:13:37 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:Temp' did not find a matching property.
Dec 30, 2011 11:13:37 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre6\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre6/bin/client;C:/Program Files/Java/jre6/bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Java\jdk1.6.0_10\bin;
Dec 30, 2011 11:13:37 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Dec 30, 2011 11:13:37 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 365 ms
Dec 30, 2011 11:13:37 PM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Dec 30, 2011 11:13:37 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.20
Dec 30, 2011 11:13:38 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Dec 30, 2011 11:13:38 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Dec 30, 2011 11:13:38 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/15 config=null
Dec 30, 2011 11:13:38 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 296 ms
null
null
Dec 30, 2011 11:13:58 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet projects threw exception
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at projects.doPost(projects.java:73)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
Upvotes: 0
Views: 12901
Reputation: 646
enctype="multipart/form-data"
was the problem..!
in short, we can not use request.getParameter() while we are using multipart/form-data
:)
Upvotes: 1
Reputation: 502
i think the problem is in this line,
int formDataLength = req.getContentLength();
or
int astatus = Integer.parseInt(status);
here may be req.getContentLength() is null or stats is null, when you try to assign null value to integer, that will raise numerformat exception.
instead of that, check whether the req.getContentLength() is null or not before assign to any variable.
if(req.getContentLength() != null){
int formDataLength = req.getContentLength();
}
if(status != null){
int astatus = Integer.parseInt(status);
}
Upvotes: 0
Reputation: 2400
You should completely re-write this section of your code:
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,
saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1, contentType
.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
There are a number of inputs that will cause this to fail. Consider using concise Regex, StringUtils, and NumberUtils to make your job easier. This parsing code smells a bit.
Good luck.
Upvotes: 1
Reputation: 66647
NumberFormatException occurs when you create an integer from non-integer value. Following line in your servlet causing the issue. It seems 'status' value is empty. Make sure you are getting proper integer value into 'status' string.
int astatus = Integer.parseInt(status);
Upvotes: 0