2079 3

CSC419 ยท 2079 ยท 10 marks

Define JSP. What are the benefits of using JSP? Create a HTML file with two text fields to first name and last name. Create a JSP file that reads data from the HTML form and display full name.

Define JSP. What are the benefits of using JSP? Create a HTML file with two text fields to first name and last name. Create a JSP file that reads data from the HTML form and display full name.[10]

Advanced Java ProgrammingChapter 7: Servlets and Java Server pagesServlet vs JSP, JSP Access Model, JSP Syntax

Quick answer

JavaServer Pages (JSP) is a server-side technology used to create dynamic, platform-independent web pages. JSP allows developers to embed Java code directly into HTML pages using special JSP tags. When a JSP page is requested by a client, the server processes the Java code, generates an HTML response, and sends it back to the browser.

JSP: Definition, Benefits, and Form Handling


1. Definition of JSP

JSP is built on top of Servlet technology. Internally, a JSP page is automatically translated into a Servlet by the JSP engine (container) before execution.

Syntax of JSP Scriptlet:

<% Java code here %>

2. Benefits of Using JSP

BenefitDescription
Separation of ConcernsHTML (presentation) and Java (logic) can be written separately, making code cleaner and easier to maintain.
Easy to UseDevelopers familiar with HTML can embed Java code without writing full Servlet classes.
Platform IndependentJSP runs on any platform that supports a Java-compatible web server (e.g., Apache Tomcat).
ReusabilityJSP supports JavaBeans and custom tag libraries, promoting code reuse.
Automatic Servlet TranslationThe JSP container automatically converts JSP into a Servlet, so the developer does not need to write boilerplate Servlet code manually.
Implicit ObjectsJSP provides built-in implicit objects such as request, response, out, session, etc., which simplify development.
Integration with Java EEJSP integrates easily with JDBC, EJB, and other Java EE technologies for building enterprise applications.
Better MaintainabilitySince HTML and Java are written in the same file with clear separation using tags, updating the UI does not require recompiling Servlet classes.

3. HTML File with Two Text Fields (First Name and Last Name)

File Name: index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Full Name Form</title>
  </head>
  <body>
    <h2>Enter Your Name</h2>

    <!-- Form submits data to the JSP file using POST method -->
    <form action="displayName.jsp" method="post">

      <label for="firstName">First Name:</label>
      <input type="text" id="firstName" name="firstName" placeholder="Enter first name" />
        

      <label for="lastName">Last Name:</label>
      <input type="text" id="lastName" name="lastName" placeholder="Enter last name" />
        

      <input type="submit" value="Submit" />

    </form>
  </body>
</html>

Explanation:

  • The form uses method="post" to send data securely.
  • The action="displayName.jsp" attribute directs the form data to the JSP file for processing.
  • Two text fields are created with name="firstName" and name="lastName", which are used as parameter names in JSP.

4. JSP File that Reads Form Data and Displays Full Name

File Name: displayName.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
  <head>
    <title>Display Full Name</title>
  </head>
  <body>
    <h2>Full Name Display</h2>

    <%
      // Read the form parameters sent from index.html
      // request is an implicit object in JSP (similar to HttpServletRequest in Servlet)
      String firstName = request.getParameter("firstName");
      String lastName  = request.getParameter("lastName");

      // Combine first name and last name to form the full name
      String fullName = firstName + " " + lastName;
    %>

    <!-- Display the full name in the HTML response -->
    <p><strong>First Name:</strong> <%= firstName %></p>
    <p><strong>Last Name:</strong>  <%= lastName  %></p>
    <p><strong>Full Name:</strong>  <%= fullName  %></p>

  </body>
</html>

Explanation of Key Elements:

ElementPurpose
<%@ page ... %>Page directive that sets language and encoding for the JSP page.
<% ... %>Scriptlet tag used to write Java code inside JSP.
<%= ... %>Expression tag used to output a Java variable value directly into HTML.
request.getParameter("firstName")Reads the value of the firstName field submitted from the HTML form. This is the same method used in Servlets.
request.getParameter("lastName")Reads the value of the lastName field submitted from the HTML form.

5. Flow of Execution

User fills index.html
        |
        v
Browser sends POST request to displayName.jsp
        |
        v
JSP Container translates JSP to Servlet (internally)
        |
        v
request.getParameter() reads firstName and lastName
        |
        v
Full name is constructed and displayed in browser

Summary

  • JSP is a server-side technology for building dynamic web pages by embedding Java into HTML.
  • It simplifies web development compared to pure Servlets by avoiding excessive out.println() statements.
  • The request.getParameter() implicit method in JSP works exactly like in Servlets to read HTML form data.
  • The HTML form sends data via POST to the JSP file, which processes and displays the full name.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.