Thursday, 10 March 2011 01:56
// description of your code here
hiiii...
package careers;
import java.io.DataInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class addjobs extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection con=null;
private PreparedStatement ps=null;
public void init(ServletConfig config) throws ServletException {
String url = "jdbc:mysql://localhost:3306/rivigroup";
String user="root";
String password="root";
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con=DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String saveFile="";
String contentType=request.getContentType();
if((contentType!=null)&&(contentType.indexOf("multipart/form-data")>=0))
{
DataInputStream in=new DataInputStream(request.getInputStream());
int formDataLength=request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
try{
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;
@SuppressWarnings("unused")
int startPos = ((file.substring(0, pos)).getBytes()).length;
@SuppressWarnings("unused")
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
HttpSession session=request.getSession();
String jobid = (String)session.getAttribute("jobid");
String position=(String) session.getAttribute("job_position");
String description=(String)session.getAttribute("description");
String location=(String) session.getAttribute("job_location");
try{
ps=con.prepareStatement("insert into job_table values(?,?,?,?,?) ");
ps.setString(1,jobid);
ps.setString(2,position);
ps.setString(3,description);
ps.setString(4,location);
ps.setString(5, saveFile);
//ps.setBytes(6, dataBytes);
ps.executeUpdate();
}catch (SQLException e1) {
e1.printStackTrace();
}
request.setAttribute("fileName", saveFile);
request.setAttribute("jobid",jobid );
request.setAttribute("position",position );
request.setAttribute("description",description );
request.setAttribute("location",location );
RequestDispatcher view = request.getRequestDispatcher("add_jobs.jsp");
view.forward(request, response);
}catch (Exception e) {
}
}
}
}
It is the servlet code.
But in my jsp
jobid:
position:
Description: