[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

HttpHeader

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
macca_26
Asked:
macca_26
  • 7
  • 4
  • 3
1 Solution
 
TimYatesCommented:
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
 
TimYatesCommented:
Its in the "Headers in this Request" section...  eg:

request.getHeader("User-Agent");

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

Tim.
0
 
shyamkumarreddyCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Good luck,

Tim.
0
 
macca_26Author Commented:
Hi Tim,

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

Should I have this in the SnoopServlet also??
0
 
shyamkumarreddyCommented:
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
 
macca_26Author Commented:
Hey shyam,

Would you have the address for this java web site
0
 
shyamkumarreddyCommented:
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
 
TimYatesCommented:
> 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
 
TimYatesCommented:
PS:  As you can see from the output, I am running:

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


Tim.
0
 
macca_26Author Commented:
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
 
TimYatesCommented:
No...  Username and password are not sent in an HTTP request...  This is for security reasons....
0
 
macca_26Author Commented:
Thanks for your help Tim.

macca
0
 
TimYatesCommented:
No worries :-)
0

Featured Post

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.

  • 7
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now