Servlet get full path of request with parameters [duplicate] - java

This question already has answers here:
HttpServletRequest to complete URL
(12 answers)
Closed 9 months ago.
I'm using a Servlet accessible via POST request, but I've seen that parameters in POST, can be set either thanks to header parameters but also in a GET format way (/MyServlet?param1=123&param2=456) and I would need to detect it on my Servlet.
I tried to retrieve the request by using
request.getRequestURI()
but I cannot see the parameters in that case...
Do you know how can I retrieve the full path of the request in a GET parameter way ?

getParameter() returns http request parameters. Those passed from the client to the server. For example http://example.com/servlet?parameter=1. Can only return String.

Related

Get the currently used protocol name from HttpServletRequest? [duplicate]

This question already has answers here:
Java: String representation of just the host, scheme, possibly port from servlet request
(6 answers)
Closed 1 year ago.
I'm constructing a new URL in a Spring MVC controller, to be passed back to the client. Currently I'm trying this:
// httpRequest is the current HttpServletRequest
new URL(httpRequest.getProtocol(),
httpRequest.getServerName(),
httpRequest.getServerPort(),
httpRequest.getContextPath().concat("/foo/bar.html"));
Problem is that httpRequest.getProtocol() gives me "HTTP/1.1" instead of just "HTTP". I can trim it but wondered if there was a more elegant way.
The protocol is HTTP/1.1, since it is a specific version of HTTP. The scheme as given by ServletRequest#getSchemeitself is http:
new URL(httpRequest.getScheme(),
httpRequest.getServerName(),
httpRequest.getServerPort(),
httpRequest.getContextPath().concat("/foo/bar.html"));
In 2020 I'll suggest you use ServletUriComponentsBuilder and it's static methods, such as ServletUriComponentsBuilder#fromCurrentRequest which helps you build your URL using the previous Request.
Example:
URL url = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/foo/bar.html")
.encode() // To encode your url... always usefull
.build()
.toUri()
.toURL()
Whatsmore, if you wanna redirect on the same app, please just return "redirect:/foo/bar.html" with status 302 and spring boot MVC will transform it into a normal redirection.
Example:
#ResponseBody
#GetMapping("/some/endpoint")
#ResponseStatus(HttpStatus.FOUND)
public ModelAndView redirectToFoo() {
return new ModelAndView("redirect:/foo/bar.html");
}

Ajax: Sending multiple parameters in HTTP POSTrequest [duplicate]

This question already has answers here:
How to send simple http post request with post parameters in java
(5 answers)
Closed 7 years ago.
How to send multiple parametrs in HTTP POST request?
I am sending POST request through Ajax call to servlet
I tried below two ways but neither worked
Approach 1:
httpRequest.open("POST", "http://localhost/Servlet/ps" , true);
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
.
.
.
httpRequest.send("msg="+data & "TC="+TC);
At Servlet:
String data = req.getParameter("msg"); // this prints value fine
String TestCaseNo = req.getParameter("TC"); // this prints null
Approach 2:
httpRequest.open("POST", "http://localhost/Servlet/ps" , true);
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
.
.
.
httpRequest.send("msg="+data + "TC="+TC);
ANSWER:This is how it worked for me
var value="msg="+data+"&TC="+TC;
http-Request.send(value);
I'm unfamiliar with AJAX but this seems more like a setup for GET request httpRequest.send("msg="+data & "TC="+TC).
Try writing parameters to your connection output stream and then in doPost method get it from req.getInputStream(). This will definitely work, but idk if it's exactly what your asking for.

for sendredirect which method gets called doGet or doPost() [duplicate]

This question already has answers here:
doGet and doPost in Servlets
(5 answers)
Closed 6 years ago.
I am new to servlets.
My Q is if I use for response.sendredirect()
which method gets called doGet or doPost()?
I know that in jsp to servlet get or post method will get called according to method type.
But if it is servlet to servlet request using response.sendRedirect() which method will get called?
how servlet engine decides which method to call?
Thanks in avdance.
redirect is always use get method,
redirection means a new request..
when we give send redirect actually happening is a new request from the user..
and it is always get..
since it is a new request we cant access the old request parameters
response.sendRedirect is always a GET
A sendRedirect() is always a 2 step process in which the server sends a URL Location and a status code of 301 to client browser.
The client browser then GET's the URL and then goes to that url location.(You can see this url in the address bar).
Remember a request to a Http or a URL link is always a Get request whether the URL is to a servlet within application or to external location.
Refer
http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServletResponse.html#sendRedirect%28java.lang.String%29

how does doPost request works? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Servlets: doGet and doPost
I know doGet() request is appended to the request URL in a query string.But I don't know the concept of doPost() request.how does doPost request posting information to the server.
Please Guide me to get the working concept of doPost request...
Post requests are used usually for sending data to Server, and get request for reading data from server. In Post request data is sent in http request body, so data size can be very large compared to Get. If a browser fires an POST request (usually a form submit) doPost of the mapping Servlet will be called. There is another overloaded method (service()) which is called for both GET and POST
In doPost() the data is not appended in the URL.
It can handle large amount of data compared to the doGet() method.
Filling of form and submitting is done through doPost(), it's secure to use doPost() during submission of the username and password.
There is also differnce in the doGet() and doPost() header and body structure.
The main conceptual difference GET and POST is that, GET is used for getting the data from the server, and POST is used for updating the data to the server.
In general POST has the following properties:
The data is x-www-form-urlencoded . Which means, the request parameters are sent as request body. And the server has to parse the request body for parameters.
By default, when no content-length header is present, the default value for GET is 0 whereas for POST it is till end of stream.
GET is Idempotent whereas POST is Non-Idempotent. i.e, Proxies on failures for GET they retry. But, for POST they do not retry.
doGet() can be used when client request doesn't intend to change stored data.

Send Http Post Message using a stream in Java [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to use java.net.URLConnection to fire and handle HTTP requests?
I have to send an http post message to a server.
This message must have an header and a body and to send it i must use a Stream.
Both the header and the body must be an array of Byte.
How can I do this?
Thank you
From the server you can get header information separately from HttpServletRequest.getHeaders(). Data should be read in the form of parts (multipart/form-data) using input stream that can be opened from Request object. Hope this help.
Ref:
http://www.servlets.com/cos/javadoc/com/oreilly/servlet/MultipartRequest.html
http://www.jguru.com/faq/view.jsp?EID=1045507

Categories

Resources