• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Passing parameter to servlet from another servlet

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am working on creating online forum where users post question with title. Both title and question posted goes to database and title become url link, if someone wants check the question he.she cliks on the title(link) and whole question body is shown. But i am having problem passing title to the DetlReq servlet.
Code for PosReq.java (which gets all the titles existing in database and makes them as clickable links).
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.lang.String;
import java.lang.StringBuffer;

public class PosReq extends HttpServlet
{
private Connection conn;
public void init(ServletConfig conf) throws ServletException
{
super.init(conf);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
conn =DriverManager.getConnection("jdbc dbc:request", "srini", "srini");
}catch (Exception e) {
System.err.println(e);
}

}

public void doGet(HttpServletRequest req, HttpServletResponse res) throws javax.servlet.ServletException, java.io.IOException
{
StringBuffer buffer = new StringBuffer();
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>ALL REQUESTS</TITLE></HEAD>\n" +
"<BODY>\n" + "<h1> Your Previous Requests</h1>\n"
+ "Please Click On Specific Request For detailes.\n"+"\n"+
"</BODY></html>");

try{

Statement stmt = conn.createStatement();
String req_show= "select title from request";

ResultSet rs = stmt.executeQuery(req_show);

while (rs.next() ) {

String tit = rs.getString("title");
//out.println("<a href=\"http://localhost:8080/test/index.html\">\n</a>\n");
out.println("<p>");
out.println("<a href=\"/test/servlet/DetlReq?Post_titl=" + URLEncoder.encode("<the_id>") +">\n" + tit + "</a>\n");
out.println("<p>");

//"/test/servlet/PosReq"
// out.println(tit);
//out.println("\n");

}
stmt.close();
}
catch(SQLException es) {
es.printStackTrace();;
}
catch(Exception e) {
e.printStackTrace();
}
}
}
example output
test
req1
req2
Once user clicks on any of above links title should be passed to (example: test) next servlet
whcih has sql query
select * from request where title="test";
but how to i pass the link value (test) to DetlReq.(which is supposed to show only question body when user clicks on respective link)

import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.lang.String;
import java.lang.StringBuffer;

public class DetlReq extends HttpServlet
{
private Connection conn;
public void init(ServletConfig conf) throws ServletException
{
super.init(conf);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
conn =DriverManager.getConnection("jdbc dbc:request", "srini", "srini");
}catch (Exception e) {
System.err.println(e);
}

}

public void doGet(HttpServletRequest req, HttpServletResponse res) throws javax.servlet.ServletException, java.io.IOException
{
StringBuffer buffer = new StringBuffer();
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>ALL REQUESTS</TITLE></HEAD>\n" +
"<BODY>\n" + "<h1> Your Previous Requests</h1>\n"
+ "Please Click On Specific Request For detailes.\n"+"\n"+
"</BODY></html>");
String add=req.getRequestURI();
String par = (String)req.getAttribute("Post_titl");
out.println(par);
try{

Statement stmt = conn.createStatement();
String req_show= "select * from request where title=Post_titl";
ResultSet rs = stmt.executeQuery(req_show);

while (rs.next() ) {

String tit = rs.getString("title");
String sys = rs.getString("system_id");
String des1 = rs.getString("description");
String com1 = rs.getString("comments");
out.println("<HEAD><TITLE>ALL REQUESTS</TITLE></HEAD>\n" +
"<BODY>\n" + "<TABLE border=\"1\">\n" +
"<TR>\n" + "<td>" + tit + "</td>\n" + "<td>" + sys + "</td>\n" + "<td>" + des1 + "</td>\n" + "<td>" + com1 + "</td>\n"+"</tr>\n"+"</table>"+
"</BODY></html>");
out.println(par);
/*out.println(tit);
out.println("<p>");
out.println("<p>");
out.println(sys);
out.println("<p>");
out.println("<p>");
out.println(des1);
out.println("<p>");
out.println("<p>");
out.println(com1);
out.println("<p>");*/
}
stmt.close();
}
catch(SQLException es) {
es.printStackTrace();;
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Looks like somehow i have pass the value user clicked to DetlReq thru URl. I don't know how to do it. If someone can help me i will appreciate it very much.
Thanx in advance.
[ June 30, 2003: Message edited by: srini kami ]
 
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instead of req.getAttribute("Post_titl");
try req.getParameter("Post_titl");
Since it is embedded in the URL, this value is a parameter value, not an attribute.
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic