2

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!

user1947236
  • 673
  • 3
  • 12
  • 27
  • possible duplicate of [What is the difference between JSF, Servlet and JSP?](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp) – BalusC Apr 02 '13 at 02:26

2 Answers2

3

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • To support your last sentence about scriplets: [How to avoid Java code in JSP files?](http://stackoverflow.com/q/3177733/1065197) – Luiggi Mendoza Mar 30 '13 at 19:33
0

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.

seanhodges
  • 17,426
  • 15
  • 71
  • 93
  • When you say "post to a servlet." Does that mean that the "servlet" is in another file or what... – user1947236 Mar 30 '13 at 19:31
  • You might also GET or PUT to a servlet. – Luiggi Mendoza Mar 30 '13 at 19:31
  • 1
    @OP What Sean describes is having a JSP which renders a form (possibly using data from the server) and having a separate servlet (with no display code) to handle the form submission. A JSP is a way of writing a servlet that places the focus on the rendering (view) part but you can use a straightforward Java servlet to handle the POST data and then redirect to another JSP to inform the user of the result. – Adriaan Koster Mar 30 '13 at 20:18
  • @AdriaanKoster is correct, you would have 2 files - a JSP file ("compose.jsp") and a servlet ("SendEmail.java"). Try some of the Java servlet tutorials on the Web to create a simple web app... – seanhodges Apr 02 '13 at 01:43
  • @seanhodges Ok, and as for my other question, can I use native java libraries in servlets? – user1947236 Apr 02 '13 at 23:15
  • @user1947236 yes you can do pretty much anything inside a servlet. One thing to watch out for is state - a servlet has a very specific life cycle. If you want to persist any data, consider putting it in a database or a singleton class rather than a field variable. This link gives some more insight to this behaviour: http://oreilly.com/catalog/jservlet/chapter/ch03.html – seanhodges Apr 02 '13 at 23:57