|
JSP form is a way to interact with user input with web server or database server. In this, HTML form tag (e.g <input type=”text” name=”name”>, <input type=”password” name=”name”>, <input type=”radio” name=”name”>, <input type=”checkbox” name=”name”>) is used and values inside this form tag can be retrieved by request objects of JSP engine.
JSP Example of input text form
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<html>
<body>
<form name="frm" method="get" action="textInput.jsp">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%"> </td>
<td width="78%"> </td>
</tr>
<tr>
<td>Name Student </td>
<td><input type="text" name="studentName"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
Get these form value in JSP
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%
String studentNameInJSP=request.getParameter("studentName");
%>
<html>
<body>
Value of input text box in JSP : <%=studentNameInJSP%>
</body>
</html>
html.jsp is html form having input text box. This input box take input from user and send to action=”textInput.jsp” file. In textInput.jsp file, retrieved value from studentName form field with request.getParameter of JSP. Remember name of input text box should be same when fetching request parameter, otherwise it will give null value.
<input type="text" name="studentName">
String studentNameInJSP=request.getParameter("studentName");
<form name="frm" method="get" action="textInput.jsp">
In this form, method using get. Get work in query string and display in address bar of browser e.g
http://localhost:8080/ textInput.jsp?studentName=Johny&submit=Submit
Second one is method=”post”
<form name="frm" method="post" action="textInput.jsp">
When using post method, user can not see what string variable is passing to server. In, Address bar we will see only
http://localhost:8080/ textInput.jsp
JSP Example of radio button form
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<html>
<body>
<form name="frm" method="get" action="radioInput.jsp">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%"> </td>
<td width="78%"> </td>
</tr>
<tr>
<td>Active <input type="radio" name="active" value="Active"></td>
<td>DeActive <input type="radio" name="active" value="DeActive"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
radioInput.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%
String radioInJSP=request.getParameter("active");
%>
<html>
<body>
Value of radio button box in JSP : <%=radioInJSP%>
</body>
</html>
JSP Example of checkbox button in form
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<html>
<body>
<form name="frm" method="get" action="checkboxInput.jsp">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%"> </td>
<td width="78%"> </td>
</tr>
<tr>
<td>State <input type="checkbox" name="state" value="state"></td>
<td>City <input type="checkbox" name="city" value="city"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
checkboxInput.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%
String checkboxStateInJSP=request.getParameter("state");
String checkboxCityInJSP=request.getParameter("city");
%>
<html>
<body>
Value of checkbox in JSP : <%=checkboxStateInJSP%> <%=checkboxCityInJSP%>
</body>
</html>
|