<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.apache.commons.io.output.*" %>
<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ page import="javax.mail.internet.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
<%
String name=request.getParameter("name");
String address=request.getParameter("addr");
String telephone=request.getParameter("cont");
String email=request.getParameter("email");
File file ;
String fileName=null;
int maxFileSize = 5000 * 1024;
int maxMemSize = 5000 * 1024;
ServletContext context = pageContext.getServletContext();
String filePath = context.getInitParameter("file-upload");
// Verify the content type
String contentType = request.getContentType();
if ((contentType.indexOf("multipart/form-data") >= 0))
{
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(maxMemSize);
factory.setRepository(new File("C:\temp1"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax( maxFileSize );
try{ //OUTER TRY()
List fileItems = upload.parseRequest(request);
Iterator i = fileItems.iterator();
while ( i.hasNext () )
{
FileItem fi = (FileItem)i.next();
if ( !fi.isFormField () )
{
// Get the uploaded file parameters
String fieldName = fi.getFieldName();
fileName = fi.getName();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
// Write the file
if( fileName.lastIndexOf("\\") >= 0 )
{
file = new File( filePath + fileName.substring( fileName.lastIndexOf("\\"))) ;
}
else{
file = new File( filePath + fileName.substring(fileName.lastIndexOf("\\")+1)) ;
}
fi.write( file ) ;//FILE UPLODING ENDS
//SENDING MAIL ........................
String result;
String to = "sangramkeshari01@gmail.com";
String from = email;
String host = "localhost";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session mailSession = Session.getDefaultInstance(properties);
try{//INNER TRY BLOCK
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("name");
messageBodyPart.setText("address");
messageBodyPart.setText("telephone");
messageBodyPart.setText("email");
Multipart multipart = new MimeMultipart();
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileName);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
// Send message
Transport.send(message);
String title = "Send Email";
result = "Sent message successfully....";
}catch (MessagingException mex)//INNER CATCH()
{
mex.printStackTrace();
result = "Error: unable to send message....";
}//INNER TRY,CATCH() ENDS
//mail sending ends..............
}
}
}catch(Exception ex) //OUTER CATCH()
{
System.out.println(ex);
}
%>
<jsp:forward page="sucess.jsp"/>
<%
}
else{
%>
<jsp:forward page="careers.jsp"/>
<%
}
%>
</head>
</html>
It shows java null pointer exception
Anyone please help me
Its very urgent
Thanks in Advance