Reputation: 673
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!
Upvotes: 2
Views: 357
Reputation: 17524
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.
Upvotes: 0
Reputation: 308733
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.
Upvotes: 3