Go easy please, I'm rather new to the whole web development thing and I'm a little perplexed.
I have the Spark framework installed from Maven, which has Jetty as its underlying web/application server, correct? Jetty is embedded in the Spark jar so I can't/shouldn't mess with it. Is that correct so far?
I read that Spark's embedded Jetty should support rendering of JSP files, however my browser is literally interpreting my JSP pages as text documents. How do I tell Spark/Jetty that it needs to interpret and serve these pages as opposed to just serving them as is?
I'm using Spark 2.5 and the velocity template engine.
Very minimal code so far. Rather than setting up a route to my JSP file I've just been typing the path into my browser while Jetty is running.
I've tested several different minimal JSP files this way.
I added a route in Spark to my JSP file and lo and behold, now it attempts to render the file! I suppose Spark has to handle the interpretation of the string passed to it and Jetty simply serves the content? Beats me! It works!
EDIT: Sorry to bring this back from the dead, but it doesn't have an answer yet and clarity was asked for so.... I added the route using the spark framework in my program, and used the render() function of spark to have Jetty render the JSP file. I had previously been entering the path of the JSP file as a static file, and so Jetty was not interpreting the contents of the file server side (as you would do with JSP) but instead trying to hand the file itself over to the client's browser. Rookie mistake!
Related
I'm currently developing a Java project where I need to implement a Webserver. The webserver should be implemented because the installation should be easy. I already took a look at WebServlet and HttpServer from sun. As far as I know, it is always necessary to put the whole html into quotes in oder to use vars -e.g. collecting data from DB. What is the easiest way to use java code in html like php? (not js) Including the php module into the java server seems wrong to me. But when putting the whole document into vars, quotes are always necessary and it has no syntax highlights.
EDIT: I do not mean static content :)
I had developed a lucene search engine (with Lucene 5.2.1) and now I want to embed it into a very simple web application (it has demonstrative purpose, it will run in localhost).
This web application should let the user insert some keywords and navigate through search results (in other words it should let the user read the index that I had created, with appropriate html formatting). I had already made some simple html GUI with Bootstrap.
I'm very new to web applications, but I had been told that a good idea would be embedding Lucene in a TomCat server. I had visited this link, but it's quite old and not very detailed. I have tried to look for "lucene tomcat" but I get results about SolR or very specific posts.
Can anyone please suggest me a good resource or a general approach to my issue?
Many thanks
My solution:
I wrapped my search engine library into a JAR file.
I downloaded zipped version of TomCat 8 and installed it into my Eclipse environment as a new Runtime Enviroment.
I made a Dynamic Web Project in Eclipse (see this video lesson) using TomCat 8 server.
I put the JAR into my webapp (should be something like WebContent\WEB-INF\lib\searchEngine.jar)
I created a Servlet to handle user input.
I did the rest using very simple JSP pages, embedding Java code that uses my JAR (I know, that is not so good but it works!)
Cheers!
Recently I've discovered that document upload feature in Alfresco backed with a simple Java Servlet (UploadContentServlet.java). I don't understand why it is a servlet not a webscript. Since everwhere in Alfresco webscripts are used.
Another question which pops up is what's the difference between Alfresco webscript and Java Servlet? They look almost the same, except the procedure to create them and that Alfresco WebScript response could be customized with some ftl template.
Is there any other differences?
As you can find here Alfresco UploadContentServlet, UploadContentServlet is
Responsible for streaming content directly from servers into the repository using the HTTP PUT command. The NodeRef of the node onto which the content will be streamed can be encoded into the URL. In this situation the content of the property can be considered to be updated once the servlet has returned.
It is related to URL addressability, a topic that seems to be discontinued on newer Alfresco versions.
By the way, I think that the reason could be that using a servlet is more efficient especially when uploading huge files.
Mainly webscripts are easier to write and to mantain than servlets. Moreover if you change something in your server side javascript file all you have to do is to reload the container, instead of reloading the context of your application.
This link may show you the architecture Web Script Framework and even if it is quite old the information is still valid for newer versions of the platform.
Ftl templates are very useful and remember that the platform gives you a lot of root objects that you can use in your webscripts.
I am using Weblogic for deploying my Java EE applications. I am using JSP technology for my project. I know that the jsp files are processed in the jsp engine and in the life cycle of jsp, it is converted into the servlet code.
Can you please tell me that how I can get the generated servlet code?
This will vary based on the weblogic set up on your box. If weblogic is compiling your JSPs (you are not pre-compiling them) then it may be under domains\<YOUR_DOMAIN_NAME>\servers\<APP_SERVER_NAME>\tmp\_WL_user\<APPLICATION_WAR> .
It is generally a good idea to pre-compile your JSPs for performance reasons and add the servlet mappings in the web.xml. You can see a reference to how to do that in weblogic here under the section "Precompiling JSPs".
In my application I am using Maven and the JSP servlet compiles are sent to the war file in the \target folder of the maven project, i.e. \target\.war\WEB-INF\classes\jsp_servlet. If you find the jsp_servlet folder you will find the compiled Java servlet code and the class file. With Eclipse you could use the debugger and set breakpoints. A word of caution, the servlet code is complex. If you are trying to debug JSP I would use other techniques, such as temporarily embed EL commands in the JSP to display data and functionality. Also, JSP should have little or no business logic so it would also be advisable to focus on the model and controller.
I am working on a project which is basically a Java application with an embedded IE browser (using JDIC 0.9.5) to display custom HTML files (stored locally that I created). I have a test HTML file with a JavaScript function that checks a simple form with checkboxes and alerts the user with a dialog stating which checkboxes are checked.
My question is, is there a way for my Java application to do the same procedure on the embedded HTML form instead of using JavaScript. I want to keep my application and HTML files simple without the clutter of JavaScript in my HTMLs or a pile of .js files.
Thanks for any help and guidance!
You have two options. Either shift the project to run the Java on the server side using JSP technology inside a web container like Apache Tomcat or Jetty, or write you web page to open up a Java applet.
The applet route allows you to run the code on someone else's machine, and as a trade off you will have to run the application in a strongly security constrained environment. After all, if someone were to run code on your machine, you wouldn't want it able to access your disk, etc.
The JSP solution will have you running the code on the same machine as your web server, since you (probably) control your own web server, the code will not be ran with as many security constraints enabled. This means the code can make requests to other machines, write and read files, etc.
You could replace your model with a client-server model using Java Server Pages (JSP).
If you are displaying the page through an embedded browser it is very unlikely that you will be able to get access to the DOM via Java.
One option is to use GWT javascript compiler to code in java and then convert to javascript. If it will only use IE, then you will only need to keep one of the generated .js files, so the clutter will be low.
Since you're embedding your HTML files in your own Java program, I would recommend you to use one of these 2 approaches:
1.- Use Javascript and structure the files cleanly, is not that complex.
2.- When you do the POST check the values in your Java code and return a new dynamically generated HTML file with needed info
I sincerely recommend you to follow the first option. Other options to work with Java in HTML would be JSP or GWT, but both will require a proper J2EE server, which would be overkill in your application