Set request attribute in varnish script - java

Is it possible from varnish vcl script to set an attribute on the request and get it in the backend by java Servlet getAttribute() method? http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getAttribute(java.lang.String)

Request attributes are a mechanism internal to the Servlet framework, so it is not possible to set them from the outside. It would however be possible to set a Request parameter (in either query String or request body, depending on the request type), that would then be available through HttpServletRequest.getParameter(name)

Related

Managing session and request attributes in Servlet

I have a very simple JSP page where it has one search box and based off the input, in the search box, it will return a response with a submit button to get the following response.
I noticed that whenever I use request.getattribute("foo") in my servlet to retrieve some request it returns null due to the request ending so I looked at the answers on here and started using session.getattribute("foo") instead. However, now I am stuck having session variables responses being set and it is causing my view to have old session data that isn't suppose to be there so now I have to use session.removeAttribute("foo"), whenever, I don't want that particular response data to be shown.
Is there a better way to go about managing this instead of having to use session.getattribute("foo"), session.removeAttribute("foo") and session.setattribute("foo")?
You should work with request.getSession()
Returns the current session associated with this request, or if the request does not have a session, creates one.
Set an attribute:
request.getSession().setAttribute("foo")
And get attribute using:
request.getSession().getAttribute("foo")
It will be used in the context of the request and not effect other requests, so you don't need to remove attribute.
Read more in Servlets tutorial
Servlets provide an outstanding technical solution: the HttpSession API. This is a high-level interface that allows the server to "remember" a set of information relevant to a particular user's on-going transaction, so that it can retrieve this information to handle any future requests from the same user.
You can go for request.getparameter("foo") or request.setparameter("foo", obj)
This can be used for every request, and it will not add to your session variables and basically will not make your "session object heavy".
Java doc:
Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

Forward to a servlet and set attribute

I'm working with servlets in java; I'm trying to forward from one servlet to another servlet.
I also want to pass an attribute to that other servlet.
When i want to forward to a JSP, it works fine. i do
request.setAttribute("attrName", attribute)
request.getRequestDispatcher("forward.jsp").forward(request, response);
But When I do the same with a servlet:
request.setAttribute("attrName", attribute)
request.getRequestDispatcher("TheServlet").forward(request, response);
My server freaks out and I get the following error:
javax.servlet.ServletRequestWrapper.isAsyncStarted(ServletRequestWrapper.java:395)
I know I can use the following line to redirect to a servlet:
response.sendRedirect("TheServlet");
But for some reason the set Attribute doesn't work when I redirect instead of forward.
redirect is a HTTP response sent to the browser requesting it to submit a new request to the specified URL. Since it results in issuing an entirely new request previous request attributes you set wont be available in the new request.
In terms of forwarding to a servlet, did you check your web.xml configuration. Is it setup so that the forwarded servlet is seeing forwarded requests ?
You could save the attribute to the session in the first servlet and access it from the second.
Use http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getSession()
Also you could pass the attribute value in the URL query string in the redirect. So your redirect URL would look like 'myRedirectUrl?attributeName=attributeValue'
Also additionally try using 'include' method rather than 'forward'.

Remove invalid request parameter from URL with servlet filter

I have a web application written in Java which uses Struts 1.0. Sometimes when a URL is fired, I can see that it one of the request parameters does not have any name and value like the following ...
http://www.aaa.com/test.do?a=1&b=2&=&d=4&e=5
As can be seen, there is a '&=' which is essentially a parameter with no name and value. I'd like to remove this part from the URL before sending the request to the server. How can I achieve this? Should I use a filter or is there an easier way?

When using SolrJ can I point it at a request handler?

I have created a request handler in Solr that uses dismax and limits my query to certain fields and adds boosts for relevancy on the "title" field.
This all works fine when I go directly to Solr using an http request in a browser. However my question is whether I can use the request handler if I am accessing Solr using SolrJ?
It would be better if I could control boosts and filters and so on in the request handler rather than having to make code changes but I can't see how to specify a request handler in the API.
Any ideas?
In the class SolrQuery, there is a method setRequestHandler that allows you to do that. You pass the name of the request handler as defined in solrconfig.xml (probably 'dismax').

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