I'm using below code to upload the file to server. Is it proper? The path "d:/new" in the code, is it server path or client PC path. Do i have to specify client pc path anywhere. Or it will directly take the path while clicking browse button and select the file? Is there any better approach other than this to upload file to server?
index.html
<html>
<body>
<form class="form-group" action="go" method="post" enctype="multipart/form-data">
<%
Connection connection = null;
Statement statement = null;
String error = "";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/lms_data", "root", "");
statement = connection.createStatement();
ResultSet resultset = statement.executeQuery("select * from assessment_type");
connection.setAutoCommit(false);
%>
<label>Assessment Type</label>
<select class="form-control" name="assessment_type_id">
<% while (resultset.next()) {%>
<option value="<%= resultset.getString(1)%>"><%= resultset.getString(2)%></option>
<% } %>
</select>
<%
ResultSet resultset_cat_id = statement.executeQuery("select * from course_category");
%>
<br/><label>Category Name</label>
<select class="form-control" name="category_id">
<% while (resultset_cat_id.next()) {%>
<option value="<%= resultset_cat_id.getString(1)%>"><%= resultset_cat_id.getString(2)%></option>
<% } %>
</select>
<%
} catch (Exception e) {
if (!connection.isClosed()) {
connection.close();
}
out.println(e);
e.printStackTrace();
}
%>
<label class="control-label">Select File</label>
<input type="file" name="file" id="file"><br/>
<input type="submit" name="submitform" class="btn btn-primary" value="Submit" style="width:20%;border-radius:0px;"><br/><br/>
</form>
</body>
</html>
UploadServlet.java
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import com.oreilly.servlet.MultipartRequest;
public class UploadServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
MultipartRequest m=new MultipartRequest(request,"d:/new");
out.print("successfully uploaded");
}
}
web.xml
<web-app>
<servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/go</url-pattern>
</servlet-mapping>
</web-app>