title: how to set a path while saving image with jsp/servlet?
body: <p>signup.jsp: here am not able to set path to save my uploaded image inside a folder????thanks in advance..!!everything is fine and evrything is working properyly but image path is showing wrong. </p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
  &lt;base href="&lt;%=basePath%&gt;"&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;Interactive Demos&lt;/title&gt;
  &lt;link rel="stylesheet" href="css/style.css" type="text/css"/
  
  
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;header&gt;
  &lt;jsp:include page="header1.jsp"/&gt;
  
  
  &lt;/header&gt;
  &lt;!--start mainwrap--&gt;
  &lt;div id="mainwrap"&gt;
  
  &lt;!--start midwrap--&gt;
  &lt;div id="midwrap"&gt;
  
  &lt;table style="width:100%; border:0; border-collapse: collapse; padding:0px;"&gt;
    &lt;!-- &lt;tr&gt;
      &lt;td style="width:74%; vertical-align:top;"&gt;&amp;nbsp;&lt;/td&gt;
      &lt;td style="width:26%; "&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt; --&gt;
  
     &lt;h1 style="padding-top:6px; font-family: Palatino Linotype;  font-size:25px; font-weight:bold; color:#476aba; border-bottom:2px solid #476aba"&gt;Registration&lt;/h1&gt;
  
    &lt;tr&gt;
      &lt;td style="vertical-align:top;"&gt;
      &lt;div class="lf" style="width:500px; margin-left: 240px;"&gt;
      &lt;!-- &lt;div style="font-size: 25px"&gt; --&gt;
  
     &lt;!--  &lt;/div&gt; --&gt;
      &lt;div class="tp-btm"&gt;
  
      &lt;div style="border-radius:7px; border:1px solid black; background-color: #e7ebf5; padding-top:15px; padding-bottom:15px;"&gt;
  
      &lt;form name = "frmRegistration"  id="frmRegistration"  onsubmit="submitAlbum()" method="post"  enctype="multipart/form-data"&gt;
      &lt;table style="width:100%; text-align:center; border:0; border-collapse: collapse; padding:0px; text-align:center;"&gt;
  
  
  
  
    &lt;tr&gt;
      &lt;td class="text"&gt;&amp;nbsp;&lt;/td&gt;
      &lt;td style="height:40; text-align:left;" class="text"&gt;E-mail :&lt;span class="red"&gt;*&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;td&gt;&lt;input type="email" name="email" id="email" size="25" required="required" onmouseout=""  placeholder="Eg: admin@bdisys.com"  pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" /&gt;&lt;/td&gt;
    &lt;/tr&gt;
  
  
  
  
  
    &lt;tr&gt;
      &lt;td class="text"&gt;&amp;nbsp;&lt;/td&gt;
      &lt;td style="height: 40" class="text"&gt;&lt;/td&gt;
      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;td style="text-align: center; padding-right: 25px;"&gt;&lt;input type="checkbox" name="imgstatus" id="imgstatus" onclick="showFileUpload();"&gt;Click Here To Set Default Image &lt;/td&gt;
    &lt;/tr&gt;
  
  
    &lt;tr&gt;
      &lt;td class="text"&gt;&amp;nbsp;&lt;/td&gt;
      &lt;td style="height: 40" class="text"&gt;Upload Image :&lt;span class="red"&gt;*&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&amp;nbsp;&lt;input type="hidden" value="images/blogs.png" name="defaultimg" id="defaultimg"&gt; &lt;/td&gt;
      &lt;td style="text-align: center; padding-left: 50px;"&gt;&lt;input type="file" name="datafile" id="datafile" size="42" required="required"
                              onchange="checkFilesExtention(this);" accept='image/png,image/jpg,image/gif,image/jpeg,image/bmp,image/x-eps'&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  
    &lt;tr&gt;
      &lt;td class="text"&gt;&amp;nbsp;&lt;/td&gt;
      &lt;td style="height:32;" class="text"&gt;&amp;nbsp;&lt;/td&gt;
      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;td style="text-align:left; padding-left:50px; padding-top:20px;"&gt;&lt;input type="image"  class="ab" src="images/submiit.png"/&gt;&lt;/td&gt;
    &lt;/tr&gt;
   &lt;/table&gt;
  &lt;/form&gt;
  
  &lt;/div&gt;
      &lt;/div&gt;
      &lt;/div&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style="height:5;" colspan="2"&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/div&gt;
  &lt;!--end midwrap--&gt;
  &lt;/div&gt;
  &lt;!--end mainwrap--&gt;
  
  &lt;footer&gt;
  &lt;jsp:include page="footer1.jsp"/&gt;
  &lt;/footer&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Servlet.java : i want to know how to set this path ???thanks in advance for any help!!</p>
  
  <pre><code>public class signUpServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
      private String webTempPath;
      private String filename;
  
  //  String server_url="http://localhost:8080/BDI Systems/";
      private static Properties properties;
  
      private static String filePath = MailSenderDAO.class.getResource("").getPath().replaceAll("%20", " ");
  static {
  try {
      properties = new Properties();
      properties.load(new FileInputStream(filePath + "/mail.config"));
  } catch (Exception ex) {
  //  ex.printStackTrace();
  }
  }
  public void init(){
       webTempPath = getServletContext().getRealPath("/")+"images/";
      }
  
      public signUpServlet() {
          super();
          // TODO Auto-generated constructor stub
      }
  
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doPost(request,response);
      }
  
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          HttpSession session =request.getSession();
          UserDao userDao=new UserDaoImpl();
          System.out.println("Inside SignUp Servlet===============");
          String formIdentity=request.getParameter("formidentity");//activateaccount
          System.out.println("FormIdentity================"+formIdentity);
  
          if(formIdentity.equalsIgnoreCase("doRegistration"))
          {
  //          System.out.println("Inside doRegistration===================="+properties.getProperty("server_url"));
  
              String checkboxStatus=request.getParameter("checkboxStatus");
               System.out.println("status...."+checkboxStatus);
               MultipartRequest mpr = null;
               Throwable e;
              if(checkboxStatus.equalsIgnoreCase("No"))
              {
                       mpr=new MultipartRequest(request,webTempPath,5*1024*1024,new MnthFileRenamePolicy());
                      response.setContentType("text/html");
                      System.out.println("inside add details..");
                   try{
  
  
                            System.out.println("in datafile con of  servlet...");
  
                            this.filename=mpr.getFilesystemName("datafile");
  
  
                            File srcFile=new File(webTempPath+this.filename); 
                            System.out.println("  web temp=="+webTempPath);
                            System.out.println("image path==="+webTempPath+this.filename);
  
                            mpr.copyFile(srcFile, new File(webTempPath+this.filename));
                            String imgpath="images/"+this.filename;
                            System.out.println("image is=="+imgpath);
                            System.out.println("After file of  servlet...");
  
                            User user=new User();
                            user.setFirstName(mpr.getParameter("fname"));
                            user.setLastName(mpr.getParameter("lname"));
                            user.setEmail(mpr.getParameter("email"));
                            user.setDesc(mpr.getParameter("desc"));
                            user.setProfession(mpr.getParameter("profession"));
                            user.setDateofbirth(mpr.getParameter("dob"));
                            user.setLocation(mpr.getParameter("location"));
                            user.setProjects(mpr.getParameter("myprojects"));
                            user.setHobbies(mpr.getParameter("myhobbies")); 
                            user.setImagePath(imgpath);
  
                            user.setPassword(SecurityManager.asyEncrypt(mpr.getParameter("password")));
                            user.setUserStatus(Status.UNBLOCKED);
                            user.setUserType(UserType.NORMAL);
                            user.setCreatedOn(new Date());
  
  
  
                             User createduser=userDao.createUser(user);
                             System.out.println("hi this is temp..."+temp);
                             if(createduser==null){
                                System.out.println("hi this is ...");
                                response.sendRedirect("Registration?status=exist");
                         }
  
  
                      }
  
  
                      catch(Exception e1){
                                e1.printStackTrace();
  
                      }
                      finally
                      {
                          userDao.closeSession();
                      }
                  }
  
                   else 
                   if (checkboxStatus.equalsIgnoreCase("Yes"))
                   {
  
                       System.out.println("in datafile con of  servlet...");
  
                      try{
  
                                User user=new User();
                                user.setFirstName(request.getParameter("fname"));
                                user.setLastName(request.getParameter("lname"));
                                user.setEmail(request.getParameter("email"));
                                user.setDesc(request.getParameter("desc"));
                                user.setProfession(request.getParameter("profession"));
                                user.setDateofbirth(request.getParameter("dob"));
                                user.setLocation(request.getParameter("location"));
                                user.setProjects(request.getParameter("myprojects"));
                                user.setHobbies(request.getParameter("myhobbies"));
                                user.setImagePath(request.getParameter("defaultimg"));
                                user.setPassword(SecurityManager.asyEncrypt(request.getParameter("password")));
                                user.setUserStatus(Status.UNBLOCKED);
                                user.setUserType(UserType.NORMAL);
                                user.setCreatedOn(new Date());
                                System.out.println("in try con of  servlet...");
  
  
  
  
                                User createduser=userDao.createUser(user);
                                if(createduser==null){
                                   response.sendRedirect("Registration?status=exist");
                                }
  
  
  
                          }   
                      catch(Exception e1){
                          e1.printStackTrace();
  
                      }finally{
                          userDao.closeSession();
                      }
                   }
  
          }
  
  }
  </code></pre>