Servlet technology

What is JavaServer Pages?

Java Server Page.


  • JavaServer Pages (JSP) is a technology for developing Webpages that supports dynamic content. This helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>
  • The JSP engine loads the JSP page from disk and converts it into a servlet content. This conversion is very simple in which all template text is converted to println( ) statements and all JSP elements are converted to Java code. This code implements the corresponding dynamic behavior of the page. .


JSP Life Cycle

JSP can have Life Cycle


  • Translation of JSP page.
  • Compilation of JSP page(Compilation of JSP page into _jsp.java).
  • Classloading(_jsp.java is converted to class file _jsp.class).
  • Instantiation(Object of generated servlet is created).
  • Initialisation(_jspinit() method is invoked by container).
  • Request Processing(_jspservice() method is invoked by the container).
  • Destroy(_jspDestroy() method invoked by the container)


Simple JSP example

welcome.jsp

<html>
<body>
<form>
<%
    String name=request.getParameter("uname");
    out.print("welcome "+name);
%>
<input type="text" name="uname"/>
<input type="submit">
</form>


Today's date: <%= (new java.util.Date()).toLocaleString() %>




 


Our exampe from gitlab

 git clone https://gitlab.com/developer.armenia/mavenjsp_bean

How to run

mvn package
mvn jetty:run


JSP vs Servlet

JSP Servlet
A web scripting language that helps developers to create dynamic web pages based on HTML or otherServlet is server side Java proframing module thet processes and responds to clinet requests by implementing the servlet interface
Focuses more on displaing informationMainly focuses on information processing
Executes slower comared to a servlet and it compiles into servletRan faster then JSP
Works as the view in MVC achitectureWorks as the Controller in MVC achitecture
Programming is easierProgramming is not as easy
Consist of the Java, HTML, XHTML or XMLA fully java code


JSP Tags


JSP Directives

JSP directives give some special commands to the Container while the JSP pages are translated to the Servlet code.

<%@ directive attribute=”value” %>
  • Page directive
  • Include directive
  • Taglib dircetive

Page directive


  • <%@ page contentType=”text/html; charset=ISO-8859-1” %>
  • <%@ page import=”java.util.date” %>
  • <%@ page language=”java” %>
  • and other

Taglib directive

The JavaServer Pages API allow you to define custom JSP tags that look like HTML or XML tags and a tag library is a set of user-defined tags that implement custom behavior. The taglib directive declares that your JSP page uses a set of custom tags, identifies the location of the library, and provides means for identifying the custom tags in your JSP page.

<%@ taglib uri = "uri" prefix = "prefixOfTag" %> When you use a custom tag, it is typically of the form <prefix:tagname%gt;

for example <%@ taglib prefix="jgc" uri="WEB-INF/custom.tld"%> then you can use <jgc:HelloWorld/>


Standard Action Tags

The action tags are used to control the flow between pages and to use Java Bean

  • jsp:forward forwards the request and response to another resource
  • jsp:paramset the parameter value
  • jsp:include includes another resource
  • jsp:useBean create local bean object
  • jsp:setProperty sets the value of property
  • jsp:getProperty prints the value of property
  • jsp:plugin embeds components
  • jsp:fallback its use in jsp:plugin, print the message