Hello friends,
I have a table which contains file names. For example i have stored a file "java.pdf" in a directory called "course". The file is located here. http://www.something.com/course/java.pdf

Now i stored the file name in a column named "document_filename".
I stored it as "java.pdf"

Now if a user click the course it should rediect to view the file. I coded like this. Please correct my code.

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" %> 
<% 
try { Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/elearning_datasource?user=root"); 
int course_id; 
String query,query1; course_id=Integer.parseInt(request.getParameter("course_id")); 
String statement="SELECT document_filename from x_masterlistofdocuments where course_id="+course_id+""; ResultSet objRs=null; response.sendRedirect("http://www.something.com/course/%=statement%"); 
con.close(); 
} 
catch(Exception e) 
{ 
out.println("Error?"+e.getMessage()); 
} 
%>

I understand the error is in this line:

response.sendRedirect("http://www.something.com/course/%=statement%");

I want redirect to the file what is selected using query. Thankyou friends

Don't use JSP's for placing your business logic; use Servlets instead for your redirection purposes. Also, for performance reasons, before going in production make sure you replace the logic of grabbing connection directly from the DriverManager class with getting connection from a connection pool.

Coming to your code, you need to actually "execute" the SQL statement you created to get the file name. Read the JDBC tutorial on how to go about doing this.

A rough structure of your code should be something like:

// Servlet Class
public class ElearningServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
		Course course = createCourseFromRequest(req);
		ElearningDao dao = new ElearningJbdcDao(ConnectionUtils.getDatasource());
		String fileName = dao.getFileNameForCourse(course);
		// sanitize name for making it uri compatible if required
		response.sendRedirect("http://www.something.com/course/" + fileName);
	}
}
// Value object Course
public class Course {

	private String courseId;
	// getters and setters

	// other fields
}
// DAO interface
public interface ElearningDao {
	public String getFileNameForCourse(Course course);
}
// DAO implementation
public class ElearningJdbcDao {
	private Datasource;
	
	public ElearningJdbcDao(Connection connection) {
		this.connection = connection;
	}
	
	public String getFileNameForCourse(Course course) {
		String statement = "SELECT document_filename from x_masterlistofdocuments where course_id=?";
		Statement stmt = null;	// prepare statement using the connection
		// set the placeholder value using the `course` object using `course.getCourseId()`
		ResultSet rs = null;	// execute statement
		String name = null;	// read the name from the resultset
		return name;
	}
}

Of course, the example code above is very much oversimplified. A lot of boilerplate code can be removed by using a good framework like Spring coupled with a view framework like Struts 2.

Don't use JSP's for placing your business logic; use Servlets instead for your redirection purposes. Also, for performance reasons, before going in production make sure you replace the logic of grabbing connection directly from the DriverManager class with getting connection from a connection pool.

Coming to your code, you need to actually "execute" the SQL statement you created to get the file name. Read the JDBC tutorial on how to go about doing this.

A rough structure of your code should be something like:

// Servlet Class
public class ElearningServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
		Course course = createCourseFromRequest(req);
		ElearningDao dao = new ElearningJbdcDao(ConnectionUtils.getDatasource());
		String fileName = dao.getFileNameForCourse(course);
		// sanitize name for making it uri compatible if required
		response.sendRedirect("http://www.something.com/course/" + fileName);
	}
}
// Value object Course
public class Course {

	private String courseId;
	// getters and setters

	// other fields
}
// DAO interface
public interface ElearningDao {
	public String getFileNameForCourse(Course course);
}
// DAO implementation
public class ElearningJdbcDao {
	private Datasource;
	
	public ElearningJdbcDao(Connection connection) {
		this.connection = connection;
	}
	
	public String getFileNameForCourse(Course course) {
		String statement = "SELECT document_filename from x_masterlistofdocuments where course_id=?";
		Statement stmt = null;	// prepare statement using the connection
		// set the placeholder value using the `course` object using `course.getCourseId()`
		ResultSet rs = null;	// execute statement
		String name = null;	// read the name from the resultset
		return name;
	}
}

Of course, the example code above is very much oversimplified. A lot of boilerplate code can be removed by using a good framework like Spring coupled with a view framework like Struts 2.

Dude you are great....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.