0

hi,
im doing a project on jsp..which is a file uploading program
i have a file chooser and i want the file choosed by the user to be stored on my server. how should i do that..
please help me
my code is given below--addphoto.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title>Candidate Management</title>


    </head>
    <body bgcolor="#99ccff">
        <form action="Photo" method="post">
            <center>
                <h1>Photo management</h1>
                <p>&nbsp;</p></center>

        
            <table width="425" height="359" border="0" cellpadding="5">
                
                
                <tr>
                    <td align="right">Add photo</td>
                    <td colspan="2"><label>
                            <input type="file" name="text_photo" id="text_photo/>
                        </label></td>
                </tr>
                
                <tr>
                    <td>&nbsp;</td>
                    <td width="107" align="center"><label>
                            <input type="submit" name="submit" id="submit" value="Submit" />
                        </label></td>
                    <td width="159" align="center"><label>
                            <input type="reset" name="reset" id="reset" value="Reset" />
                        </label></td>
                </tr>
            </table>
            <p>&nbsp;</p>
        </form>
    </body>
</html>

my servlet code is also given. Photo.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */




import java.io.*;
import java.sql.*;


import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author New User
 */
public class NewServlet extends HttpServlet
{
    String user,pass,values1;
    boolean success=false;
int values=0;
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void connect(String uname,String upath )
    {
        int i=0,m=0;
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection("jdbc:mysql://localhost/photo", "root", "123456");
            if (!con.isClosed()) {

                java.sql.Statement cs;
                     ResultSet rs=null;
                     cs= con.createStatement();
                String SQL = "select max(slno) from addphoto";
                cs = con.createStatement();
                rs = cs.executeQuery(SQL);
rs.next();

int result = Integer.parseInt(rs.getString("max(slno)"));
values=result;

                values++;
                   String val = Integer.toString(values);
String ins="INSERT INTO addphoto(slno,path) VALUES("+val+", '"+ upath +"')";
  success=true;
int roweffected=cs.executeUpdate(ins);
                 
       
             }
        } catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
            }
        }
    }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        String upath=request.getParameter("text_photo");
        File f=new File(upath);
       String path=f.getAbsolutePath();
        connect(uname,path);
        if(success)
        {//System.out.println("success");
            response.sendRedirect("Success.html");
        }
 else
        {//System.out.println("failure");
            response.sendRedirect("LoginError.html");
 }
        /*
        if(uname.equalsIgnoreCase("Arun")&&pword.equals("admin"))
        {
            response.sendRedirect("LoginSuccess.html");
        }
        else
        {
            response.sendRedirect("LoginError.html");
        }*/
        out.close();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by peter_budo
0

I solved it all by myself..THank you for not helping..if you guys had helped me..i wouldn't have tried... Thank you...Atleast i'm feeling proud of myself... of being able to solve a problem myself...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.