Current position:  Home > Default > String manipulation question (servlet)

String manipulation question (servlet)

Time:November 30
Advertisement
Hello,
I have a problem replacing parts of a String with values from a HttpServletRequest.
I have the following method:
public String replaceVariables(String s, HttpServletRequest request) {
     Enumeration e = request.getParameterNames();
     Vector v = new Vector();
     while(e.hasMoreElements()) {
          v.add(e.nextElement());
     String paramName;
     ListIterator li = v.listIterator();
     while(li.hasNext()) {
          paramName = (String) li.next();
          if (s.indexOf("#" + paramName + "#") > -1) {
               s = new String(new StringBuffer(s).replace(s.indexOf("#" + paramName + "#"), s.indexOf("#" + paramName + "#") + (("#" + paramName + "#").length()), request.getParameter(paramName).toString()));
     return s;
}What I am trying to do here is read in a line of text (String s) along with a the htpp servlet request. Put the names of the request paramaters into Vector v.
Next I try to check the String s for any part of the String that matches one of the parameter names surrounded by #'s. If there is a match then I replace the #string# with the value associated with it in the http servlet request.
The trouble is, my code on ly replaces the very first variable then stops, I cannot see why it does not do the rest.
I hope this makes sense to somebody, becuase it doesn't to me :-)
Thanks for any advise or help
Advertisement
Yep. I have just done a basic one for testing. The String I read in is:
username = #username#, action = #action#, email = #email#
And the html form information that is sent to the servlet is:
<input type="hidden" name="action" value="test">
<input type="hidden" name="username" value="testuser">
<input type="hidden" name="email" value="testemail">
....etc.
I don't know if it makes a difference, I don;t think it does, but I am reading in a whole file, and passing the file line by line to this method.
The method that passes each line is:
public Vector generateHTML(HttpServletRequest request) {
     Vector v = new Vector();
     try {
          File f = new File("/fakepath/" + request.getParameter("action") + ".template");
          FileReader fr = new FileReader(f);
          BufferedReader br = new BufferedReader(fr);
          String s;
          while((s = br.readLine()) != null) {
               s = replaceVariables(s, request);
               v.add(s);
     } catch (Exception e) {
          // ignore
     } finally {
          return v;
}