0% found this document useful (0 votes)
2 views3 pages

HTML Forms in JSP Lecture

This document provides an overview of handling HTML forms in JSP, detailing how to collect user input using form tags and retrieve it with request.getParameter(). It explains how to pass data between JSPs using setAttribute() and getAttribute(), along with examples of a simple greeting form and the associated JSP files. Additionally, it includes teaching tips for demonstrating data flow and the differences between form data and request object data.

Uploaded by

ေမဇင္
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

HTML Forms in JSP Lecture

This document provides an overview of handling HTML forms in JSP, detailing how to collect user input using form tags and retrieve it with request.getParameter(). It explains how to pass data between JSPs using setAttribute() and getAttribute(), along with examples of a simple greeting form and the associated JSP files. Additionally, it includes teaching tips for demonstrating data flow and the differences between form data and request object data.

Uploaded by

ေမဇင္
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Handling HTML Forms in JSP - Teacher Notes

1. What is an HTML Form?

- A way to get user input from a webpage.

- Forms use tags like <form>, <input>, <textarea>, etc.

- Data is sent to server on submit.

2. Getting Form Data: request.getParameter()

- Use in JSP or Servlet to get data from form fields.

- Syntax:

String value = request.getParameter("inputName");

- Example:

<form action="submit.jsp" method="post">

Name: <input type="text" name="userName" />

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

</form>

In submit.jsp:

<%

String name = request.getParameter("userName");

%>

Hello, <%= name %>!

3. Passing Data Between JSPs: setAttribute() and getAttribute()

- Use request.setAttribute("key", value) to store data in request.

- Use request.getAttribute("key") to retrieve it later.

- Commonly used with RequestDispatcher.forward() to pass data when forwarding request.


Handling HTML Forms in JSP - Teacher Notes

Example Servlet or JSP:

String name = request.getParameter("userName");

request.setAttribute("greeting", "Hello, " + name + "!");

RequestDispatcher rd = request.getRequestDispatcher("result.jsp");

rd.forward(request, response);

In result.jsp:

<p><%= request.getAttribute("greeting") %></p>

Mini Lab Exercise: Simple Greeting Form

Files:

1. index.jsp

<html><body>

<form action="process.jsp" method="post">

Enter your name: <input type="text" name="userName" />

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

</form>

</body></html>

2. process.jsp

<%

String name = request.getParameter("userName");

request.setAttribute("greeting", "Hello, " + name + "! Welcome to JSP.");


Handling HTML Forms in JSP - Teacher Notes

RequestDispatcher rd = request.getRequestDispatcher("result.jsp");

rd.forward(request, response);

%>

3. result.jsp

<html><body>

<h2>Greeting Result</h2>

<p><%= request.getAttribute("greeting") %></p>

<a href="index.jsp">Back</a>

</body></html>

Teaching Tips

- Let students try submitting different names.

- Show how the data flows from form to JSP and then forwarded with the greeting.

- Explain request lifecycle and why attributes exist only in the request scope.

- Discuss difference between getParameter() (form data) and getAttribute() (request object data).

You might also like