I wrote:
> Hemant writes:
> > I have validated the user from "login.html" by checking the data in
> > the database. Now if the user is a valid user i have to display aa
> > ALREADY existing html page. can i do this or will i have to write the
> > whole page in the servlet println code?
>
> 1) redirect the user to the page using the Location: header
> 2) load in the page file upon startup and substitute in any variables,
> then print it (there are template packages for this)
> 3) set up the existing HTML page as a JSP with code to insert the
> variables where appropriate, then in the first servlet, redirect
> the user to the JSP page.
Sorry, I just realized these look like sequential steps, when
what I meant was "here are three different ways of doing what you
want." Also, I agree with the poster who suggests in general keeping
as many pages static as you can.
To do a quick & dirty string substitution in java, use the
String.indexOf() method to find the old string, then cut the string
apart using String.substring() method, and paste it back together with
the new string. Or, if you're using Java 1.2, use a StringBuffer
object and use the StringBuffer.delete() and StringBuffer.insert()
methods. In either case, it's not pretty, but it works; go get a real
regexp package for more sophisticated stuff (there are URLs for two or
three pages on www.gnu.org).
Here's a quick example; of course you should probably add other
convenient verbs here and make some sort of "HtmlPage" class.
public class SubstitutableString extends Object ;
// pagetext is set to the body of the html page
String pagetext ;
public int substitute(int mark,
String oldstring,
String newstring) {
// Starting from index mark in the pagetext String,
// find the first occurrence of oldstring
// and replace it with newstring.
// mark is so you can can call this method from a loop and
// loop through the whole pagetext string.
// Find the first occurrence of oldstring
int start = pagetext.indexOf(oldstring, mark) ;
// indexOf returns -1 if no match, so check for -1
if (start != -1) {
// Okay, so start is the index of the first character of the
// string we're searching for, and we'll calculate end as the
// start index plus the length of the search string
int end = start + oldstring.length() ;
// This bit should properly be in a separate method:
// chop out the text before the search string and
// chop out the text after the search string and
// put it all back together with the new string and
// store the results n the class variable pagetext
String before = pagetext.substring(0, start) ;
String after = pagetext.substring(end, pagetext.length()) ;
pagetext = before + newstring + after ;
}
// return start; for a global search, code a loop that takes the
// returned start value, increments by length of the oldstring and
// calls substitute again.
return start ;
}
I haven't really worked with JSP yet; I don't really like the
design. However, I suspect the "cleanest" way to do all of this for
pages that aren't going to require a whole bunch of custom HTML
composition is probably to figure out what values you're going to
insert, code the page as a JSP, and use some mechanism (servlet
context?) to pass the values into the JSP, using standard JSP
facilities for doing the actual insertion. Could somebody who's a
little more familiar with JSPs suggest how this would be done?
For links to some intro tutorials for JSP, scroll to the bottom
of the page at http://java.sun.com/products/jsp/docs.html.
Steven J. Owens
[EMAIL PROTECTED]
[EMAIL PROTECTED]
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html