• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

Requesting help to create a webpage log file that counts hits

Hello and thank you in advance for you help.  I have several hundred web pages located in
www.domain.com/MainCategory/Category1/page1.jsp
www.domain.com/MainCategory/Category1/page2.jsp
www.domain.com/MainCategory/Category2/page1.jsp
etc....
I'd like to add code to each of these pages to append a log file located in www.domain.com/MainCategory.  If I could get a date stamp and the address visited added to a text file each time someone visited it then that would be swell.  What I will do is process the text file each month and count the hits to each of the 700 or so sites.  I have access to the www.domain.com/MainCategory folder, but not to the server root level itself.  Is it possible to do this without having to put something on the server root level?
Thanks,
T
0
tkeiffer
Asked:
tkeiffer
  • 35
  • 21
  • 21
  • +1
3 Solutions
 
CEHJCommented:
Do you not have access to your server logs? If not, that's a serious drawback for anything remotely serious
0
 
tkeifferAuthor Commented:
No.  Is there not a way to have the js write to a file on
 www.domain.com/MainCategory
and just maintain it there?
0
 
CEHJCommented:
I would change you host immediately. In the meantime you can write to a file, yes

File f = new File(context.getRealPath("/logs/access.log"));
PrintWriter out = new PrintWriter(new FileWriter(f));
out.printf("Client %s accessed at %s\n", req.getRemoteAddrr(), new Date());
out.close();

Or use log4j
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
tkeifferAuthor Commented:
Hi.  Thank you for the info.  However, I have a couple more questions.  First, where in the html do I put this?  Second, Does this file need to exist already?.  I'm a novice when it comes to this particular procedure so I kind of need some hand holding.
Thanks,
Tod
0
 
CEHJCommented:
Oops that should have been

PrintWriter out = new PrintWriter(new FileWriter(f, true));

>> First, where in the html do I put this?

That needs to be in jsp or a servlet

>>Does this file need to exist already?.

No
0
 
tkeifferAuthor Commented:
I still don't understand where exactly I need to put this.  I have 700 .jsp files.  Where in the jsp file do I put the code:

File f = new File(context.getRealPath("/logs/access.log"));
PrintWriter out = new PrintWriter(new FileWriter(f, true));
out.printf("Client %s accessed at %s\n", req.getRemoteAddrr(), new Date());
out.close()

Thanks,
Tod
0
 
tkeifferAuthor Commented:
...and, will the file get appended each time one of the 700 pages trys to write to it?
0
 
CEHJCommented:
>> I have 700 .jsp files.

If you have that many it would be better to have the code centralized. Do you have a controller servlet or is each jsp accessed directly?
0
 
tkeifferAuthor Commented:
I generate all these 700 pages from an excel generater that concatenates code fields. If I change it onece then it propogates to all 700 pages.
0
 
tkeifferAuthor Commented:
I just need to figure out where in the jsp code to put it, such as after
<%@ page import="org.apache.regexp.*"%>
but before:
<head>
0
 
tkeifferAuthor Commented:
Oh, each jsp is accessed directly.
0
 
CEHJCommented:
That would be OK yes
0
 
CEHJCommented:
Make sure the directory structure is in place (the 'logs' directory)
0
 
tkeifferAuthor Commented:
I'm sorry to keep asking questions.  But I still can't figure out where to put this based on your responses.  Does your "OK Yes" mean to put it after
<%@ page import="org.apache.regexp.*"%>
but before:
<head>
Or, does it need to go somewhere after <head>?
????
0
 
rrzCommented:
Try using CEHJ's code like this.  
<%@ page import="org.apache.regexp.*,java.io.*"%>
<%
     File f = new File(application.getRealPath("/MainCategory/access.log"));
     PrintWriter out = new PrintWriter(new FileWriter(f, true));
     out.printf("Client %s accessed at %s\n", req.getRemoteAddrr(), new Date());
     out.close()
%>
<head>


0
 
rrzCommented:
like this  
<%@ page import="org.apache.regexp.*,java.io.*"%>
<%
     File f = new File(application.getRealPath("/MainCategory/access.log"));
     PrintWriter out = new PrintWriter(new FileWriter(f, true));
     out.printf("Client %s accessed at %s\n", request.getRemoteAddrr(), new Date());
     out.close()
%>
<head>
0
 
rrzCommented:
I tested it this time.  
<%@ page import="org.apache.regexp.*,java.io.*"%>
<%
     File f = new File(application.getRealPath("/MainCategory/access.log"));
     PrintWriter writer = new PrintWriter(new FileWriter(f, true));
     writer.printf("Client %s accessed at %s\n", request.getRemoteAddr(), new Date());
     writer.close();
%>
0
 
objectsCommented:
be easier to use a filter/interceptor to implement something like that. no need to clutter all your pages, you should keep it seperate.
0
 
objectsCommented:
theres an example here that basically does what u need:

http://java.sun.com/products/servlet/Filters.html
0
 
objectsCommented:
U also should avoid putting java code in your jsp, its a terrible practice and just becoomes a nightmare to maintain. By using a filter your jsp doesn't get cluttered with java code, and logic that isn't related to whats displayed on the page (which is what jsp purpose is).
0
 
CEHJCommented:
The best solution is to get the information that has already been collected and to which you should have access. There are tons of free programs that will turn weblogs into stats for you. If your hosting company won't let you have them, then ditch them
0
 
tkeifferAuthor Commented:
The reason I have to do this is this.  I "own" a part of our company website which I have full control over.  I do not have access to the root or the servers.  I created an excel program that generates all of my jsp pages for my part of the site.  My site resides on www.domain.com/MainCategory.  I want to count the hits to my area of the site so I want to make an addition to my code so that they append a log every time someone visits them.  The code change only needs to be put in one place since I am generating all of these pages from a concatenating excel program I created.  I can only put the file on www.domain.com/MainCategory somewhere as that is the only place I have access to.  I hope that helps clarify the situation?
T
0
 
rrzCommented:
Are you limited to just JSPs  ?
objects suggestion is best.  But, do you have access to your context's  web.xml ?    Can you complie a Filter and place into classes or lib folder ?  
Otherwise  just use CEHJ's  idea   with my last post.      rrz
0
 
rrzCommented:
What web server are you using ?  If you don't know then run the following  JSP.  
Server is <%= application.getServerInfo()%><br/>
JSP version is <%= JspFactory.getDefaultFactory().getEngineInfo().getSpecificationVersion()%><br/>
Servlet API is <%= application.getMajorVersion()%>.<%=application.getMinorVersion()%>
0
 
tkeifferAuthor Commented:
Yes, I am limited to just JSPs.  I am getting errors when I put this in to my test environment:

<%@ taglib uri="/ps_www_v2" prefix="ps_www" %>
<%@ page import="com.testsite.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.net.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="org.apache.regexp.*,java.io.*"%>
<%
     File f = new File(application.getRealPath("/logs/access.log"));
     PrintWriter writer = new PrintWriter(new FileWriter(f, true));
     writer.printf("Client %s accessed at %s\n", request.getRemoteAddr(), new Date());
     writer.close();
%>
<head>
<title>


The errors are:

Error 500--Internal Server Error
weblogic.servlet.jsp.CompilationException: Compilation of c:\applogs\portal\webserv\weblogic\debug\jsp_servlet\_folder1\_folder2\__page1.java failed.
      at weblogic.servlet.jsp.JspStub.compilePage(JspStub.java:446)
      at weblogic.servlet.jsp.JspStub.prepareServlet(JspStub.java:220)
      at weblogic.servlet.jsp.JspStub.prepareServlet(JspStub.java:172)
      at weblogic.servlet.internal.ServletStubImpl.getServlet(ServletStubImpl.java:504)
      at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:349)
      at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
      at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6310)
      at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
      at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
      at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3622)
      at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2569)
      at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
      at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)
Caused by: java.io.IOException: Compiler failed executable.exec
      at weblogic.utils.compiler.CompilerInvoker.compileMaybeExit(CompilerInvoker.java:470)
      at weblogic.utils.compiler.CompilerInvoker.compile(CompilerInvoker.java:329)
      at weblogic.utils.compiler.CompilerInvoker.compile(CompilerInvoker.java:337)
      at weblogic.utils.compiler.CompilerInvoker.compile(CompilerInvoker.java:322)
      at weblogic.servlet.jsp.JspStub.compilePage(JspStub.java:419)
      ... 12 more
 
0
 
CEHJCommented:
Try instead

<%
      try {
            File f = new File(application.getRealPath("/logs/access.log"));
            PrintWriter writer = new PrintWriter(new FileWriter(f, true));
            writer.printf("Client %s accessed at %s\n", request.getRemoteAddr(), new Date());
            writer.close();
      }
      catch(IOException e) {
            out.printStackTrace();
      }
%>
0
 
tkeifferAuthor Commented:
This came up with the same errors.  Is the word "try" supposed to be in there?
0
 
CEHJCommented:
>> Is the word "try" supposed to be in there?

Yes. You need to import java.util.Date too
0
 
CEHJCommented:
Or better, change

>>writer.printf("Client %s accessed at %s\n", request.getRemoteAddr(), new Date());

to

writer.printf("Client %s accessed at %s\n", request.getRemoteAddr(), new java.util.Date());
0
 
rrzCommented:
What does
<%=application.getRealPath("/logs/access.log")%>
print out ?
What is the path to the logs folder that you created ?
0
 
tkeifferAuthor Commented:
Hmm, I'm still getting errors.  I wonder if there's some restriction in my test environment.  This is the code I put in.

<%@ page import="org.apache.regexp.*,java.io.*"%>
<%
      try {
            File f = new File(application.getRealPath("/logs/access.log"));
            PrintWriter writer = new PrintWriter(new FileWriter(f, true));
            writer.printf("Client %s accessed at %s\n", request.getRemoteAddr(), new java.util.Date());
            writer.close();
      }
      catch(IOException e) {
            out.printStackTrace();
      }
%>
0
 
rrzCommented:
>Hmm, I'm still getting errors.  
Show us the errors.
0
 
tkeifferAuthor Commented:
Error 500--Internal Server Error
weblogic.servlet.jsp.CompilationException: Compilation of c:\applogs\portal\webserv\weblogic\debug\jsp_servlet\_folder1\_folder2\__page1.java failed.
      at weblogic.servlet.jsp.JspStub.compilePage(JspStub.java:446)
      at weblogic.servlet.jsp.JspStub.prepareServlet(JspStub.java:220)
      at weblogic.servlet.jsp.JspStub.prepareServlet(JspStub.java:172)
      at weblogic.servlet.internal.ServletStubImpl.getServlet(ServletStubImpl.java:504)
      at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:349)
      at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
      at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6310)
      at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
      at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
      at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3622)
      at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2569)
      at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
      at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)
Caused by: java.io.IOException: Compiler failed executable.exec
      at weblogic.utils.compiler.CompilerInvoker.compileMaybeExit(CompilerInvoker.java:470)
      at weblogic.utils.compiler.CompilerInvoker.compile(CompilerInvoker.java:329)
      at weblogic.utils.compiler.CompilerInvoker.compile(CompilerInvoker.java:337)
      at weblogic.utils.compiler.CompilerInvoker.compile(CompilerInvoker.java:322)
      at weblogic.servlet.jsp.JspStub.compilePage(JspStub.java:419)
      ... 12 more
0
 
rrzCommented:
Please try it this way. Make simple test page with  just    

 
<%@ page import="org.apache.regexp.*,java.io.*"%>
<%
      try {
            File f = new File(application.getRealPath("/MainCategory/access.log"));
            PrintWriter writer = new PrintWriter(new FileWriter(f, true));
            writer.printf("Client %s accessed at %s\n", request.getRemoteAddr(), new java.util.Date());
            writer.close();
      }
      catch(IOException e) {
            out.print( e.getMessage());
      }
%>
rrz
0
 
rrzCommented:
I think this is what CEHJ  meant to post.
<%@ page import="org.apache.regexp.*,java.io.*"%>
<%
      try {
            File f = new File(application.getRealPath("/logs/access.log"));
            PrintWriter writer = new PrintWriter(new FileWriter(f, true));
            writer.printf("Client %s accessed at %s\n", request.getRemoteAddr(), new java.util.Date());
            writer.close();
      }
      catch(IOException e) {
            e.printStackTrace(writer);
      }
%>
0
 
tkeifferAuthor Commented:
Exact same error as earlier.
0
 
rrzCommented:
That last one will appear in log file. The one before gets printed to response.
0
 
rrzCommented:
Did you try that last one. Should be stacktrace in log file.
0
 
CEHJCommented:
You need >= Java 1.5 for that code. Do you have it?
0
 
CEHJCommented:
Otherwise it'll need to be

writer.println("Client " +  request.getRemoteAddr() + " accessed at " + new java.util.Date());
0
 
tkeifferAuthor Commented:
Hi,
Sorry for the delay, I've been out of town.  I'm not sure if I have Java 1.5 or not.  I put that new line in and I am still receiving the errors:

Error 500--Internal Server Error
weblogic.servlet.jsp.CompilationException: Compilation of c:\applogs\portal\webserv\weblogic\debug\jsp_servlet\_folder1\_folder2\__page1.java failed.
      at weblogic.servlet.jsp.JspStub.compilePage(JspStub.java:446)
      at weblogic.servlet.jsp.JspStub.prepareServlet(JspStub.java:220)
      at weblogic.servlet.jsp.JspStub.prepareServlet(JspStub.java:172)
      at weblogic.servlet.internal.ServletStubImpl.getServlet(ServletStubImpl.java:504)
      at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:349)
      at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:431)
      at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
      at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6310)
      at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
      at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
      at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3622)
      at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2569)
      at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
      at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)
Caused by: java.io.IOException: Compiler failed executable.exec
      at weblogic.utils.compiler.CompilerInvoker.compileMaybeExit(CompilerInvoker.java:470)
      at weblogic.utils.compiler.CompilerInvoker.compile(CompilerInvoker.java:329)
      at weblogic.utils.compiler.CompilerInvoker.compile(CompilerInvoker.java:337)
      at weblogic.utils.compiler.CompilerInvoker.compile(CompilerInvoker.java:322)
      at weblogic.servlet.jsp.JspStub.compilePage(JspStub.java:419)
      ... 13 more
 
0
 
objectsCommented:
there may well be limitations on what your provider allows
how do u currently create your jsp's?
create a simple test jsp with some scriptlet to see if that works.

0
 
tkeifferAuthor Commented:
I create my jsp's from a spreadsheet I created.  It concatenates a bunch of fields and essentially creates a text file with a .jsp extension.  Could you possibly provide a scriptlet that you know works that I could test?  I'm kind of scriptlet-writing-challenged :)  Thanks for  your help. - T
0
 
objectsCommented:
> I'm kind of scriptlet-writing-challenged :)

a good way to be, they are evil :)
0
 
objectsCommented:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
<%
   out.println("Test");
%>
</body>
</html>
0
 
tkeifferAuthor Commented:
That compiled alright without error.
0
 
objectsCommented:
ok, now try this:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<%
            File f = new File(application.getRealPath("/logs/access.log"));
            PrintWriter writer = new PrintWriter(new FileWriter(f, true));
            writer.println("test "+new Date());
            writer.close();
%>

<head>
</head>
<body>
<%
   out.println("Test");
%>
</body>
</html>
0
 
tkeifferAuthor Commented:
That got the same compile errors as earlier:

Error 500--Internal Server Error
weblogic.servlet.jsp.CompilationException: Compilation of c:\applogs\portal\webserv\weblogic\debug\jsp_servlet\_folder1\_folder2\__page1.java failed.
      at weblogic.servlet.jsp.JspStub.compilePage(JspStub.java:446)
      at weblogic.servlet.jsp.JspStub.prepareServlet(JspStub.java:220)
      at weblogic.servlet.jsp.JspStub.checkForReload(JspStub.java:157)
      at weblogic.servlet.internal.ServletStubImpl.getServlet(ServletStubImpl.java:508)
      at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:349)
      at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
      at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6310)
      at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
      at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
      at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3622)
      at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2569)
      at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
      at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)
Caused by: java.io.IOException: Compiler failed executable.exec
      at weblogic.utils.compiler.CompilerInvoker.compileMaybeExit(CompilerInvoker.java:470)
      at weblogic.utils.compiler.CompilerInvoker.compile(CompilerInvoker.java:329)
      at weblogic.utils.compiler.CompilerInvoker.compile(CompilerInvoker.java:337)
      at weblogic.utils.compiler.CompilerInvoker.compile(CompilerInvoker.java:322)
      at weblogic.servlet.jsp.JspStub.compilePage(JspStub.java:419)
      ... 12 more
 
0
 
objectsCommented:
ok were getting closer to answer (though it looks like its not going to be a nice one)

try this:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<%
            String path = application.getRealPath("/logs/access.log");
%>

<head>
</head>
<body>
<%=
   out.println("Test");
%>
</body>
</html>
0
 
objectsCommented:
then try this:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<%
            String path = application.getRealPath("/logs/access.log");
             File f = new File(path);
%>

<head>
</head>
<body>
<%=
   out.println("Test");
%>
</body>
</html>


I suspect one or both will also fail.
Which suggests the hosting is blocking you accessing the file system from your app (which is probably a sensible thing to do).
0
 
rrzCommented:
Typo in those last two from objects.    Instead of  
><%=
   out.println("Test");
%>
try    this line    
path is <%=application.getRealPath("/logs/access.log")%>    
0
 
objectsCommented:
woops.  thanks for that :)
0
 
tkeifferAuthor Commented:
Same compiling errors from earlier for the file I created javatest.jsp.  Does this mean there are limitations?  Is there any other way to get this to write to a log file that you can think of?
0
 
objectsCommented:
it would appear you do not have access to disk so no there isn't.
you could perhaps record hits to memory, and email them on a regular basis. You could lose data though and its messy.

Only other way would be to use an external hit counter.
0
 
tkeifferAuthor Commented:
bummer.  I can't use an external counter because the entry into the site is password protected.  is there a way to set it up to send an email or something with this info each time it is accessed?
0
 
objectsCommented:
> is there a way to set it up to send an email or something with this info each time it is accessed?

sure (assuming u have access to an smtp server), but I hope its a very low access site :)

you'd need to add a scriptlet to send an email on every page.

theres a simple example here:
http://www.objects.com.au/java/qa/321498399.html
0
 
CEHJCommented:
You could try to proxy this by hitting a servlet elsewhere from your jsp
0
 
tkeifferAuthor Commented:
Stop the press.  I was going back through the notes and I realized that I missed something from before.    OBJECTS, from 10.23.2007 at 08:07PM MDT, ID: 20135914, told me to run a test.  I received that response and ran the test and missed the two follow ups from:

rrz@871311:Typo in those last two from objects.    Instead of  
><%=
   out.println("Test");
%>
try    this line    
path is <%=application.getRealPath("/logs/access.log")%>  



THIS WORKED WHEN I CORRECTED THE CODE.  Does this mean we can make this work????  When I put this correction in, I received this on my page instead of compiling errors:

path is C:\WebLogic\user_projects\domains\knowledgebrowser\applications\logs\access.log

0
 
rrzCommented:
Ok,  lets try writing to a file again.    If the code throws  java.io.IOException   again   then you should try CEHJ idea of catching it on the page. Maybe we can get a message to explain error.  Code using everybody's  idea  could look like this.  
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<%@ page import="java.io.*"%>
<%
      try {
            File f = new File(application.getRealPath("/access.log"));
            PrintWriter writer = new PrintWriter(new FileWriter(f, true));
            writer.println("Client " +  request.getRemoteAddr() + " accessed at " + new java.util.Date());
            writer.close();
      }
      catch(IOException e) {
            out.print( e.getMessage());
      }
%>
path is <%=application.getRealPath("/access.log")%>
rrz
0
 
tkeifferAuthor Commented:
That code worked without error giving me the result:

path is C:\WebLogic\user_projects\domains\knowledgebrowser\applications\access.log
0
 
tkeifferAuthor Commented:
Should that code have added some sort of record/content to access.log?
0
 
objectsCommented:
>             out.print( e.getMessage());

i wouldn't display the error message to the page, better to just log it

0
 
tkeifferAuthor Commented:
Sorry, I'm a bit slow sometimes.  Should it have added the message I saw on the screen to the access.log?  The message I saw on the screen for that page was:
path is C:\WebLogic\user_projects\domains\knowledgebrowser\applications\access.log
0
 
objectsCommented:
>             writer.println("Client " +  request.getRemoteAddr() + " accessed at " + new java.util.Date());

that goes to the access log

> path is <%=application.getRealPath("/access.log")%>

that goes to the page
0
 
tkeifferAuthor Commented:
So I think I misunderstood something.  Above in "10.23.2007 at 08:11PM MDT, ID: 20135927", rrz said to swap out some code from your code in "10.23.2007 at 08:07PM MDT, ID: 20135914".  According to my interpretation that would leave me with this code:


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<%@ page import="java.io.*"%>
<%
      try {
            File f = new File(application.getRealPath("/access.log"));
            PrintWriter writer = new PrintWriter(new FileWriter(f, true));
            writer.println("Client " +  request.getRemoteAddr() + " accessed at " + new java.util.Date());
            writer.close();
      }
      catch(IOException e) {
            out.print( e.getMessage());
      }
%>

<head>
</head>
<body>
> path is <%=application.getRealPath("/access.log")%>
</body>
</html>





However, I have a feeling this is wrong.  Is this what I was supposed to do?  In the response pasted below, swapping this line out only changes what is printed out on screen.





>             writer.println("Client " +  request.getRemoteAddr() + " accessed at " + new java.util.Date());

that goes to the access log

> path is <%=application.getRealPath("/access.log")%>
0
 
objectsCommented:
whats in your (publicly viewable) access log?
0
 
tkeifferAuthor Commented:
Sorry, I was waiting for the crawl to run.  This runs without compiler errors in production.  On the page, I get the output:
> path is /wwwsh/appsrv/access.log
However, it doesn't appear to be writing to that log.  Should it be writing to that log?
Thanks,
Tod
0
 
objectsCommented:
no it shouldn't be, thats jusyt for testing purposes to check the file is written at correct place.
0
 
tkeifferAuthor Commented:
Thanks so much for you help.  How do we take it to the next step and attempt to write to the log?
0
 
objectsCommented:
           File f = new File(application.getRealPath("/access.log"));
            PrintWriter writer = new PrintWriter(new FileWriter(f, true));
            writer.println("Client " +  request.getRemoteAddr() + " accessed at " + new java.util.Date());
            writer.close();

that code is writing to the log
0
 
objectsCommented:
and setup  a development version of your site on a local box, it'll make your job a a lot easier :)
0
 
tkeifferAuthor Commented:
I apologize, this medium is not condusive for easy communication.  Earlier I asked if the code, as it was pasted, should write to the log.  Your response was 10.26.2007 at 08:22PM MDT, ID: 20160210 was "no it shouldn't be".   I then asked in "How do we take it to the next step and attempt to write to the log" in response 10.26.2007 at 08:52PM MDT, ID: 20160300.  Your response in 10.26.2007 at 08:58PM MDT, ID: 20160312 says that code is writing to the log.  So I take it that the code, as it is in 10.26.2007 at 04:48PM MDT, ID: 20159596, should not only be giving me the Path to the log in the browser, but it should also be writing to the access.log.  Correct?  It DOES seem to be returning the path to the browser, but it does NOT appear to be writing to the log.  Perhaps I am experiencing a permissions issue.
0
 
CEHJCommented:
The output showing the path does NOT go to the log - it's just there for debugging purposes. The following will tell you whether you can write to the log file

<%@ page import="java.io.*"%>
<%
      try {
            File f = new File(application.getRealPath("/access.log"));
            out.println("Can we write to the log file? " + f.canWrite());
            PrintWriter writer = new PrintWriter(new FileWriter(f, true));
            writer.println("Client " +  request.getRemoteAddr() + " accessed at " + new java.util.Date());
            writer.close();
      }
      catch(IOException e) {
            writer.println( e.getMessage());
      }
%>
0
 
objectsCommented:
> I apologize, this medium is not condusive for easy communication.

don't worry, its a well documented fact

> Earlier I asked if the code, as it was pasted, should write to the log.  Your response was 10.26.2007 at 08:22PM MDT, ID: 20160210 was "no it shouldn't be".

was referring to the line
> path is /wwwsh/appsrv/access.log

the code u posted should write a line to the log *and* display the path to the page

is the page displaying anything other than the path?


0
 
objectsCommented:
>             writer.println( e.getMessage());

that should be:

            out.println( e.getMessage());

or write it to the log (which you should be doing once you go live with this)
0
 
CEHJCommented:
>>that should be: ..........

Why?
0
 
tkeifferAuthor Commented:
This is confusing.  The last working version I have is from RRZ ID 20159086 and is pasted below.  The page is displaying the path ONLY, and does not appear to be writing to a log.  The CEHJ code ID: 20160686 (thank you by the way for helping) causes compiler errors similar to the ones posted earlier.  

Objects, are you saying in your comment ID: 20160772, that in the code below, I should do the replacement in order for it to write to the log?  I tried replacing writer.println.. with out.println... and I got a compiler error.  I just need to figure out why it's printing to screen but not the log.



<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<%@ page import="java.io.*"%>
<%
      try {
            File f = new File(application.getRealPath("/access.log"));
            PrintWriter writer = new PrintWriter(new FileWriter(f, true));
            writer.println("Client " +  request.getRemoteAddr() + " accessed at " + new java.util.Date());
            writer.close();
      }
      catch(IOException e) {
            out.print( e.getMessage());
      }
%>

<head>
</head>
<body>
> path is <%=application.getRealPath("/access.log")%>
</body>
</html>
0
 
CEHJCommented:
Sorry tkeiffer, that should have been

<%@ page import="java.io.*"%>
<%
      PrintWriter writer = null;
      try {
            File f = new File(application.getRealPath("/access.log"));
            out.println("Can we write to the log file? " + f.canWrite());
            writer = new PrintWriter(new FileWriter(f, true));
            writer.println("Client " +  request.getRemoteAddr() + " accessed at " + new java.util.Date());
            writer.close();
      }
      catch(IOException e) {
            if (writer != null) {
                  writer.println( e.getMessage());
            }
      }
%>
0
 
tkeifferAuthor Commented:
CEHJ.  Thank you.  In the test environment I received the following in my browser:

Can we write to the log file? true > path is C:\WebLogic\user_projects\domains\knowledgebrowser\applications\access.log

I wouldn't find out until the crawl runs tonight whether or not it writes to the access.log or not.  I'll give an update after the crawl.  Thank you all so much for helping me out on this.
0
 
CEHJCommented:
Before it does run, you'd better correct yet another error i made ;-)


<%@ page import="java.io.*"%>
<%
      PrintWriter writer = null;
      try {
            File f = new File(application.getRealPath("/access.log"));
            //out.println("Can we write to the log file? " + f.canWrite());
            writer = new PrintWriter(new FileWriter(f, true));
            writer.println("Client " +  request.getRemoteAddr() + " accessed at " + new java.util.Date());
      }
      catch(IOException e) {
            if (writer != null) {
                  writer.println(e.getMessage());
            }
      }
      finally {
            if (writer != null) {
                  writer.close();
            }
      }
%>

Now we know that file is writable, you can make the commenting-out as above
0
 
tkeifferAuthor Commented:
I copied and pasted the code exactly as you put it above.  That new code compiled without error and returned:
> path is C:\WebLogic\user_projects\domains\knowledgebrowser\applications\access.log

I won't know until tonight if it is writing or not.  Thanks again for your help.
0
 
objectsCommented:
> I should do the replacement in order for it to write to the log?

no to write the error to the page, otherwise u won't know what it is :)

>  I tried replacing writer.println.. with out.println... and I got a compiler error.

sorry type, should be print, not println

> I won't know until tonight if it is writing or not.

Why not, how are you checking the log contents?
It should be inside your webapp (which aint a good spot for it but thats another story)
so you can load it via the browser
0
 
tkeifferAuthor Commented:
Fantastic, the log was successfully written to.   The contents of the log are:
Client 172.31.251.142 accessed at Fri Oct 26 19:02:10 PDT 2007
Client 172.31.251.153 accessed at Sat Oct 27 18:11:55 PDT 2007
Client 172.31.251.153 accessed at Sat Oct 27 20:41:17 PDT 2007
Client 172.31.251.153 accessed at Mon Oct 29 06:26:09 PST 2007

Thank you all so much for your help.  Before I go, In the original request I wanted to capture the name of the page, such as "test.jsp", or even the full path to the file, somewhere in the log.  That way I can use this in multiple files and be able to differenciate between log stamps.  Is there a simple Java procedure to do this?
0
 
CEHJCommented:
Try

writer.println("Client " +  request.getRemoteAddr() + " accessed " + request.getPathTranslated() + " at " + new java.util.Date());
0
 
rrzCommented:
I am glad you finally got it working. Did we ever figure out what was causing the error ?   Anyway you can add these lines.  
            writer.println("Request URI was " + request.getRequestURI());
            writer.println("Request URL was " + request.getRequestURL());
0
 
CEHJCommented:
Sorry - my suggestion wasn't good - use rrz's ;-)
0
 
tkeifferAuthor Commented:
Thanks guys.  That is everything I need.  One last minor issue I have is that the code is literally appending log entries without any carriage return.  Is there a way to add a carriage return after each log entry?
0
 
CEHJCommented:
writer.println() definitely should be appending newlines. Are you sure you're not viewing it in a web browser? (It will ignore them)
0
 
tkeifferAuthor Commented:
my bad.  This is kind of a tough question to pick multiple solutions for because a few of you deserve 500 points in my opinion.  Is there a way to do that?  Do you think this question should be in the knowledgebase?  
0
 
tkeifferAuthor Commented:
If someone is accessing this site after logging in, is there a way to capture their userID?
0
 
CEHJCommented:
Don't worry - it's actually against the terms of this site to award more than 500. Just split

>>If someone is accessing this site after logging in, is there a way to capture their userID?

Their user id should already have been captured elsewhere. You need to get it from there (session?) and add it to the log printout
0
 
rrzCommented:
In the login page, you could store it into session  object .  
session.setAttribute("userID",userID);
on other pages you could use the following line in a scriptlet.  
String userID = (String)session.getAttribute("userID");  
or elsewhere just  
${userID}
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 35
  • 21
  • 21
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now