I wrote a servlet in Java and I would like to know if the request to that servlet was executed using HTTP or HTTPS.
I thought I can use request.getProtocol()
but it returns HTTP/1.1 on both methods.
Any ideas?
The doPost() method in servlets is used to process the HTTP POST requests. It is used to submit the data from the browser to the server for processing. The data submitted with POST method type is sent in the message body so it is secure and cannot be seen in the URL.
request – an HttpServletRequest object that contains the request the client has made of the servlet. response – an HttpServletResponse object that contains the response the servlet sends to the client.
Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request. Servlets are platform-independent because they are written in Java.
The doGet() method is used for getting the information from server while the doPost() method is used for sending information to the server.
HttpSerlvetRequest.isSecure() is the answer. The ServletContainer is responsible for returning true in the following cases:
The Container should also make this request attributes available when the request is received on https:
You can't reliably depend on port numbers.
But you can depend on the scheme:
Use: request.getScheme() to see if it is https
.
If it is then it is secure connection.
I believe this should work regardless of Tomcat version
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With