Register Login
Internet / AI Technology University (ITU/AITU)





|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 4. Web Apps Frameworks
Current Topic: 4.3. Servlets and Java Server Pages
Sub-Topics: 4.3.1. Application Servers - Tomcat | 4.3.2. Application Servers - JBoss or WildFly | 4.3.3.Major concepts and steps of a Dynamic Web Project | 4.3.4.Troubleshooting Problems and Debugging Code in Web Applications
-- Scroll to check for more content below...
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
4.3.Servlets and Java Server Pages

What is the place for servlets and JSPs in web applications?
Servlets and Java Server Pages provide the core of server side in web applications. All Java based frameworks, which support MVC design pattern, are based on Java Servlets and JSPs.

The Concept Maps below provides a good illustration to client and server components – participants in web applications.
MvcConceptMaps

The client side includes a client program – a web browser. A browser can work with HTML pages. In other words, web browsers render HTML into video and audio for human beings.

Html can be enhanced with Cascade Style Sheet (CSS) and Java Script. On the client side you can see familiar names, such as Bootstrap, jQuery and AngularJS.

The server side includes a server program – a web server, which interacts with a set of frameworks.
In this section we will learn about Servlets and JSPs - the core for Java-based frameworks, which support the MVC design pattern.

The illustrations below show the long way MVC went from its original to its current implementation.
MvcModel2

The first implementation, Common Gateway Interface (CGI), effectively sent the requests from a client to a server, targeting an execution program on the server side. The program started its processing and ended by sending back to the client a response, usually an HTML page.

It was simple and expensive. Yes, it was expensive in terms of server resources. Each client request fired a program on the server. Ten users sending requests would fire ten programs. Hundred users sending requests would bring the server down.

MVC Model 1 and then MVC Model 2 consistently improved the separation of concerns (Model-View-Controller) and drastically improved performance by introducing Java Servlets.


What are Java Servlets?
Servlet – A Standard Java class to handle an HTTP request and provide a response, usually as HTML page, back to a client.
Java Servlets are glued to a web server. A web server pass every client request to a servlet. Instead of creating a new program on each request, servlets just fire a tiny shadow – a new thread with that program.
A Java Servlet receives a request from a Web browser and sends back to a browser an HTML page.

A java Servlet must have doGet() and doPost() methods that are responsible for processing GET and POST types of HTTP requests.
Here is a greatly simplified example of a Java Servlet.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class HelloServlet extends javax.servlet.http.HttpServlet {
public void doGet( HttpServletRequest request, HttpServletResponse response) throws Exception {
String html = "My Web Page Title" +
"

This is just an example

" +
"";
response.getWriter().println(html);
}
}

In this example a Java Servlet HelloServlet uses the doGet method to create and send back a very small HTML page with the announcement This is just an example.

Any Get – request to that server will come back to a client with this message: This is just an example.

More realistic code would call some other classes for processing the request and creation of a dynamic web page. But we do not want to jump ahead too much…
JSP – Java Server Pages allow for a better separation of Java and HTML code;
JSP can include JSP scriplets, pieces of Java code embedded in HTML
Here is a simple example of a JSP which dynamically provide us with a login name and a current date by including Java code directly into the page.


<%@page import="java.util.Date" %>
<%
String login = request.getParameter(“login“);
%>

Simple date with JSP


A dynamic value of Date is provided by a JSP scriplet below.


Today’s date is <%= new java.util.Date( ) %>
The login name is <%=login%>



Java Server Pages may include special tags that can be rendered into several lines of Java or HTML code. This is another way to simplify Web page development. One small tag – instead of several lines or even paragraphs of code. These tags are collected into the Tag Libraries.

Tag libraries – Tag Libraries define custom tags that encapsulate Java code and HTML. Thus, one JSP tag can replace a lot of code from JSP pages and make them much cleaner.
There are several well-known JSP Tag libraries, such as appeared in the STRUTS framework or offered and supported by Oracle Java Server Pages Standard Tag Library (JSTL).

JSTL has support for common, programming tasks such as iteration and conditions, tags for processing XML documents, SQL related and other tags. JSTL has a very powerful provisioning for integrating existing custom tags with JSTL tags. In other words, a developer can create a composite JSP tag that includes other existing tags.
Was it clear so far? Highlight the text in question Or


There is a special keyword on JSP pages to declare a tag library.
The taglib directive declares a custom tag library and identifies the location of the library.
Example:

<%@taglib prefix="itsDate" uri="/WEB-INF/its.tld" %>




The tag CurrentDate will be rendered with the code located in the library – file its.tld.
This Java code will provide a current date as a string:

Date d = new Date();
String dateAsString = d.toString();

When you have a lot of Java code on your Java Server Pages it is a good idea to think of creating your custom JSP Tag Libraries.
JSTL – the standard library, which is offered and supported by Oracle (previously designed by Sun Microsystems) must be declared as follows:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Here is a small subset of JSTL tags:

Like <%= ... >, but for expressions.
Sets the result of an expression evaluation in a 'scope'
Removes a scoped variable (from a particular scope, if specified).
Catches any Throwable that occurs in its body and optionally exposes it.
Simple conditional tag which evaluates its body if the supplied condition is true.
Simple conditional tag that establishes a context for mutually exclusive conditional operations,
marked by and

Subtag of that includes its body if its condition evalutes to 'true'.
Subtag of that follows tags and runs only
if all of the prior conditions evaluated to 'false'.
Retrieves an absolute or relative URL and exposes its contents to either the page,
a String in 'var', or a Reader in 'varReader'.
The basic tag for iterations/loops, accepting different types of collections
Iterates over tokens, separated by the supplied separators.
Adds a parameter to a containing 'import' tag's URL.
Redirects to a new URL.


Here is an example of a JSP with JSTL core tag forEach:

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>


For each example




Next Iteration






Required jar files in the project WEB-INF/lib directory: jstl.jar & standard.jar
These jar files include Java code that provides rendering of the forEach tag into the iteration loop resulting in three lines:
Next Iteration 1
Next Iteration 2
Next Iteration 3

There are many more standard tags including formatting and SQL tags that can be placed in the Java Server Pages.

But keep in mind that JSP must be just a VIEW portion in the Model – View – Controller design pattern.
We will learn to keep processing in Java classes and minimize code in any form in Java Server Pages.
Assignments:
1. Read on MVC and Servlets:
- http://ITUniversity.us/school/public/training/2.web.pdf
- http://ITUniversity.us/school/public/training/4.jsp.pdf
2. Optional: watch on Youtube 1-2 sessions on JSP & Servlets and email the best links to dean@ituniversity.us
For example: https://www.youtube.com/watch?v=b42CJ0r-1to&list=PLE0F6C1917A427E96
3. Create two QnAs on JSP and two on Servlets, save in a text document created by WordPad with the name 4.3.Servlets.QnA.Your.Name.txt and send it to dean@ituniversity.us
Questions and Answers (QnA): QnA1 | QnA2
We offer a fun way to share your experience and be rewarded.
You create a puzzle - quiz with a good question and several answers, one correct and several wrong ones.
The question as well as answers can include text, links, video.
This is your creation, completely up to your taste, humor, and of course your knowledge.
If there is an existing quiz above, you need first to solve the puzzles and evaluate the quality (at the bottom of every quiz).
After finishing the existing quiz you will be able to create your own. The best puzzles will be rewarded!
We invite you to create your own questions and answers (QnA) to increase your rank and win the Top Creativity Prize!

Topic Graph | Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/>import javax.servlet.*; 
<br/>import javax.servlet.http.*; 
<br/>import java.io.*;
<br/>public class HelloServlet extends javax.servlet.http.HttpServlet {
<br/>    public void doGet( HttpServletRequest request, HttpServletResponse response) throws Exception { 
<br/>           String html = "<html><head><title>My Web Page Title</title></head>" + 
<br/>           "<body><h1> This is just an example</h1>" +
<br/>           "</body></html>";
<br/>           response.getWriter().println(html);    
<br/>    }
<br/>}
<br/>

In this example a Java Servlet HelloServlet uses the doGet method to create and send back a very small HTML page with the announcement This is just an example.

Any Get – request to that server will come back to a client with this message: This is just an example.

More realistic code would call some other classes for processing the request and creation of a dynamic web page. But we do not want to jump ahead too much…
JSP – Java Server Pages allow for a better separation of Java and HTML code;
JSP can include JSP scriplets, pieces of Java code embedded in HTML
Here is a simple example of a JSP which dynamically provide us with a login name and a current date by including Java code directly into the page.

<br/><%@page import="java.util.Date"   %>
<br/><%
<br/>String login = request.getParameter(“login“);
<br/>%>
<br/><html> 
<br/><head><title>Simple date with JSP</title></head>
<br/><body>
<br/> <p>
<br/>A dynamic value of Date is provided by a JSP scriplet below.
<br/></p><p>
<br/>Today’s date is <%= new java.util.Date( ) %>
<br/>The login name is <%=login%>
<br/></p></body></html>
<br/>

Java Server Pages may include special tags that can be rendered into several lines of Java or HTML code. This is another way to simplify Web page development. One small tag – instead of several lines or even paragraphs of code. These tags are collected into the Tag Libraries.

Tag libraries – Tag Libraries define custom tags that encapsulate Java code and HTML. Thus, one JSP tag can replace a lot of code from JSP pages and make them much cleaner.
There are several well-known JSP Tag libraries, such as appeared in the STRUTS framework or offered and supported by Oracle Java Server Pages Standard Tag Library (JSTL).

JSTL has support for common, programming tasks such as iteration and conditions, tags for processing XML documents, SQL related and other tags. JSTL has a very powerful provisioning for integrating existing custom tags with JSTL tags. In other words, a developer can create a composite JSP tag that includes other existing tags.





Was it clear so far? Highlight the text in question

Or



There is a special keyword on JSP pages to declare a tag library.
The taglib directive declares a custom tag library and identifies the location of the library.
Example:
<br/><%@taglib prefix="itsDate" uri="/WEB-INF/its.tld" %>
<br/><body>
<br/><itsDate:CurrentDate/>
<br/></body>
<br/>

The tag CurrentDate will be rendered with the code located in the library – file its.tld.
This Java code will provide a current date as a string:
<br/>Date d = new Date();
<br/>String dateAsString = d.toString();
<br/>

When you have a lot of Java code on your Java Server Pages it is a good idea to think of creating your custom JSP Tag Libraries.
JSTL – the standard library, which is offered and supported by Oracle (previously designed by Sun Microsystems) must be declared as follows:
<br/><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<br/>

Here is a small subset of JSTL tags:
<br/><c:out > Like <%= ... >, but for expressions.
<br/><c:set > Sets the result of an expression evaluation in a 'scope'
<br/><c:remove > Removes a scoped variable (from a particular scope, if specified).
<br/><c:catch> Catches any Throwable that occurs in its body and optionally exposes it.
<br/><c:if> Simple conditional tag which evaluates its body if the supplied condition is true.
<br/><c:choose> Simple conditional tag that establishes a context for mutually exclusive conditional operations, 
<br/>  marked by <when> and <otherwise>
<br/>
<br/><c:when> Subtag of <choose> that includes its body if its condition evalutes to 'true'.
<br/><c:otherwise > Subtag of <choose> that follows <when> tags and runs only 
<br/>  if all of the prior conditions evaluated to 'false'.
<br/><c:import> Retrieves an absolute or relative URL and exposes its contents to either the page, 
<br/>  a String in 'var', or a Reader in 'varReader'.
<br/><c:forEach > The basic tag for iterations/loops, accepting different types of collections 
<br/><c:forTokens> Iterates over tokens, separated by the supplied separators.
<br/><c:param> Adds a parameter to a containing 'import' tag's URL.
<br/><c:redirect > Redirects to a new URL.
<br/>


Here is an example of a JSP with JSTL core tag forEach:
<br/><%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<br/><html>
<br/><head>
<br/><title>For each example</title>
<br/></head>
<br/><body>
<br/><c:out value="This example will display 3 lines with values: 1,2,3" />
<br/><c:forEach var="i" begin="1" end="3">
<br/>Next Iteration <c:out value="${i}"/><p>
<br/></c:forEach>
<br/></body>
<br/></html>
<br/>

Required jar files in the project WEB-INF/lib directory: jstl.jar & standard.jar
These jar files include Java code that provides rendering of the forEach tag into the iteration loop resulting in three lines:
Next Iteration 1
Next Iteration 2
Next Iteration 3

There are many more standard tags including formatting and SQL tags that can be placed in the Java Server Pages.

But keep in mind that JSP must be just a VIEW portion in the Model – View – Controller design pattern.
We will learn to keep processing in Java classes and minimize code in any form in Java Server Pages.
Assignments:
1. Read on MVC and Servlets:
- http://ITUniversity.us/school/public/training/2.web.pdf
- http://ITUniversity.us/school/public/training/4.jsp.pdf
2. Optional: watch on Youtube 1-2 sessions on JSP & Servlets and email the best links to dean@ituniversity.us
For example: https://www.youtube.com/watch?v=b42CJ0r-1to&list=PLE0F6C1917A427E96
3. Create two QnAs on JSP and two on Servlets, save in a text document created by WordPad with the name 4.3.Servlets.QnA.Your.Name.txt and send it to dean@ituniversity.us
Questions and Answers (QnA): QnA1 | QnA2

We offer a fun way to share your experience and be rewarded.
You create a puzzle - quiz with a good question and several answers, one correct and several wrong ones.
The question as well as answers can include text, links, video.
This is your creation, completely up to your taste, humor, and of course your knowledge.
If there is an existing quiz above, you need first to solve the puzzles and evaluate the quality (at the bottom of every quiz).
After finishing the existing quiz you will be able to create your own. The best puzzles will be rewarded!

We invite you to create your own questions and answers (QnA) to increase your rank and win the Top Creativity Prize!


Topic Graph | Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to AI
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy