I written a program to retrieve data from database in servlets.now I want pass this value to jsp as a hyperlink. How to do this?
This is my servlet code what I wrote:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ViewServlet extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
Connection con;
Statement stmt;
ResultSet rs;
String acid = null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:DSN","scott","krishna");
stmt = con.createStatement();
rs = stmt.executeQuery("Select id from custm where name='jai'");
req.setAttribute(acid,rs);
req.getRequestDispatcher("my_jsp_page.jsp").forward(req, res);
}
catch (Exception e){
pw.println(e);
}
}
}
Now I edited the code now its saying cannot call setAttribute with null value.
For extracting value from resultset:
You can store values in Request scope using setAttribute() method….
In jsp you can retrieve it using expression language…
In servlet
In jsp:
There is also other way…
You can also use scriplets for displaying purpose in jsp..
for example….
for calling jsp from servlet use :