• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem executing the servlet  RSS feed

 
Ranch Hand
Posts: 640
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a problem executing the following servlet code:
The servlet class is stored under the proper directory structure.
That is myDevelopment/WEB-INF/classes/myServlets/ShowSession
myDevelopment is stored under the tomcat/webApps.
I have set the context to myDevelopment directory in the servlet.xml file
The code is:
package myServlets;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class ShowSession extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Session tracking example";
HttpSession session = request.getSession(true);
String heading;
Integer accessCount = (Integer)session.getAttribute("accessCount");
if(accessCount == null)
{
accessCount = new Integer(0);
heading = "Welcome, newcomer";
}
else
{
heading = "Welcome back";
accessCount = new Integer(accessCount.intValue()+1);
}

session.setAttribute("accessCount",accessCount);
String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
out.println(docType + "<Html>\n"+"<head><title>"+ title+
"<body bgcolor = \" #FDF5E6\">\n"+
"<H1 align=\"center\">"+ heading +"</H1>\n"+
"<h2>Information on your Session:</h2>\n"+
"<table border=1 align=\"center\">\n"+
"<tr BGCOLOR=\"#FFAD00\">\n"+
"<th>Info Type<Th>Value\n"+
"<tr>\n"+
"<td>ID\n"+
"<TD>"+session.getId()+"\n"+
"<tr>\n"+
"<td>Creation time\n"+
"<td>"+
new Date(session.getCreationTime())+"\n"+
"<tr>\n"+
"<td>Time of last Access\n"+
"<td>"+
new Date(session.getLastAccessedTime())+"\n"+
"<tr>\n"+
"<td> Number of previous accesses\n"+
"<td>"+accessCount+"\n"+
"</table>\n"+
"</body></html>");

}

public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
doGet(request,response);
}

}
whenever I try to access the above servelt through the URL: http://localhost/myDevelopment/servlet/myServlets.ShowSession
I don't get any kind of error but the browser doesn't show any HTML code..I mean it displays blank browser..
any help is appreciated..
thaks in advance..
trupti
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello trupti,
In your doGet() method, the tags <title>, <head> were not closed properly. I concatenated "</title></head>"+
to your original code, it works fine. I also noticed some of your <td>,<tr> tags also were not closed properly.

Thambi
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Trupti,
I did try your code. It did not work because you forgot the enclosing tag of 'title'...
Then you must add "</title>" in your Html code.
Thanks.
 
trupti nigam
Ranch Hand
Posts: 640
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thambi and engin..
I didn't get any compilation error..so never bothered to check my code..for HTML stuff.
Thanks a lot..
I didn't knew that a small mistake in html code can cause these kinds of errors..(not exactly errors..but wierd result.)
thanks
Trupti
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!