File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Passing parameters from jsp to servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing parameters from jsp to servlet" Watch "Passing parameters from jsp to servlet" New topic
Author

Passing parameters from jsp to servlet

m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
Hi All,
I am trying to pass the multiple selections from a list box to a servlet that will update my database with those selections. My servlet isn't behaving properly, the field I am trying to update is being changed to "null" in all records. Can someone look at my servlet and see where I am going wrong? Thanks

public class editLangProfile extends HttpServlet {

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

String PartID = request.getParameter("partID");
String[] Language = request.getParameterValues("selectedOptions");
String next = null;

if (PartID.length() != 0 ) {

try {


statement.executeUpdate("Update partdb.profile_lang set language = \"" + Language + "\" "
+ "where partid = \"" + PartID + "\" ");

next = "/partnerDB/partners/LangProfile.jsp?m=modified&partID=" + PartID;

connection.close();
}
catch (Exception e) {
PrintWriter out = response.getWriter();
out.println("<pre>");
PrintWriter errorOut = new PrintWriter(out);
e.printStackTrace(errorOut);
out.println("<br></pre>" + e);

}
}
else {
next = "/partnerDB/partners/LangProfile.jsp";
}

response.sendRedirect(next);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doPost(request, response);
}

}
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

What does your form look like on the client side?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
Hi Ben,
It doesn't appear to like my code, something about the html syntax. Any suggestions?
M.
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
I think I got it.



String pid = request.getParameter("partID");

<form action="/partnerDB/servlet/editLangProfile" method="get" style="margin:0">

// Connection
Vector langList = new Vector();
admin.getLanguage(langList);
String langString = " ";

for (int i = 0; i < langList.size(); i++) {
String lString = langList.get(i).toString();
langString = langString + lString.trim() ;
}

StringBuffer options = new StringBuffer();
ResultSet rs = statement.executeQuery("Select partid, language from partDB.profile_lang "
+ "where partid = '" + pid + "' ");
while (rs.next()) {
options.append("<option value=\"" + rs.getString("language") + "\">" + rs.getString("language") + "</option>\n");
}

<tr>
<td><select multiple size="10" id="availableOptions" name="availableOptions">
<%= langString %>
</select> </td>

<td valign="top">
<td class="tableimgcol" width="40" onSNAP="if (document.images) copySelected(availableOptions,selectedOptions)"><img src="/partnerDB/images/ArrowRightAdd.gif">
<br><onSNAP="if (document.images) copyAll(availableOptions,selectedOptions)"><img src="/partnerDB/images/ArrowDoubleRight.gif">
<br>
<br>
<td class="tableimgcol" width="40" onSNAP="if (document.images) remSelected(selectedOptions,availableOptions)"><img src="/partnerDB/images/ArrowLeftRemove.gif">
<br><onSNAP="if (document.images) remAll(selectedOptions,availableOptions)"><img src="/partnerDB/images/ArrowDoubleLeft.gif">

</td>

<td><select id="selectedOptions" name="selectedOptions" multiple size="10">
<%=options.toString()%>
</select></td>
</tr>
</table>
</div>
</div>
</div>

<input type="hidden" name="partID" value="<%= pid %>"></input>
<input type="hidden" name="hidden1"></input>


<div class="dialogbuttons">
<!-- InstanceBeginEditable name="dialogbuttons" -->
<input type="submit" value=" OK " class="mmhide_btn" onmiceover="hov(this,'mmhide_btn mmhide_btnhov')" onmiceout="hov(this,'mmhide_btn')" />
<input type="button" value=" Cancel " class="mmhide_btn" onmiceover="hov(this,'mmhide_btn mmhide_btnhov')" onmiceout="hov(this,'mmhide_btn')" onSNAP="window.location.href='LangProfile.jsp?partID=<%= pid %>'" />
</div>
</div>
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
okay, I have updated my servlet but now it is only taking one of the changes and updating all rows that match the where statement. Help! Below is the revised servlet:


public class editLangProfile extends HttpServlet {

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

String PartID = request.getParameter("partID");
String next = null;
String[] Language = request.getParameterValues("selectedOptions");

if (PartID.length() != 0 ) {

try {


for(int j=0; j<Language.length; j++)
{
statement.executeUpdate("Update partdb.profile_lang set language = \"" + Language[j] + "\" "
+ "where partid = \"" + PartID + "\" ");

}


next = "/partnerDB/partners/LangProfile.jsp?m=modified&partID=" + PartID;

connection.close();
}
catch (Exception e) {
PrintWriter out = response.getWriter();
out.println("<pre>");
PrintWriter errorOut = new PrintWriter(out);
e.printStackTrace(errorOut);
out.println("<br></pre>" + e);

}
}
else {
next = "/partnerDB/partners/LangProfile.jsp";
}

response.sendRedirect(next);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doPost(request, response);
}

}
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Looks like you're looping through all of the elements in the Language array and overwriting the same record with each value..

What, exactly, are you trying to do?

BTW: Please use the "Code" tags (button provided below) when posting your code.
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
Hi Ben,
Here is what I want to do, the second list box displays what is currently in the database. Any changes to the 2nd list box need to be made to a field in the database. I have been able to get it to add new additions to the database but not deletions. Below is my updated servlet:
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410



Why not just:
DELETE FROM PART.DB.PROFILE_LANG WHERE PARTID = '..'?
which would delete all of them.
You're replacing them anyway.

You're java code looks OK (at a glance). This is becoming more of a SQL issue.
You will find more SQL gurus in the JDBC forum, I think.
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
Hi Ben,
I don't want to delete all of them...I may have 5 different languages but only want to delete one, i can get it out of the listbox via javascript but then there is nothing to pass back to the servlet so it doesn't delete. Another example might be that a user enters 2 more languages and deletes 3, the additions are handled but because the list box has 3 blanks nothing is deleted. Does this make sense? Do I need to do something more complicated in the javascript to capture what is being deleted?
Thanks!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The simplest thing to do is blow them all away and replace with what's hightlighted in the select box. The user selects the ones she wants to keep and deselects the ones he doesn't want to keep.
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
I have tried that but here is what happens, if I delete one or more and don't do any additions then I get a servlet not available error and the database is not updated. If I add one to the select box everything is deleted except for the new addition, it appears that the only way to update the database is to highlight everything in the second select box that you want to add and/or keep. I think this may be confusing for the user to have to highlight everything even if they are only adding 1 item or trying to delete something. I thought i was capturing whatever was "appearing" in the 2nd list box but apparently I am only capturing what is "selected".
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If your page loaded with all avalable languages in the select box and their currently choosen ones highlighted, it shouldn't be confusing to them.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Actually, I've overstepped my boundries. It will be up to you to decide how you want to implement it.

It looks like you're getting your form parameter's correctly at this point.
From here, it's really more of an architecture or SQL/JDBC issue -- each of which has it's own forum on this site.
[ February 07, 2005: Message edited by: Ben Souther ]
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
There are two boxes: The first one contains a list of options to choose from and stays static the Second contains what is currently in the database. When the user clicks on items from the first list they appear in the second list, when they delete something from the second list they disappear, so what i need to capture is this new list in the second box. IE. the
Box 1 Box 2 (current) Box 2(after editing)
l1 l1 (currently what is in db) l1 (not selected, no changes)
12 l2 (delete from db)
l3 l3 (new addition) l3 (selected)

So box 2 is what gets sent to the servlet but since l1 is not selected it is deleted from the database. I don't really want to force the user to highlight something that doesn't need to be changed. I guess my question is how do I capture what is appearing in the text box since getParameterValues seems to only capture what is selected/highlighted?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You can't.
You'll need to re-think your strategy.

If you have any control over what browsers are supported, this can be done fairly easily with Javascript. If not, think about having two buttons on which the user can click.

Button one (Add Button) sends the list of selected items from the 'available' select list.

Button two (Remove Button) sends the list of selected itmes from the from the second 'Your current list' select box to be deleted.

If the two buttons sit right between the select lists and are images of arrows, it will be very intuitive for your end users.
[ February 07, 2005: Message edited by: Ben Souther ]
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
I already have the arrows set up between the 2 lists and javascript that gets the 2nd list the way the user wants it. I guess I will have to try to updated the database via javascript?? Hmmmm...maybe a hidden field in js...
Thanks for your help Ben
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Or, read the value of the button that's been clicked and branch accordingly.

Give both buttons the same name (ie: 'button_clicked').


[ February 07, 2005: Message edited by: Ben Souther ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing parameters from jsp to servlet
 
Similar Threads
HF example Weird result - help please
Compiling the servlet
Discrepancy in closing popup window in Netscape & IE
Wrapper cannot find servlet class
Hello world ejb3 problem