This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HTTP servlet problem

 
Sanjeev Arora
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet gurus,
I have a strange problem. Well, it might not be strange for everybody but at least it is for me. I have Tomcat version 4.0 installed on Windows 2000. I have my CLASSPATH variable correctly set. In fact, if my servlet (which extends HttpServlet) works just fine and displays all the HTML tags when run. However, when I add this extra line of code, it does not display today's date. Also when I do a 'view source' in the browser (IE 5.0), it does not display the line of HTML code either.
Here's the code:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleServlet extends HttpServlet
{
/**
* Handle the HTTP GET method by building a simple web page.
*/

String today = (new Date()).toString();
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
PrintWriterout;
Stringtitle = "Simple Servlet Output";
// set content type and other response header fields first
response.setContentType("text/html");
// then write the data of the response
out = response.getWriter();
out.println("<HTML><HEAD><TITLE>");
out.println(title);
out.println("</TITLE></HEAD><BODY>");
out.println("<H1>" + title + "</H1>");
out.println("<P>This is output from the <b>SimpleServlet</b>.");
out.println("How does it look like?");
&&& out.println("<P>Today's date is: "+today+"<P>");
out.println("</BODY></HTML>");
out.close();
}
}
The line that is not displayed is annotated with &&&. The other funny thing is that the file compiles successfully.
Can someone please help me out?
Thank you,
Sanjeev
 
Ganesh Anekar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Try using this..
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class simpleservelet extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res) throws
ServletException,java.io.IOException
{
doGet(req,res);
}
public void doGet(HttpServletRequest req,HttpServletResponse res) throws
ServletException,java.io.IOException
{
res.setContentType("text/html");
java.io.PrintWriter out=res.getWriter();

//ServletContext s=getServletContext();
HttpSession session=req.getSession(true);
/*
your code
*/
Ganesh

 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you restart your Tomcat server after adding your new line, compiling your file, and putting the class file where Tomcat can see it? Tomcat will cache class files, and often needs a restart to pick up the latest versions.
 
Ganesh Anekar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Actually That is the Bug of tomcat and it is reported in tomcat bug report...

U can not use tomcat for development...when u change any code and recompile it...to see that effect each time u have to restart u r tomcat server....

Ganesh
 
Sanjeev Arora
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. Unfortunately, I'd already tried shutting down and restarting Tomcat - before I posted my first message - since I knew about this bug. The new line of code never got displayed. Before trying Ganesh's code, I would like to first understand why my original code is not displayed completely. Any suggestions/advice are/is welcome.
Thanks,
Sanjeev
 
Sanjeev Arora
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put in Ganesh's code and then added the HTML inside the 'doGet' method. I still keep getting the same output as I did before I added the date field. When I do a 'view source' on the browser, I get the old tags. It looks like Tomcat has somehow cached my original servlet. I have no clue how to handle this. Can you guys please help?
Thanks a lot,
Sanjeev
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a dirty check . Just change the name of the servlet and try calling from browser.
I checked with TOmcat3.1 and got the new output after restarting tomcat.
regds
maha anna
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic