I am using the RequestDispatcher to pass control from a controling servlet to JSP pages when user output is required. In all cases when a JSP page containing a form is submitted all values are passed in to the servlet and all works well. I don't seem to be able to do the same in reverse. My servlet does a mySQL lookup based on a submitted value but I don't seem to be able to pass these vars to a jsp page to display them.
I have tried using request.setAttribute() but this seems not to work. Any ideas ? here's an extract
else if (state.equals("new"){
String target = "/displayinfo.jsp";
//existing session new
//Open Connection
try {
Class.forName("org.gjt.mm.mysql.Driver"
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: "
System.err.println(e.getMessage());
}
try {
Connection cons = DriverManager.getConnection
("jdbc:mysql:///moreinfo", "username", "password"
Statement stmts = cons.createStatement();
String queryStrs = "Select * from details where code = '" + code + "'";
ResultSet rss = stmts.executeQuery(queryStrs);
while (rss.next()){
replace rep = new replace();
String oldDetails = rss.getString("details"
String details = rep.replace(oldDetails,"\n","<BR>"
String serial = rss.getString("serial"
String title = rss.getString("title"
String summary = rss.getString("summary"
}
} catch (java.sql.SQLException e){
System.err.print("SQLException: "
System.err.println(e.getMessage());
}
request.setAttribute ("serial", serial);
request.setAttribute ("title", title);
request.setAttribute ("summary", summary);
request.setAttribute ("details", details);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(target);
dispatcher.forward(request, response );
}
I have tried using request.setAttribute() but this seems not to work. Any ideas ? here's an extract
else if (state.equals("new"){
String target = "/displayinfo.jsp";
//existing session new
//Open Connection
try {
Class.forName("org.gjt.mm.mysql.Driver"
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: "
System.err.println(e.getMessage());
}
try {
Connection cons = DriverManager.getConnection
("jdbc:mysql:///moreinfo", "username", "password"
Statement stmts = cons.createStatement();
String queryStrs = "Select * from details where code = '" + code + "'";
ResultSet rss = stmts.executeQuery(queryStrs);
while (rss.next()){
replace rep = new replace();
String oldDetails = rss.getString("details"
String details = rep.replace(oldDetails,"\n","<BR>"
String serial = rss.getString("serial"
String title = rss.getString("title"
String summary = rss.getString("summary"
}
} catch (java.sql.SQLException e){
System.err.print("SQLException: "
System.err.println(e.getMessage());
}
request.setAttribute ("serial", serial);
request.setAttribute ("title", title);
request.setAttribute ("summary", summary);
request.setAttribute ("details", details);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(target);
dispatcher.forward(request, response );
}