I´ve programmed some basic web applications using GWT. So all I´ve coded is just pure java (although the client part is transformed to javascript).
What I wanna know is how can I access and manipulate html elements (fields of a form, some text, etc) from server side using java (no GWT).
I mean, a web application where I have some .html files and then I use some kind of framework from server side. Is that way of accesing html element framework dependent? How could I manipulate those elements from a servlet, for example?
I know it´s a very general question, but I´d appreciate any help or tips, thanks.
I think, at first you might want to look at a basic servlet tutorial and at a JSP tutorial. These are the first steps, no frameworks are involved here, but I think for the basic understanding of how it works you really should look through this information, and then only read about Spring or any other framework with a great MVC structure.
What I wanna know is how can I access and manipulate html elements (fields of a form, some text, etc) from server side using java (no GWT).
The server side creates the HTML (possibly using data submitted through a form). At the lowest level, it's simply concatenating text. The various web application frameworks offer different ways to work at a more abstract level, i.e. via a template mechanism (JSP) and/or through component libraries that replace elements in a basic HTML layout with the actual data (JSF, Wicket).
However, once the HTML has been generated and sent to the client, it cannot be manipulated by the server, except through Javascript sent along with the HTML and executed by the browser. AJAX then allows it to connect back to the server.
Take a look at Spring MVC Framework.
Related
I'm learning JavaEE now and I kind of feel that JSP can be messy... I hate to mix my Java code with HTML... I want to separate the front end view and Java code as much as possible.
I've ever tried Twitter Bootstrap and I find it amazing. Is there any way that I can use HTML and JS(maybe some framework like Bootstrap) to substitute for JSP view technique? How should I accomplish the data interaction?
Yes, that is what all the kids are doing. You use REST.
You can see an example of just that in this Quickstart.
Strictly speaking, no. JSP is a server-side technology. That means that the code you write in the page is executing on your server in the environment you have set up there. HTML and JavaScript are client-side technologies; the code you write in these languages executes in the web browser of the people viewing your website. Neither technology is a complete replacement for the other; both are needed.
Even if you move more of functionality out of JSP into JavaScript (a "heavy client" implementation), you still need some server technology for your JavaScript to call to. You could get rid of JSP by moving some of the server functionality into other technologies: webservices (JAX-WS, Axis, etc.), RESTful services (JAX-RS, ApacheCXF, etc.), or straight up synchronous web-pages. But the point is regardless of what you use or whether you get rid of JSP entirely, you still need a server-side technology.
I have been doing web development with PHP for the last few years, and like a lot of people, I have a strong dislike for PHP. I also have done lots of Java development, but never web development. Having a good knowledge of Java, I decided I would try web development using JSP. I have read a few articles but I am not fully "understanding" it. My first question is, what is the difference between a JSP (Java Servlet Page) and a servlet? Is a JSP not simply a file that contains a servlet and HTML? My second question is can Servlets interact with HTML elements as easily as PHP? Can I check for HTML form input using POST/GET etc.
I was also wondering if Servlets could use "native" java code. By this, I mean can I use the same code that I use in desktop applications/use the same methods and classes that I have already made. What I am trying to get at is, can servlets do (almost) anything that a desktop application can do. Can I access the servers file system to delete/ modify files? Can I use third part java libraries in my JSP? If someone could clear this up for me that would be great! Thanks in advance!
JSPs are a templating language for generating servlets. Every JSP is compiled into a servlet. Everything you can do with a JSP can be written as a servlet.
No, a JSP does not contain a servlet. It's compiled into servlet Java code and then that's compiled into Java byte code.
Servlets can interact with all HTML elements. They're HTTP listeners.
I would strongly advise you not to use native code in a servlet.
You should not be putting scriptlet code into JSPs. That's a 90s style of writing JSPs that's been discredited. Use JSTL. JSPs are for display only.
At a basic conceptual level:
JSPs are similar to PHP in that they provide server-side mark-up scripting to standard HTML pages, allowing for dynamic content within a page. The language a JSP uses is similar to HTML, but it is not valid HTML without being pre-processed by a web container like Apache Tomcat. A JSP can contain Java code, although due to separation of concerns this is often discouraged in large systems.
Servlets are separate files written entirely in Java and are used to process actions requested by the client. The requests sent to a servlet can take the form of any HTTP action type, i.e. GET/POST/PUT/etc. A servlet can technically do everything (and more) that a JSP page can do, but the mark-up language that JSP provides is more suitable for working on the presentation layer.
As an example: you may have an HTML form in your JSP that you POST to a servlet, which might then use the posted form data to send a secure email. Once the email has been sent, the servlet may redirect you to a success/failure page, which in turn could be another JSP page. More recently Javascript has become a more common way to interact with servlets, using the XMLHttpRequest API.
Take a look at this guide, particularly the diagram provided.
My website is built on JSP and Java Servlets. Html, Javascript and CSS (limited) is contained within my JSP pages as well.
I recently started looking into Joomla. I have quite a bit to learn yet...
I understand that Joomla is based on PHP.
[ I don't know anything about PHP, but I've read that knowing PHP is not really necessary in order to use Joomla. Do you agree? ]
I have some questions about Joomla -
1) The code that Joomla generates, is that in PHP? or Html? Perhaps a combination of both?
2) How, if possible, can I integrate my JSP code into what Joomla generates?
-- or do I need to integrate the Joomla code into my JSP code?
I have a fully functional website designed in JSP and Java - but after seeing the layout/design that can be accomplished with a CMS, such as Joomla, I want to know how I can 'convert' my site, or 'integrate' it, and still keep it running under JSP and Java.
Joomla is written in PHP. It generates the HTML files which are output to the browser. As far as I know, you'll need PHP to use Joomla unless there's been some sort of JSP port for it (which is highly unlikely).
Simply, Joomla IS PHP, and it generates HTML code which the browser reads.
Also, a CMS has nothing to do with layout/design, a CMS is simply that, a Content Management System. They are usually designed to be used as the actual website itself, the user just picks a theme and inserts content.
If you are looking to keep the server-side business based on JSP, JAVA and upgrade the layout using Joomla you are facing a lot of PHP programming and the result would not be great in terms of performance. It is easier to migrate to a solution based on JSF and Facelets.
Joomla is written in PHP ans as with every other server side webbased view/template technology (including JSP), it just generates HTML/CSS/JS (a webbrowser doesn't understand anything else anyway).
If the sole requirement is to have a webbased blog engine with a CMS in Java/Servlet, then checkout Hippo. It's similar to Wordpress/Joomla (although less eye-candy imo).
You could check out Quercus from Caucho which allows you to run PHP applications in a servlet container. I believe they support Joomla though I'm not sure which versions.
Possible solution or answer is that you will need complete rewriting.
You can use Joomla for the web content and JSP for the front end of an enterprise application. Integrating external content in Joomla can be done in many ways, for example, you can embed pages in Menu items, AJAX calls to retrieve content from your JSP pages -either in html or in another format (JSON or XML).
How exactly or what exactly does Direct Web Remoting (DWR) do?
While I have already gone through the official site http://directwebremoting.org/
I wanted to understand in simple language..
Specifically I have following questions;
1. While engine.js is standard Javascript library, how exactly does it help in including that in our app ?
2. What is so special about DWR that cannot be done by normal Javascript / AJAx combination?
Thank you.
OK, I've only had a brief look at DWR and I was wondering the same thing. As far as I can gather DWR builds and includes on your page some javascript that mirrors your server-side java. It will generate client-side proxies that you can call in your javascript. So instead of using jquerys $.ajax(...) command and specifying the url, data type, data, etc, you can just use RemoveObj.doSomeStuff(...) and it'll do the actual AJAX request for you, behind the scenes.
This is basically what ASP.NET does too when you declare a [WebMethod]. Its simply a shortcut and might save time with maintenance, e.g. if your urls change I suspect DWR will update its client-side code too.
My only issue with this is that it seems to pollute the namespace.
To actually answer your question it does nothing you can't do with normal javascript and servlets.
I am trying to create a website, kind of a search engine.
In the back-end I have Java servlets which process requests and return responses.
I am not sure what I should use for front-end, so that I can keep the back-end and the front-end completely (or as much as possible) de-coupled. Also, which one of these (or some other) has the most capabilities.
Few options are:
Freemarker templates
JSP
PHP
Please help me; how can I proceed with this idea?
The servlet API does not out the box offer seamless integration with PHP as view technology. You would in PHP need to invoke HTTP requests to the servlet using curl and consorts or to provide the servlet as a webservice and use JS/Ajax to get results from it in the generated PHP output. That's may unnecessarily end up to be too clumsy and expensive.
Use a Java based view or template technology. JSP is a perfect suit since it goes hand in hand with Servlets. You just store the search results as a request attribute, forward the request to the JSP and then use taglibs/EL in the JSP to present it.
If you want more templating capabilities, e.g. reusing the same template for both HTML output or in some email message or for whatever purpose you'd like to generate the output programmatically without the need for a HTTP request, then Freemarker is a better choice.
See also:
JSP info page
Servlets info page
JSP's are servlets so development is can be easily coupled if not done properly and ensuring the JSP's only stay as the views.
PHP would have to be entirly de-coupled as it is a seperate language.
Freemarker templates - I have never used this product so I can't recommend anything.
Another option would be using JavaScript/HTML for your view. Servlets could serve up the data and JavaScript could run on the client side and create the view.
Of course any of your options will function it comes down to what technology you are experienced with and what is your timeframe. If you are on a short timeframe and are already familar with Java and servlets then you are not far off from just using JSP to render your front-end view. Although my preference is for servlet back-end using Jax-RS to serve the data as JSON to a JavaScript front-end view.
The usual way to create dynamic web pages in Java is to have servlets on the server that creates html that is sent to the browser. Technologies like JSP and Freemarker templates is designed as enhancements to servlets that makes it easier to create the html. JSP pages will i.e. be compiled to pure servlets when you deploy them.
If you want to decouple the technology that creates the html totally from the servlets you have created, then I would go for static html and ajax. You would create your system this way:
Create static html pages with javascript. Use a javascript library like jquery.
Create your servlets so that they accept parameters and respond using json-formatted data.
Write Javascript in your html-pages that send queries to the servlets and modifies the html-code based on the response.