*
The moose likes Servlets and the fly likes Passing parameter to servlet from another servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing parameter to servlet from another servlet" Watch "Passing parameter to servlet from another servlet" New topic
Author

Passing parameter to servlet from another servlet

srini kami
Greenhorn

Joined: Dec 29, 2002
Posts: 26
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 ]

Thank you<br />Srini
Scott Duncan
Ranch Hand

Joined: Nov 01, 2002
Posts: 363
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.


No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Passing parameter to servlet from another servlet