Solved

HttpHeader

Posted on 2001-06-19
14
165 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
  • 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now