• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing parameters

 
srini kami
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am having problem passing paramers between servlets. I have 2 servlets called
oneservlet and twoservlet.
oneservlet shows following html links.
test
check
request
Number of links can change dynamically. It can have 10 links or 100 as they are created dynamically by user actions.
Once user clicks on any of the links twoservlet should show data regarding that link.
oneservlet should pass the value of the link to twoservlet. two servlet uses that parameter value in its SQL select statement.
select * from database where title='&variable passed from oneservlet';
One of the problems is i don't know the parameter value of the link before as they are created by user whenever he wants. He can create link and check the data for that link in same session.
If someone can help me passing parameter value from oneservlet to twoservlet i will appreciate very much.
 
Kok Choon Ling
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
once you created your oneservlet, just append the needed data to the link.
e.g. in your oneserver
out.println "<a href=\"http://{host}/servlet/twoservlet?userData1={user-data}&userData2={user-data2}\">";
hth,
Kok Choon.
 
srini kami
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kok Choon
Thank you for reply. I tried your reply and it worked fine. Its been great help.But i still can't figure out, how to catch dynamic parameter. In your reply you said
out.println "<a href=\"http://{host}/servlet/twoservlet?userData1={user-data}&userData2={user-data2}\">";
The values of {user-data}and {user-data2}should be the values of links user clicks. How do i get that. Following is the code

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("<p>");
//out.println("<a href=\"/test/servlet/DetlReq\">\n" + tit + "</a>\n");

out.println("<a href=\"http://localhost:8080/test/servlet/DetlReq?Post_titl=test\">" + tit + "</a>\n");

out.println("<p>");

}
stmt.close();
}
catch(SQLException es) {
es.printStackTrace();;
}
catch(Exception e) {
e.printStackTrace();
}
}
}

In the above code where
out.println("<a href=\"http://localhost:8080/test/servlet/DetlReq?Post_titl=test\">" + tit + "</a>\n");
right now i hard codes parameter value i am passing in URL as 'test', but it has be whatever link user clicks on.
I will really apprecaite your help very much.
[ July 06, 2003: Message edited by: srini kami ]
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a StringBuffer to dynamically build the link String.
Use HttpServletRequest.getParameterNames() to get a String[] of parameter names.
Loop through the parameter names, adding &name=value for each one.
Viola!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic