简体   繁体   中英

Passing parameters through URLs

I've written a servlet which queries a database for a list of messages, the output is directed to servlet generated html. The user has the option of selecting to view a selected message by clicking a button

ResultSet rs = null;
try
{
    startPage(response);
        rs = statement.executeQuery(sql);
        while(rs.next())
        {
            out.println("<tr>");
            out.println("<td align=center>"+rs.getString("Heading")+"</td>");
            out.println("");
            out.println("<td align=center>"+rs.getString("Username")+"</td>");
            out.println("");
            out.println("<td align=center>"+rs.getString("DatePosted")+"</td>");
            out.println("");
            out.println("<td align=center><form action=dbShowMessage?action='"+rs.getString("Heading")+"'method=post><input value=VIEW type=submit></form></td>");
            out.println("</tr>");
        }

    endPage(response);
}

The code compiles without any error, but when I invoke it using the web server the HTML page displays without the table containing the results or the buttons, but as soon as I remove the it displays everything; what am I doing wrong here.

Alternatively I tried it with a URL as follows:

out.println("<tr>");
out.println("<td align=center><a href=dbShowMessage?title="+rs.getString("Heading")+">"+rs.getString("Heading")+"</a></td>");
out.println("");
out.println("<td align=center>"+rs.getString("Username")+"</td>");
out.println("");
out.println("<td align=center>"+rs.getString("DatePosted")+"</td>");
out.println("");
out.println("</tr>");

Again it's the same out come; the links and the table displayed after invocation but as soon as I create the reference same story.

Not sure if it solves your problem, but you may want to put a space before the method attribute, that is, change

...Heading")+"'method=post...

to

...Heading")+"' method=post...

Besides this, what characters does your heading actually include? No citation-marks I assume...

You have invalid html...

Use this line:

out.println("<td align=center><form action=\"dbShowMessage?action='"+rs.getString("Heading")+"'\" method=\"post\"><input value=\"VIEW\" type=\"submit\"></form></td>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM