setAttribute() and getParameter(), is this right? - servlets

I'm working on an servlet based project where I found that in a place, a value is set using request.setAttribute("") and in another place, that value is retrieved using request.getParameter(""). Is this right?
I know the difference between the getParameter and getAttribute. But the retrieved value intermittently becomes null.

Only those values can be retrieved using request.getAttribute which are set using request.setAttribute. And query string in GET request or request prameters in POST request can be retrieved using request.getParameter. There is no method as request.setParameter in Servlet API. Now coming to the intermittent behavior, check the URL/AJAX request on each request to server and see exactly when it has the property and its value set which you are trying to retrieve using getParameter method. Hope this clarify your issue.

Related

Any one please describe the use of NO_ENTITY_BODY

I need to know the exact use of NO_ENTITY_BODY.Explanation with example could be more useful.
For GET and DELETE API calls, this property is set in the message context by default. This is used for internal purposes.
In case you want to send a response from ESB to such a request, you need to remove this property. Otherwise, the response body of the message is also ignored and an empty response is sent back to the client.

An error occurred with Google Calendar API insufficientPermissions

I refer to the document https://developers.google.com/calendar/v3/reference/events/insert to call the insert method, but there is an error insufficientPermissions. I want to know what the general situation is, and I can use gapi.client.calendar.events.list() method to get the result normally.
You are probably using one of the Calendar API Quickstarts which is why calls to events.list works but events.insert doesn't. You need to change the scope to https://www.googleapis.com/auth/calendar because that is required for events.insert.
Also don't forget to delete the previously saved credentials when adding the new scope.

Application Insights Operation.Id from an HTTP header

My project uses ApplicationInsightsHttpModule which initializes Operation.Id from Microsoft.ApplicationInsights.RequestTelemetry HTTP value set by a client UI application. Now I want my API to be consumed by a third party which will provide X-Operation-Id HTTP header to correlate our activities. How do I make Application Insights to initialize Operation.Id from that header if it's present in a request?
This says that the standard context is managed automatically by AI so I need a code sample that shows how to properly initialize Operation.Id with a custom value. The following code isn't working, the header value is ignored:
var operationInitializer = TelemetryConfiguration.Active.TelemetryInitializers.OfType<Microsoft.ApplicationInsights.Web.OperationCorrelationTelemetryInitializer>().FirstOrDefault();
if (operationInitializer != null)
{
operationInitializer.RootOperationIdHeaderName = "X-Operation-Id";
}
The code that you included in your question should be working at first glance: you are looking for correct initializer and overriding correct property. However there could be multiple reasons why it is not working as expected, and debugging this is hard, because it would require you to actually step into Application Insights code (if you do decide, the sources for every release are archived on GH).
My recommendation in your case would be to take a different approach - instead of trying to override this property, create and register your own telemetry initializer to set Operation.ID from your header. If you set this property, OperationCorrelationTelemetryInitializer will ignore it and not override. This way you confirm that your headers can be read correctly, and you can easily set breakpoint in Initialize method to debug.
At the moment the question was asked AI SDK had version 2.2. Recently I've upgraded to the latest version 2.4 and it's working now as intended. So, if you pass say 'HELLO' in X-Operation-Id HTTP header (you still need that code in the question in Application_Start()) and then ask for a telemetry in Application_BeginRequest() (or wherever HttpContext is available):
RequestTelemetry telemetry = HttpContext.Current.GetRequestTelemetry();
telemetry.Id will contain something like |HELLO.d0b9d5b7_.
That hexadecimal suffix increases if you pass 'HELLO' in the next request so Id is always unique. I don't know if that can be overridden though.

Spring MVC 3.1 - Model Attribute lost

I have a quick question on scope of ModelAttributes.
Dev. Env: Spring MVC 3.1/Java 6/JSP w/JSTL for Views
In my controller, I add an attribute to the model via
model.addAttribute(“appForResubmission”, appForResubmission);
In the JSP(served out in response to a GET request) I read it’s contents as:
${appForResubmission.appId}
— works fine and the data is shown on JSP as expected.
Upon submission of the JSP, in the same controller in a different method(in response to a PUT request), I try to read the attributes from the Model for any changes and I am doing this as
#ModelAttribute(“appForResubmission”) Application app
in the method signature.
However, all I get is a new Application object when I try to interrogate the object for data. Spring’s documentation says this kind of instantiation of a new object happens when the requested attribute does not exist in the Model.
What would cause the attribute to be lost? Any ideas? I am suspecting it is a scope issue someplace but I am not sure where the problem could be.
Any pointers you could provide is greatly appreciated?
Thank you,
M. Reddy
The scope of a modelattribute is the request, internally it is just equivalent to HttpSerletRequest.setAttribute("model", model).
If you want the model to be available in a different controller you probably have two options, one is to reconstruct it, based on what you submit to the controller or using your persistent source. The second option is for specific model attributes to be added to the session using #SessionAttribute({'modelname'}), but just be careful that you have to call SessionStatus.complete to remove the model added to the session later.

Is there a way to return a value in the Response?

The problem is I redirect to a sub page from the main page by passing a few values in Request.
I need to get back the value I got from manipulating values I sent in request and I don't need to store value in Session.
You can use the PreviousPage property, depending on how you got to the new page.
There are a number of things you can do:
Page.Request["ID_Of_Control_You_Are_Interested_In"] will get you a value that you need.
If you Server.Transfer to the page you can call Context.Handler, cast it to the page you come from and traverse the controls collection to get your value.... (similar to PreviousPage property)
You can also inspect the Query string parameters sent in the request too:
Page.Request.QueryString("Param_To_Inspect")
you can return your values as JSON as the response and then using a jquery to get the data
You have three options: Query String Parameters, Cookies and Session State
Query String Parameters: http://www.aspnet101.com/2007/11/using-the-querystringparameter/
This is a good way to pass data between pages. It's the most commonly used and is limited to the max length of the url.
Cookies : http://msdn.microsoft.com/en-us/library/ms178194.aspx
This is a good method. It also has a size limitation. The advantage of cookies is you can persist data that can span visits to your site.
Session State : http://msdn.microsoft.com/en-us/library/ms178581.aspx
This an ok method, I'd use the two methods above before I would use session state. In my experience you can get into trouble very fast with Session state.
To answer your question: You can take the value from the request and load it into a TextBox. Then on the web page, the user edits the value and clicks a save button, which posts the changed value back to the server.
Or you could use javascript to post the value back to the server.

Resources