Contact Us
   
   
 
   
   
 
     
 
 
JSP Tutorial
 
Book Must Read
 
 

JSP Forms and User Input

 
 
Print   Email
 
     
 

JSP form help us to take input from user and send it back to web or application server and on the base of input can process the request e.g. to save data or show data or login

JSP form is a way to interact with user input with web server or database server. In this, HTML form tag
1. <input type=”text” name=”name”>,
2. <input type=”password” name=”name”>,
3. <input type=”radio” name=”name”>,
4. <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

html.jspOutput
 

<%@ 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%">&nbsp;</td>
    <td width="78%">&nbsp;</td>
    </tr>
  <tr>
    <td>Name Student </td>
    <td><input type="text" name="studentName"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" value="Submit"></td>
    </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
</table>
</form>
</body>
</html>

Get these form value in JSP

textInput.jspOutput

 
<%@ 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

radio.jspOutput

 
<%@ 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%">&nbsp;</td>
    <td width="78%">&nbsp;</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>&nbsp;</td>
    <td><input type="submit" name="submit" value="Submit"></td>
    </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</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

checkbox.jsp Output

 
<%@ 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%">&nbsp;</td>
    <td width="78%">&nbsp;</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>&nbsp;</td>
    <td><input type="submit" name="submit" value="Submit"></td>
    </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</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>

 

 

 

 
If You Like this Article, share this

 
 
     
   
  View Forum Post  
 
Optimization of SQL queries for better performance
Example of Between in mysql
Example of sorting in mysql table
Example of join in mysql
Example of subquery in mysql
union join in mysql with example
error too many connection in mysql
calculate second highest salary
How to use select queries with array in mysql
Radio button pre selected in struts
Exception in thread "main" java.lang.NoClassDefFoundError
tree view in jsp
SSL implement in tomcat and java certificate
WARNING: Internal error flushing the buffer in release()
DynaForm validation problem in struts not working
Accessing JavaBean in servlet or servlet in javaBean.
 
     
     
   
     
     
     
 
  Privacy Policy