I'm currently running into issues with accessing HTTP resources (which are all local) while using the XForms Filter for Orbeon (in accordance with http://doc.orbeon.com/xforms/filter.html).
I am using a Java Servlet to process the data, and I'm trying to use the xf:submission element to access the Servlet in order to get/post the data required. The Java App and Orbeon wars are both deployed in the same tomcat instance, with all of the session handling etc set up as described in the link above.
Iniitally, the Servlet forwards the request to a .JSP with the XForms implementation, which loads fine. However, the page is then meant to request the data and display it. However, it doesnt do this - and the tomcat localhost access logs show no requests were made.
I know both the java code (and the XForms implementation) is correct, as I have written two .JSP's containing the java code from the servlet (and then call the JSP's directly from the xf:submission instead of the HTTP requests) and it works perfectly.
<xf:submission id="post-results-submission"
ref="instance('categories-instance')"
resource="http://localhost:8082/EmbeddedTesting/questionnaire"
method="post"
serialization="application/xml"
mediatype="application/xml"
replace=""/>
<xf:submission id="get-data-submission"
ref="instance('response-instance')"
resource="http://localhost:8082/EmbeddedTesting/questionnaire"
method="post"
serialization="application/xml"
mediatype="application/xml"
replace="instance"
instance="categories-instance"/>
These are the submission elements for accessing the HTTP resources.
Is there a reason these aren't being called at all? (as shown by logs) and if so, is it possible to fix them?
Also to note - I tested the servlet itself using the Advanced REST Chrome app and through the orbeon form builder HTTP actions (then clicking test) and both worked fine. It just wont work here for some reason. I've also made sure that the licence (for Orbeon Forms PE) is in the WEB-INF/resources/config and that it is still valid.
Here is a link to my XForms, uploaded to dropbox as an XML file so it can be previewed on dropbox: https://www.dropbox.com/s/aq4zx39ohjulcbx/index.xml?dl=0
I'm not sure if this is something I can actually do outside of Form Runner/Builder, so any help would be appreciated!
Related
I am using JSP with an Apache Tomcat server for a web project.
I do not understand .do files or how they are used, but I see them come up in answers describing how to do things I need to do.
My understanding thusfar is this:
The Struts framework uses .do files, so many people think of these in relation to one another
.do files are described in the Java Servlet Specification and are thus not specific to Struts
At least one major use case of .do files (which I'm interested in) is for easily passing data between HTML/JS forms and Java/JSP.
If I create a form such as the one in the first answer I linked:
<form action="MyServlet.do" method="post">
<input type="text" name="nameThree" value="Enter a Name" onClick="if(this.value == 'Enter a Name'){this.value = '';}" />
<input type="submit">
</form>
It will throw a 404 error, unless I create the .do file it mentions. Once that .do file is created the code runs, but does not appear to do anything because of some missing piece.
I'm not sure if the .do file is supposed to be empty or if I am supposed to put code in it, etc, etc.
Thus my question is: Outside of the context of the Struts framework, how are .do files used in JSP for passing data between Java and front end web forms?
I realize this can be a bit tricky to explain, so I'll do what I can.
".do" extensions (in the URL request) are most commonly found in struts 1 based web applications. That said, the way it works is that a webapp is configured to route http request with .do extensions to a strut servlet (usually in web.xml). This is how struts do its thing (map http requests to forms, identifies post vs get request, forwards to action classes, etc).
In a non-struts app, your http request need routing to a resource to be served. You can do this with a servlet. Alternatively, you can just return your static file (if you write your own file.do), but Tomcat won't be recognizing that file extension as a JSP that needs to be translated and compiled (hence Tomcat gives you a 404).
Likely you still need to configure your server (tomcat) to serve HTML pages. JSP is one of the many in which you can create dynamic HTML on the server. You can do a lot of things (that in today's standards are consider bad practices such as making db connections, making rest calls, etc). At the end, you can write Java in your JSP.
If you are building a simple webapp, I recommend using a different easier framework like Spring Boot to make it easier. If you absolutely want to do JSP, then make sure you http request always fetch .jsp files. If you need special extension, you'll need to map those to a servlet and write/configure it in web.xml.
Hope this helps!
I'm trying to do a single page application using servlets and jsp pages.
For the moment I have the first page, which is simple to do: a servlet that forwards to the corresponding jsp.
How should the implementation look when navigating to the second page?
I guess it should be an ajax call, the servlet would populate the necessary data, but how to display the second page jsp?
JSP is a server side ui technology. A Servlet listens to specific urls and redirects to JSPs pages. The JSP is compiled to a class (another servlet in fact), invoked (data will be added and inline scripts will run) and the output, whith is HTML, is send to the client (browser). To get to a different page its neccessary to query the server (servlet) for another url, resulting in another html page.
To create an SPA you need a client side technology like JavaScript. Your query the server for a single html page. The page, made of HTML and JavaScript, for example, (could even be the output of a single JSP, dont get confused) is send to the client (browser) and the JS is run. This is nomaly backed up by a framework like AngularJS, EmberJS or Backbone. Once the page is set up, the links within the page are anchors (http://example.com/#/mySecondPage), so clicking them will invoke the framework again (Ajax, querying the server for new data), but will stay on the same page. Some contents of the page might then be replaced by new content.
If it is a true SPA then you would just have a single JSP and handle all your functionality (after your initial page load) using Ajax.
Have you looked at using a client-side framework such as Angular to help you with this?
Depending on how rich your SPA is, you could either use the same servlet or multiple servlets to serve each page.
Unless you are doing this for a course or tutorial and have some constraints on how to achieve it, you will very probably save yourself a lot of time if you couple something like Angular with a server-side framework like Spring instead of coding servlets. As a suggestion have a look at Spring with Angular.
In SPA the browser only loads the document once (or a few, once per sub-application), and further communication to the server is done usually via AJAX or Websockets.
I recommend you to model your application as a thin server architecture, that is, a client application running in the browser (HTML, CSS, Javascript) consuming a web service API provided by the server.
The following are some points worth knowing;
Client-side:
Only presentation logic
Represent state by URL hash. This enables bookmarking, hyperlinking and browsing history. Your client app should listen to changes in the URL hash and act in consequence. This technique is called "routing" and it is implemented by all Javascript frameworks.
Client application is packaged server-side such it can be downloaded in a single request (in .html, .jsp, servlet, .jsp + multiple .jspf, ...)
Consumes services provided by the server via AJAX or Websockets
Server:
Offers client application to download
Provides a clean, stateless API to be consumed by the client application, better returning JSON (data) than HTML (presentation logic) (Why is it a bad practice to return generated HTML instead of JSON? Or is it?)
Use a REST or JSON-RPC frameworks to create the API. There is a lot of debate on what to choose (see here or here). In my opinion the only advantage of REST over RPC is that since REST has become a "de facto" standard its interoperability is higher, so my recommendation for SPA applications is using JSON-RPC, because your code is the only client of the API.
There are lots of alternatives for both client and server frameworks.
Javascript: AngularJS, EmberJS or Backbone,...
REST: Spring, Jersey, Restlet,..
JSON-RPC: https://en.wikipedia.org/wiki/JSON-RPC#Implementations
Regarding JSON-RPC, you might want to take a look to Brutusin-RPC, a JEE microframework I have created :)
If you are using an Ajax request, then you need to tell the browser that redirect to the second page. Example:
response.sendRedirect("second_page.jsp");
In your servlet, you need to differentiate a request to the first page, from a request that need to be redirected to the second page. You can use parameters, or session values, for example.
if (request.getParameter("page2") != null) {
response.sendRedirect("second_page.jsp");
} else {
.... // include here the normal logic of your Servlet for page 1
}
Then, you can invoke your servlet with or wihout the parameter page2, to go to page 1 (without parameter), or page 2 (with parameter).
As per my understanding files are cached by the browser by default. Developers don't have to set specific
headers to enable the caching of these files. Right?
If yes, how come browser know that it has to enable caching for specific types of files only not for servlet/jsp calls. For example :- if i make call to same servlet
request goes to server side and fresh page is served (not the browser cached paged).
I'm using the following jquery codes to load a Servlet inside a DIV.
$.get('Test',function(responseText){
$("#test").html(responseText);
});
The elements brought from this Servlet will use the css file including in this webpage.
But if I type in my browser ..../myProject/Test
It will display what this Servlet is meant to display, but with no css file, it will look bad.
How to restrict someone from accessing this Servlet via browser?
First of all, when you are fetching something using AJAX, you are accessing it via browser.
You can set some special HTTP header in AJAX call, but it's not secure by any means. However on the server side you can recognize this special header and if not present, refuse or return some different content.
Is it possible to embed html with java
test.html
<input id="buttonId" type="button" class="button-click"
value="" onClick="checkSucess(2)" onload="counts(count)">
test.js
checkSucess = function(firstVal) {
// Jquery Ajax with url,params and response
doPost('test.java',
'first=' + firstVal,
function(response) {
});
test.java
Here get the 'first' value from ajax request, and further processing.
I believe you're looking for JavaServer Pages (.jsp), a starting point for implementing server-side logic using Java. (You can GET/POST to a jsp.)
Reference
JSP + Ajax Example
JavaServer Pages Technology
JSP Tutorial
Well, Java on the server side doesn't work quite like PHP. i.e. you can't simply drop your java files in your htdocs directory and trigger it by filename directly. Firstly you'll need an app server like tomcat or jetty (instead of just a webserver like apache httpd). Secondly, you'll need to create a Servlet (simplest case) and write your java code there and trigger it using the server request url. Google "servlets" and you should be able to pick it from there..
No, you can't do like that, you have to use AJAX request to interact with java from your html or javascript. For that you have to use servlet and pass the servlet URL to doPost function.
doPost('url to servlet',
'first=' + firstVal,
function(response) {
});
Since this is your first java project, you should do some reading to come up to speed with java. Here are some good tutorials:
Core Servlets, Intermediate Servlets
Apache Tomcat 6 - Apache is a nice tool for learning servlets; it is easy to install and run.
Core Servlets; Advanced Servlets - This may be more than you need.
Applets are Java, and the only (usual) way for Java in the browser.
You can communicate with Applets from Javascript/JQuery code. Applets have ending .class (.java is source code, you can't communicate with it).
In the case you want to communicate with serverside Java, you need servlets there. Then send requests to the url of the servlets.
No. Besides using Java Applets - which are actually just plugins - there is no way to embed Java into HTML.
That being said, It is possible to generate HTML using Java Server Pages.
It is also possible to use an HTML page in conjunction with JavaScript to interact with Java via subsequent HTTP Requests made using AJAX. These requests are initiated on the client browser, and received and fulfilled by a server capable of executing Java Server Pages (JPS).
Example:
An HTML page is loadad with some JavaScript that requests some url upon completion of DOM loading.
The request is received by some server which then responds to the request.
The client browser receives the response and provides it to JavaScript to be dealt with.
JavaScript reads the response, and uses it in some way (like "refreshing" some information on the page).