Why setParameter() is not available for ServletRequest? - java

I have read about Servlet's in many tutorials. The Servlet parameters has getParameter() method. As the parameter has return type as only String. Why the ServletRequest do not have setParameter() method ?
I have read about that only attributes in Servlet's can be changed, and the parameters can not. Can anyone explain the basic concept why there is no provision for updating the parameter in request with method like setParameter()?

As per ServletRequest documentation, it is mentioned that :
Defines an object to provide client request information to a servlet.
That means ServletRequest object is used to transfer client side information to server methods. For ex. text field values from jsp page to servlet's doGet or doPost method.

ServletRequest Object is an client request to a Servlet, and ServletResponse object is the response sent to the client so always you get the required information from the request and you set the information in the response............!

Application developers use parameter for getting information from client, where as attributes are used by application developer for overall internal management purposes. You can not change (setParameter()) the values given to you by clients in parameters; you can simply get those values using getParameter()

Related

How to send values from one jsp page to other but redirect to some other page?

I want to send values from jsp1.jsp to jsp2.jsp but redirect jsp1.jsp to jsp3.jsp . I used the following code in servlet for jsp1
response.sendRedirect("welcome1.jsp");
request.setAttribute("usern",user);
RequestDispatcher rd = request.getRequestDispatcher("afterlogin.jsp");
rd.forward(request,response);
but it keeps on giving this error "org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP".
you can use the possiblites of sessions in those case so you can access the parameter in jsp2 and redirect the jsp 1 to 3
https://www.javatpoint.com/servlet-http-session-login-and-logout-example
http://java.candidjava.com/tutorial/Servlet-Jsp-HttpSession-Login-logout-example.htm
You can use a HttpSession object as a user session, or use the ServletContext object for sharing global application information. Then use methods getAttribute (String attb) and setAttribute (String attb, String value) for sharing information within JSPs. The issue when you use a request is that the domain of the request is constraining you to use this information only when you receive this request.
You can also use JavaBeans to share information within JSPs
EDIT: Have you included your Java code inside a scriptlet? Using <% your code here %>
when you are using the sendRedirect function it will immediately redirect to that page it will not read next line code.
So according to your code your compiler is smart enough to check it so it is giving you an error.
please see below
1) SendRedirect ():
This method is declared in HttpServletResponse Interface.
Signature: void sendRedirect(String url)
1)In case of sendRedirect request is transfer to another resource to different domain or different server for further processing.
2)When you use SendRedirect container transfers the request to client or browser so URL given inside the sendRedirect method is visible as a new request to the client.
3)In case of SendRedirect call old request and response object is lost because it’s treated as new request by the browser.
4)SendRedirect is slower because one extra round trip is required because completely new request is created and old request object is lost.Two browser request required.
5)But in sendRedirect if we want to use we have to store the data in session or pass along with the URL.
2) Forward():
This method is declared in RequestDispatcher Interface.
Signature: forward(ServletRequest request, ServletResponse response)
1)When we use forward method request is transfer to other resource within the same server for further processing.
2)In case of forward Web container handle all process internally and client or browser is not involved.
3)When forward is called on requestdispather object we pass request and response object so our old request object is present on new resource which is going to process our request
4)Using forward () method is faster then send redirect.
5)When we redirect using forward and we want to use same data in new resource we can use request. setAttribute() as we have request object available.
more in : https://www.javatpoint.com/q/3577/difference-between-requestdispatcher-and-sendredirect

Take out parameter from url

I have a very long url with numbers of parameter like
http://localhost:8080/BUUK/dbcc?dssin=9371062001&roundid=JS&KIPL=02&PLATFORM=1&FREQUENCY=2&DRBEARER=1&BUYTYPE=1&EUP=12&TID=72123456435653654&SHORTCODE=54300&ADCODE=234rfdfsf&Buytag=3&Checkpoint=5,6,7&CHARGEMODEL=complete&restbalance=1
I want retrieve all the parameter from this url.
I was wondering if i can use request.getParamter("restbalance");
I will provide more info if required.
Thanks
If you're dealing with HttpServletRequest you can use
String restbalance = request.getParameter("restbalance");
or...to get all the parameteres, you can do:
String[] params = request.getParameterValues();
Here's the javadoc for the HttpServletRequest class, with all the available methods listed.
For each request your web server more precisely your web container creates a two object request and response.
HttpServletRequest and HttpServletResponse
The servletcontainer is attached to a webserver which listens on HTTP requests on a certain port number, which is usually 80. When a client (user with a webbrowser) sends a HTTP request, the servletcontainer will create new HttpServletRequest and HttpServletResponse objects and pass it through the methods of the already-created Filter and Servlet instances whose url-pattern matches the request URL, all in the same thread.
The request object provides access to all information of the HTTP request, such as the request headers and the request body. The response object provides facility to control and send the HTTP response the way you want, such as setting headers and the body (usually with HTML content from a JSP file). When the HTTP response is committed and finished, then both the request and response objects will be trashed.
request.getParameter("request_param"); will give you request_param value.
So there is nothing to get surprise accessing request parameter from request object
Yes you can use request.getParameter where request is the object of HttpServletRequest.
From the javadocs
getParameter
java.lang.String getParameter(java.lang.String name) Returns the value
of a request parameter as a String, or null if the parameter does not
exist. Request parameters are extra information sent with the request.
For HTTP servlets, parameters are contained in the query string or
posted form data. You should only use this method when you are sure
the parameter has only one value. If the parameter might have more
than one value, use getParameterValues(java.lang.String).
If you use this method with a multivalued parameter, the value
returned is equal to the first value in the array returned by
getParameterValues.
If the parameter data was sent in the request body, such as occurs
with an HTTP POST request, then reading the body directly via getInputStream() or getReader() can interfere with the execution of this method.
Try getParameterMap()
Map params = request.getParameterMap();
Iterator i = params.keySet().iterator();
while ( i.hasNext() )
{
String key = (String) i.next();
String value = ((String[]) params.get( key ))[ 0 ];
}
Well , request.getparameter() will work fine only if the request hits your Servlet from where you want to get hold of the request parameters . Please go through the documentation ServletRequest interface for all the relevant methods for your purpose.
getParameter();
getParameterNames();
getParameterValues();
getParameterMap();
You can also use HttpServletRequest#getQueryString() for custom parsing.
For normal Java code , you can parse the string returned by URL.getQuery() yourself to extract the data .

When is doPut() called in a servlet?

Hi I was just curious when is the doPut() method in a servlet called. I know that if the form on a jsp/html page has a "post" method then the doPost() is called otherwise if it has a "GET" then the doGet() is called.When is the doPut() called ??
When an HTTP PUT request is received, naturally.
Can a page do a PUT request by code?
The only valid method attribute values of a <form> are get and post, according to the HTML5 spec. I assume that's what you're asking.
The doPut() method handles requests send by using the HTTP PUT method. The PUT method allows a client to store information on the server. For an example, you can use it to post an image file to the server. As the above answer says, goGet() and doPost() are in use, mostly. In my case, I use only these two, and I am getting only get requests, so I simply transfer the get request to doPost() and do my job easily.
if you want to send some confidential values in url via form you must use the post method, If you will use the get method for the form like login the values parameters like userid and password will be visible in url and anyone can hack that thing. So better to use post method in forms. By default it will call get method.
in get the url is like http://url?method=methodname&userid=123&password=123
so if you use post method the url will be like this http://url/methodname.do

how to over ride request object in ServletRequestWrapper?

I want to over ride the default getParameter() method of ServletRequestWrapper with getParameter() method of SecurityRequestWrapper.
For example, if I am using a simple jsp form to take the name of a person,
String name = request.getParameter("firstName");
I want the above getParameter() method to be from the SecurityRequestWrapper class. I am not able to understand how the request object is over riden since the getParameter method is mostly called on it by default in any jsp form.
I understand that the SecurityRequestWrapper you're talking about already implements HttpServletRequestWrapper? If so, then just create a Filter which is mapped on an url-pattern of *.jsp (or whatever you'd like to invoke this Filter for) and does basically the following in the doFilter() method.
chain.doFilter(new SecurityRequestWrapper((HttpServletRequest) request, response));
I might be wrong, but I do not think this is possible. Because request and response objects are created by the container and passed onto the servlet's process method. The very reason these objects are created by the container, because they want to flush the output and would like to control that. I will be interested to know however if it is possible to pass our own request / response objects.

How the attribute field of a HttpServletRequest maps to a raw HTTP request?

In Java, the attribute field of a HttpServletRequest object can be retrieved using the getAttribute method:
String myAttribute = request.getAttribute("[parameter name]");
Where the HttpServletRequest attribute data is stored in a raw HTTP request? Is it in the body of the request?
For example, I'm trying to create a raw GET HTTP request that will be sent to my servlet using some client program. My servlet.doGet() method would be something like this:
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
String myAttribute = request.getAttribute("my.username");
...
}
Where should I put the 'my.username' data in the raw HTTP request so that the 'myAttribute' String receives the value "John Doe" after the attribution?
Just to be clear as I think #Jon's answer doesn't make it perfectly clear. The values for getAttribute and setAttribute on HttpServletRequest are not present on what is actually sent over the wire, they are server side only.
// only visible in this request and on the server
request.getAttribute("myAttribute");
// value of the User-Agent header sent by the client
request.getHeader("User-Agent");
// value of param1 either from the query string or form post body
request.getParameter("param1");
To add to #gid's answer, attributes are not present in any way in the HTTP request as it travels over the wire. They are created (by your code) when processing the request. A very common use is to have a server set (aka create) some attributes and then forward to a JSP that will make use of those attributes. That is, an HTTP request arrives and is sent to a Servlet. The Servlet attaches some attributes. Additional server-side processing is done, eventually sending the page to a JSP, where the attributes are used. The response is generated in the JSP. The HTTP request and the HTTP response do not contain any attributes. Attributes are 100% purely server-side information.
When a single given HTTP request has completed, the attributes become available for garbage collection (unless they are persisted in some other location, such as a session). Attributes are only associated with a single request object.
I think what he is really asking is "how do I get parameteres into my program", not attributes. If that is the question, then send parameters in the GET request as part of the URL (after a question mark, http://myhost.com/myapp?name=joe&age=26) then retrieve them using request.getParameter("name") and request.getParameter("age"), or whatever you need.

Categories

Resources