Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Strings from JSP to Servlet

Status
Not open for further replies.

sharvey99

Programmer
Aug 24, 2000
16
GB
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,&quot;\n&quot;,&quot;<BR>&quot;);

String serial = rss.getString(&quot;serial&quot;);
String title = rss.getString(&quot;title&quot;);
String summary = rss.getString(&quot;summary&quot;);

}

} catch (java.sql.SQLException e){
System.err.print(&quot;SQLException: &quot;);
System.err.println(e.getMessage());
}

request.setAttribute (&quot;serial&quot;, serial);
request.setAttribute (&quot;title&quot;, title);
request.setAttribute (&quot;summary&quot;, summary);
request.setAttribute (&quot;details&quot;, details);

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(target);
dispatcher.forward(request, response );

}
 
Correct me if I have misunderstanding.You forward your servlet into JSP by RequestDispatcher class.This JSP has a form.When this form is submitted it leads to a servlet or same servlet.But when you type request.getParameter(<the form element name>) in your servlet.It returns null.Is this the case? Salih Sipahi
Software Engineer.
City of Istanbul Turkey
openyourmind77@yahoo.com
 
You seem to have done averything right - so are you sure the if(state.equals(&quot;new&quot;)) evaluates to true????

I don't know anything about your servlet but if it dosn't and you redirect to &quot;/displayinfo.jsp&quot; later in the code anyway you wouldn't have retrieved the data - try writing &quot;true&quot; instead and se if it work ... and if it does find out why state.equals(&quot;new&quot;) doesn't ... mayby equalsIgnoreCase will?

Nebse
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top