I am working on a web application, in which I am reading some configuration data stored in xml file from a Servlet. I want the data read from this servlet available to all the requests coming to this servlet. So inside the init() method of this servlet I am initializing global variable , which will have content read from this xml file so that all the request coming to this servlet will have this data available and don't have to read from the xml file again and again.
My first question is, is this method is better way to share data among request coming only to this servlet. I don't want to share it across all the servlets.
The rule here is just locality: if that data is only used by one single servlet, it should be local to the servlet, meaning as per you proposal:
a member of the servlet class
loaded in the init method of the servlet
But (as your initial post asked), if that data can be updated by another servlet of the same web application, it make sense to move it one step up to a SerletContext attribute. That way:
it can still be initially loaded in the init method of the servlet
it can be changed at any time by any other component of the web application that knows the name of the attribute.
You can imagine plenty of other ways, by using for example custom events and using an observable pattern, or by mapping the servlet to a special (and private) URL that would signal that the xml file must be reloaded, but IMHO, a servlet context attribute is a clean and simple way to allow different servlet to exchange informations.
But beware, you will still need an extra synchronization mechanisme if your web application is intended to be served by more than one single server
Related
I have a Java-EE application that works with JSF (ManagedBean, ManagedProperty, ect ...) and Spring framework. I need to be able to retrieve data via a javascript form sent from an external website. I have opened the rights to authorize the CORS (Cross Origin or Cross Domain).
I would like to know what is the best way to grab an external form with JSF so that it is processed by my ManagedBean.
To make my question more explicit I made a diagram
==============
EDIT
The application works with JSF, I'm looking for a way to retrieve data (from a Javascript form on an external site) in a ManagedBean under JSF. I tried to retrieve this data by creating a Java-EE standard servlet and using the doPost ... methods of HttpServlet. But this solution does not work (this was the subject of my previous question on S.O). Several people told me that in a web application do not mix Java-EE standard and JSF, it is either Servlet or JSF. I made a diagram (above) explaining quickly what I am trying to do.
To recap: I would like to retrieve data from an external website (via a Javascript form) in the ManagedBean of my application.
==============
I've already tried with a standard Java-EE servlet but it's not the right way. Indeed, with a standard servlet I can recover the data from the form but I can not access the ManagedBean. I must therefore have abandoned this hypothesis.
I did not find a similar question about Stackoverflow, If necessary I can give more indications.
Thank you for your help.
There is a requirement in the webapp I am developing (not changeable unfortunately ) that states that I have to send via post certain parameters to a servlet from a JSF managed bean. That servlet then forwards to a page where it shows some of the parameters sent via POST in a form for the client to see.
That servlet is part of an external application, thus I cannot use RequestDispatche.
I have tried Apache Http client but i get to a point that the post URL responds with the content of the page (i can actually see the HTML in my logs), what i would like to do is forward to the page not receive it as answer in my code.
If this makes no sense let me know :).
EDIT:
what i need to do is:
JSF BEAN->POST data to an external servlet -> Follow servlet redirect/forward to an external page
One way I'm thinking of doing this is forwarding to an internal facelets or JSP page that builds a hidden form with all required parameters and automatically submits it to the post Servlet
So after trying to find other ways to do it, I have decided to go this way:
Create an internal page, with a hidden form that will be automatically committed with a small javascript.
Get all required data in the bean, navigate to page, fill hidden fields with the required info, post to external servlet.
I should also say that a better way to do this kind of interaction would be:
Post data to servlet (for example using apache http client)
manage the servlet response (for example 200 OK)
navigate via GET (using a redirect) to the servlet by passing some id as a
get parameter through which the data can be shown in the page
This would require the servlet to accept POST and get requests, and also to have a way to get the data sent via post when it's called via get. This was not the case for us.
I have a custom search component which searches for some parameter(s) from a dropdown [myParam] and displays the search results in another page. I currently use the default (GET) form
<form id="searchForm" action="/content/myWeb/searchResult.html" method="get" target="_blank">
In the result page, a component picks up the request params and processes the search.
I need to make it a POST submission so that the search parameters are NOT visible in the URL. But if I make it a method="Post" in the form above, I get this error:
Status
500
Message
javax.jcr.nodetype.ConstraintViolationException: no matching property definition found for {}myParam
Location /content/myWeb/searchResult
Parent Location /content/myWeb
Path
/path/to/search/page
That exception is the incidental way that Sling tells you that the servlet to which you are attempting to POST can not be found. What happens, in this case, is that Sling defaults to the SlingDefaultPostServlet, which attempts to to POST properties (represented by your form values) to the node /content/myWeb/searchResult. There's no way for Sling to say "I can't find a servlet that's registered to your request", so it just falls back to it's default behavior.
I'm assuming /content/myWeb/searchResult is a cq:Page node type. That node type is very restrictive, which is why it tells you that you cannot add properties that correspond to your form values.
This worked before, because your GET request to /content/myWeb/searchResult.html was able to resolve and execute. All GET requests to a page node can be served up by the system, inherently.
Now, since you are trying to do a POST, you need to create and register a new servlet that can handle this POST request. To do this, you'll need to create a SlingPostServlet and register it to your specific path (not recommended) or a specific selector/extension combination (recommended). That servlet should process the request parameters and respond with an HTML document.
A caveat...
What I just described will help you technically build what you are asking. That said, I don't agree with the premise that you should "make it a POST to hide the request parameters." The reason this is so much extra work, is because you are circumventing the principles of REST, which Sling is theoretically built to support. Your URL (via request path and parameters) should be communicating "I want the page at /content/myWeb/searchResult, given the criteria param1=x, param2=y, and so on". The GET with request params is an appropriately RESTful request.
I suggest you rethink what you're trying to do. Building a more complex solution around RESTful principles is not a good practice.
Just as a sidenote, you can always check if a given URL is bound to a servlet via the sling servlet resolver. Reachable via the OSGI-console or via URL:
http://localhost:4502/system/console/servletresolver
This can at least help you find closure on, if the servlet is registered to the given URL.
You can create a POST.jsp for your page, which could handle the POST request.
It is not restful to make get like request with POST, but sometimes it can be useful. Also With POST, dispatcher won't cache your request.
I have created a REST service for uploading files. Because of various requirements, I cannot integrate that into the JSF lifecycle. Amongst others:
JQuery handles the POST and expects a JSON response which I don't feel good doing from a bean method.
Fileupload plugin does multiple concurrent file POST s, which AFAIK is not possible (by default) JSF behaviour as event's will be queued.
I have a few options in mind, but I would like to be as intrusive as possible. The ones I have examined so far are:
Using JQuery, populate a hidden field with the location of the files and process them in the ActionListener
Same as above, but also call the ActionListener from JQuery
Store data in the session (since I can access it, from my REST service)
Use an #EJB queue service to pass the data to the bean (#Inject' service to bothbeanandREST` service)
Alternatively, is there a way to actually fetch by #ViewScoped bean given that I have the viewstate form field?
I'm new to JSF and am trying to make a content controller. Basically whenever someone makes a request to www.myapp.com/external/** I'd like to forward to a controller that pulls external content into a page template and spits it out to the user.
For example /external/test/test.html might pull in content from a location XXYYZZ/test/test.html.
I was able to achive this pretty easily in Spring 3, but I'm a little confused on where to start with JSF.
I feel like I'd need to create a custom servlet to handle /external/**? But what would the class of this servlet be? What would it consist of?
Any help is appreciated!
I would suggest you not do this with the JSF servlet. Instead, do this with JAX-RS, or a custom servlet, or continue to do it with Spring. Map the custom servlet to a different URL pattern than your JSF pages. That way you can use JSF for the pieces where it is appropriate, and serve your static content as appropriate.
While I've never done so myself, Google yields plenty of examples where people have integrated Sping and JSF.