views:

40

answers:

2

Hi every one, I have really a problem that I don't know how to deal with it. I am using JSP and Servlet with the Eclipse IDE. First of all, the user fill an html table with the values that he has selected and written in the form. after that he will find his parameters displayed in the html table. the problem now is : the table contains in each row an edit button when clicking on it; the user should have the form automatically filled with values"previously selected" of the row. so it's how to reload the form with variables from html table. Note : I construct the table with a servlet. Please help.

A: 

Just prefill the value attribute of the HTML input elements accordingly. In the servlet, you put the bean with the data in the request scope, forward the request to the JSP and then in JSP do for example:

<input type="text" name="foo" value="${bean.foo}">

Note that when this is user-controlled data, you may risk XSS attacks. You should then escape the input using JSTL's fn:escapeXml:

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<input type="text" name="foo" value="${fn:escapeXml(bean.foo)}">
BalusC
A: 

For example you have to fields FirstName and LastName in your form and you are filling in the table.

When you submit the form in the servlet you construct an object POJO class in which you store first name and last name.

Your POJO class may be like this.

public class MyClass{
     private java.lang.String firstName = null;
     private java.lang.String lastName = null;
     public MyClass(java.lang.String firstName, java.lang.String lastName){
          this.firstName = firstName;
          this.lastName = lastName;
     }
     public void getFirstName(){
          return firstName;
     }
     public void getLastName(){
          return lastName;
     }
}

Now, in your servlet, you create a object of this POJO class like this and put it in a hash table. After that put the hash table in session.

 MyClass myClassObject = new MyClass(request.getParameter("firstName"), request.getParameter("lastName"));
 java.util.Hashtable htMyClassObjects = (java.util.Hashtable)request.getSession(false).getAttribute("htMyClassObjects");
 if (htMyClassObjects == null){ // For the first time, it will be null
      htMyClassObjects = new java.util.Hashtable();
 }
 htMyClassObjects.put(java.lang.String.valueOf(htMyClassObjects.size()), myClassObject);

In your table, in the button click, pass the hash table key associated with that row. For like this.

<input type = "button" onclick="location.href='myjsp.jsp?rowNumber=<%=hash table key%>'" />

Now when you process that button request, use this number and get the object from hash table and fill the fields.

My answer is a bit discriptive. But try to implement this.

RaviG