Am I Running Under HTTP or HTTPS?

M

MattC

I have a servlet application. Based on a user request the application
will generate an email that contains a URL to one of the applications
screens. This application must run in a variety of environments.

My question is how do I dynamically determine if the URL should use
HTTP or HTTPS? The only solution I can think of is to do something like
this:

String protocol = httpServletRequest.getProtocol();

if (protocol.indexOf("HTTPS") > -1) {
// protocol is HTTPS
} else {
// protocol is HTTP
}

Will this work? Is this the best way to accomplish this?

Thanks,
 
R

Real Gagnon

My question is how do I dynamically determine if the URL should use
HTTP or HTTPS? The only solution I can think of is to do something like
this:

String protocol = httpServletRequest.getProtocol();

if (protocol.indexOf("HTTPS") > -1) {
// protocol is HTTPS
} else {
// protocol is HTTP
}

Use the servlet request's isSecure() or getAuthType() methods.

Or you can check these HTTP headers : CERT_KEYSIZE , CERT_KEYSIZE,
HTTPS_KEYSIZE

This will check if the current connection is secured or not.

To check if the client can handle a secured connection, check this HowTo
at http://www.rgagnon.com/jsdetails/js-0088.html

Bye.
 
R

Roedy Green

String protocol = httpServletRequest.getProtocol();

if (protocol.indexOf("HTTPS") > -1) {
// protocol is HTTPS
} else {
// protocol is HTTP
}

I think is this more likely to work:

if ( protocol.equalsIgnoreCase( "https" ) )

Possibly the protocol is always presented as lower case, in which case
you could say

if ( protocol.equals( "https" ) )
 
M

MattC

Ah yes, isSecure() that's the ticket.

I had a sence that there was a cleaner solution I just wasn't sure what
is was. - Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top