File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Replacing html tags in servlet code with a html file name ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Replacing html tags in servlet code with a html file name ??" Watch "Replacing html tags in servlet code with a html file name ??" New topic

Replacing html tags in servlet code with a html file name ??

Vaibhav Shridish

Joined: Jun 06, 2002
Posts: 28
can someone help me solve this problem ?
Problem 1] is there some way i can replace the
out.println statements by directing the control to
another html page.
How do i achieve this ??
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
PrintWriter out=new PrintWriter(resp.getOutputStream());
String email = req.getParameter("email");
out.println("Subscription Success");
out.println("Error occured please reenter ur email id");
out.println("<form method="POST" action="http:/\"+"/"+"><center>Enter Your Email Address : <input name = "email" value = " " type = "text" size= "40"><br><br><input type="submit" value="Subscribe"><br></center></form>");

Edited by MC to use UBB Code tags
(oh never mind, it blows up the page)
[ February 21, 2003: Message edited by: Mike Curwen ]

Vaibhav Shridish
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
There are a couple of ways of doing this. The RequestDispatcher interface has a forward method that will transfer control to an html file without returning to the browser first. The HttpServletResponse interface has a send redirect method that will send a redirect call back to the browser and let the browser know that it should go to the url of the html file.

Matthew Phillips
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I guess, u can use FileStream methods, so that you can parse the text/html file as a char stream and then wrap in a String and append to the writer output.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695


Let's not get too fancy.

That's what 'include' is for.

From the tomcat examples app, modify the hello world servlet as follows:

the file included.html, which is located at the root of the application, contains nothing but:And our output is:

If, on the other hand, you want to do some of your processing in the html page, that is "Transfer" the control to that page, because you don't like writing HTML inside out.print() statements (and who does?)... you want a JSP.
I agree. Here's the link:
subject: Replacing html tags in servlet code with a html file name ??
It's not a secret anymore!