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;
}

String manipulation in JSTL

-0001-11-30

Hello All I'm a just a beginner and I'm need to write a web page that is totally based on JSTL. I found out that the standard java tab lib in JSTL doesnt really support string manipulation or regular expression like in perl. As such, I would like to

String Manipulation in BI Publisher Report Paramater

2015-10-11

Hi, My Problem is that I am not able to do string manipulation on BI Publisher report Prameters. Actually I want to Show Deptno-Dname in Menu(LOV).and when I select Certain combination like '10-Accounting',and while passing the parameter:Dept I wante

How do I Display a string from a servlet into a JSP Page???? NEED HELP!!!!

2015-10-11

Hi guys, How do I Display a string from a servlet into a JSP Page... Ive tried so many bloody things!..... Simply. I get text from JSP. The servlet does what ever it does to the string. Now. Ive create sessions and bean things,.... how the hell do I

How to pass a string from a servlet to client side

-0001-11-30

hi, i would like to generate a string from a servlet and pass it to the client side. the client will receive the string by using request.getParameter(). How should i implement this system? Could anybody give me some hint? Best Regards, HenryGreetings

How to send a String value  from Servlet to  Action class in Struts .

-0001-11-30

when iam trying to send string value from Servlet to Struts Action class through Http Session, iam getting null value, iam really in big confusion, help me.please elaborate clearly or put you code of both action and servlet here Are both of them in s

Q of Q and SQL string manipulation

-0001-11-30

I'm trying to find out if I can use SQL string manipulation within a Q of Q. Here's an example of what I'm trying to do: <cfquery dbtype="query" name="supportstatements_order"> Select *, right(SupportStatements.Name, 12) as short

Question about string manipulation

-0001-11-30

Hello, I am practicing with Java and trying to learn it, and have a quick question. I am trying to get the count on a string of numbers for after a decimal point. the numbers are generated as the result of a math, so I can't use index, since the loca

How to send byte array and String values to servlet from Swing application

2015-10-11

Hi all, I am new to swing, servlet, and socket connection. I have swing application to draw images and some input data. I dont know to send to server. byte[] buf = baos.toByteArray(); URL servletURL = new URL("http://10.70.70.1:8080/servlet/SaveImage

Very simple XSLT string replacing question

2015-10-11

Hi, This is a really simple question for you guys, but it took me long, and i still couldn't solve it. I just want to remove all spaces from a node inside an XML file. XML: <?xml version="1.0" encoding="UTF-8"?> <root inserted

How to send a String to a Servlet using a HTTP POST

2015-10-11

Well, I have designed a servlet that receives a HTTP POST, I was testing it using an HTML form to send (using POST) information, now, I have coded a Java App to send it a string, I don't know how to make the servlet recognize that info so it can make

C:import tag - Query string while calling servlet from JSP

2015-10-11

Hi, From my JSP I am calling a servlet to write back to the response stream of the JSP. From the JSP i am trying to pass some values into the query string as follows      <%      String strGetParameters = "/servlet/MyServlet?path=" + strPath;

How do I view previous strings of questions and answers on FF support?

-0001-11-30

I read this response but it really doesn't answer the question. "If you are logged on then you see a My Contributions item in the Filter bar at the top that goes to: https://support.mozilla.org/questions?filter=my-contributions" I don't see the

Classes and subclasses given as String input - question

-0001-11-30

Hello, I am new in Java so please don't laugh at my question!! I have a method as the following and I am trying to store objects of the type Turtle in a vector. However the objects can also be of Turtle subclasses: ContinuousTurtle, WrappingTurtle an

Can anyone help with string manipulation please?

-0001-11-30

I am trying to change this string 43180-1-0001-37 into the following expression 43181-0001-37 It is a string for Project No + Job Phase + Drawing No + Item No. The reduction of '43180' to '4318' is causing me the problem, I have the concatenation. Cu

Using CharAt for String Manipulation

-0001-11-30

Hi, I am new to Java and am taking a Java class. I am trying to put a social security number in "nnn-nn-nnnn" format where n is a digit 0-9. I don't know if it would work with the charAt method of the String class. Does anyone have any suggestio

Passing string variables between servlets

-0001-11-30

Hello I need to pass an SQL string constructed in Java servlet A to Java servlet B where it can be executed. For example: In servlet A I have: String cmd = ("select x from y where z"); cmd is then passed to the server as a hidden form field and

Sending a formatted string to a servlet

-0001-11-30

I'm trying to send a string that contains some carriage returns to a servlet. Right now what I'm doing is converting the contents of a file to bytes, then to a string. I write that string to a stream to the servlet. But once I pull the string from th

String manipulation: php to jsp

-0001-11-30

Im converting a menu generation program from PHP to JSP. Every page in my site has been allocated a page Id. A pages pageId determines how the primary navigation of the page displays. My Question I need to determine the sub Menu parent id To do this

Sending long XML strings from a servlet

-0001-11-30

Hi, I'm trying to send a long (16K) XML string from a Java servlet to a MSXML2.XMLHTTP40 client. The client doesn't receive any data when the string is longer than about 2K . I am using the POST method. I have tried increasing the buffer size on the