Solved

HttpHeader

Posted on 2001-06-19
14
174 Views
Last Modified: 2013-11-24
Hi all,

I want to call a servlet by its URL and I want to be able to get the User Information from the URL i.e. what machine the user is using etc.

I want to be able to display all name-value pairs on the browser also.

An example URL to use would be:

http://servletDirectory/ServletName

Would anyone have any suggestion??
0
Comment
Question by:macca_26
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
14 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 6205767
The Snoop applet developed by Sun and included with Tomcat should help you with this...

Here is the source :

/* $Id: SnoopServlet.java,v 1.3 1999/10/15 21:31:49 duncan Exp $
 *
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 *
 * @author James Duncan Davidson <duncan@eng.sun.com>
 * @author Jason Hunter <jch@eng.sun.com>
 */

public class SnoopServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        PrintWriter out = response.getWriter();
        response.setContentType("text/plain");

        out.println("Snoop Servlet");
      out.println();
      out.println("Servlet init parameters:");
      Enumeration e = getInitParameterNames();
      while (e.hasMoreElements()) {
          String key = (String)e.nextElement();
          String value = getInitParameter(key);
          out.println("   " + key + " = " + value);
      }
      out.println();

      out.println("Context init parameters:");
      ServletContext context = getServletContext();
      Enumeration enum = context.getInitParameterNames();
      while (enum.hasMoreElements()) {
          String key = (String)enum.nextElement();
            Object value = context.getInitParameter(key);
            out.println("   " + key + " = " + value);
      }
      out.println();

      out.println("Context attributes:");
      enum = context.getAttributeNames();
      while (enum.hasMoreElements()) {
          String key = (String)enum.nextElement();
            Object value = context.getAttribute(key);
            out.println("   " + key + " = " + value);
      }
      out.println();
      
        out.println("Request attributes:");
        e = request.getAttributeNames();
        while (e.hasMoreElements()) {
            String key = (String)e.nextElement();
            Object value = request.getAttribute(key);
            out.println("   " + key + " = " + value);
        }
        out.println();
        out.println("Servlet Name: " + getServletName());
        out.println("Protocol: " + request.getProtocol());
        out.println("Scheme: " + request.getScheme());
        out.println("Server Name: " + request.getServerName());
        out.println("Server Port: " + request.getServerPort());
        out.println("Server Info: " + context.getServerInfo());
        out.println("Remote Addr: " + request.getRemoteAddr());
        out.println("Remote Host: " + request.getRemoteHost());
        out.println("Character Encoding: " + request.getCharacterEncoding());
        out.println("Content Length: " + request.getContentLength());
        out.println("Content Type: "+ request.getContentType());
        out.println("Locale: "+ request.getLocale());
        out.println("Default Response Buffer: "+ response.getBufferSize());
        out.println();
        out.println("Parameter names in this request:");
        e = request.getParameterNames();
        while (e.hasMoreElements()) {
            String key = (String)e.nextElement();
            String[] values = request.getParameterValues(key);
            out.print("   " + key + " = ");
            for(int i = 0; i < values.length; i++) {
                out.print(values[i] + " ");
            }
            out.println();
        }
        out.println();
        out.println("Headers in this request:");
        e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            String key = (String)e.nextElement();
            String value = request.getHeader(key);
            out.println("   " + key + ": " + value);
        }
        out.println();  
        out.println("Cookies in this request:");
        Cookie[] cookies = request.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            out.println("   " + cookie.getName() + " = " + cookie.getValue());
        }
        out.println();

        out.println("Request Is Secure: " + request.isSecure());
        out.println("Auth Type: " + request.getAuthType());
        out.println("HTTP Method: " + request.getMethod());
        out.println("Remote User: " + request.getRemoteUser());
        out.println("Request URI: " + request.getRequestURI());
        out.println("Context Path: " + request.getContextPath());
        out.println("Servlet Path: " + request.getServletPath());
        out.println("Path Info: " + request.getPathInfo());
      out.println("Path Trans: " + request.getPathTranslated());
        out.println("Query String: " + request.getQueryString());

        out.println();
        HttpSession session = request.getSession();
        out.println("Requested Session Id: " +
                    request.getRequestedSessionId());
        out.println("Current Session Id: " + session.getId());
      out.println("Session Created Time: " + session.getCreationTime());
        out.println("Session Last Accessed Time: " +
                    session.getLastAccessedTime());
        out.println("Session Max Inactive Interval Seconds: " +
                    session.getMaxInactiveInterval());
        out.println();
        out.println("Session values: ");
        Enumeration names = session.getAttributeNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            out.println("   " + name + " = " + session.getAttribute(name));
        }
    }
}

Basically, you compile this and put the class file into /tomcat/webapps/ROOT/WEB-INF/classes and then you can access it by http://localhost/servlet/SnoopServlet

Hope this helps you!!

Tim.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 6205773
Its in the "Headers in this Request" section...  eg:

request.getHeader("User-Agent");

gives you the browser of the person calling you...

Tim.
0
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6205778
Hi Macca

Please refer to the Samples of your server.
U will have snoop Servlet which gives the answer for your question

Cheers
Shyam
0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 35

Expert Comment

by:TimYates
ID: 6205781
Doh!  When I called that the "Snoop applet"  I of course meant servlet...  It's early in the morning ;-)

Good luck,

Tim.
0
 

Author Comment

by:macca_26
ID: 6205785
Hi Tim,

What is this last bit about: "Headers in this Request"

Should I have this in the SnoopServlet also??
0
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6205791
Hi Macca

One more thing

Look in for JSP tutorial in Java web site Tutorial U will a good tutorial on headers

U will learn a lot in that

Shyam
0
 

Author Comment

by:macca_26
ID: 6205814
Hey shyam,

Would you have the address for this java web site
0
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6205827
Hey Macca
I don't think that web site exist
I will send u the PDF Document of that
Please post ur email id or send me an email

I will send the doucment to u
I check up the site but it has been removed
don't worry i have it in document


Shyam
0
 
LVL 35

Expert Comment

by:TimYates
ID: 6205928
> What is this last bit about: "Headers in this Request"

No it is already in it...about half way down...

This is the bit of the code:


       out.println("Headers in this request:");
       e = request.getHeaderNames();
       while (e.hasMoreElements()) {
           String key = (String)e.nextElement();
           String value = request.getHeader(key);
           out.println("   " + key + ": " + value);
       }

It is this bit which is likely to give you the header name and value that you require!

EG:  This is the output for my browser when I point it at the snoop servlet:

=============================================

Snoop Servlet

Servlet init parameters:

Context init parameters:

Context attributes:
   javax.servlet.context.tempdir = /opt/jboss_tomcat/tomcat-3.2-b7/work/localhost_8080
   sun.servlet.workdir = /opt/jboss_tomcat/tomcat-3.2-b7/work/localhost_8080

Request attributes:

Servlet Name: SnoopServlet
Protocol: HTTP/1.1

Scheme: http
Server Name: localhost
Server Port: 80
Server Info: Tomcat Web Server/3.2 beta 7 (JSP 1.1; Servlet 2.2; Java 1.3.0; SunOS 5.8 sparc; java.vendor=Sun Microsystems Inc.)
Remote Addr: 195.224.xxx.xxx
Remote Host: 195.224.xxx.xxx
Character Encoding: null
Content Length: -1
Content Type: null
Locale: en_GB
Default Response Buffer: 8192

Parameter names in this request:

Headers in this request:
   User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)
   Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
   Host: localhost
   Accept-Encoding: gzip, deflate
   Accept-Language: en-gb
   Connection: Keep-Alive

Cookies in this request:

Request Is Secure: false
Auth Type: null
HTTP Method: GET
Remote User: null
Request URI: /servlet/SnoopServlet
Context Path:
Servlet Path: /servlet/SnoopServlet
Path Info: null
Path Trans: null
Query String: null

Requested Session Id: hp5xek5yu1
Current Session Id: hp5xek5yu1
Session Created Time: 992948460004
Session Last Accessed Time: 992948460004
Session Max Inactive Interval Seconds: 1800

Session values:

======================================================

(I commented out my IP address cos' I am a paraniod nut ;-) )

Good luck,

Let me know if you need more help...

Tim.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 6205933
PS:  As you can see from the output, I am running:

Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)


Tim.
0
 

Author Comment

by:macca_26
ID: 6205966
Hi Tim,

Just one more thing.

Ihave to log on to my workstation using and ID and password.
Is there any way that I would be able to pick these up from the servlet URL and output to the screen in order to identify who the actual user of the machine is??

thanks,

macca
0
 
LVL 35

Accepted Solution

by:
TimYates earned 100 total points
ID: 6206026
No...  Username and password are not sent in an HTTP request...  This is for security reasons....
0
 

Author Comment

by:macca_26
ID: 6206145
Thanks for your help Tim.

macca
0
 
LVL 35

Expert Comment

by:TimYates
ID: 6206164
No worries :-)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question