Contact Us
   
   
 
   
   
 
     
 
 
JSP Tutorial
 
 
 

JSP Forms and User Input

AddThis Social Bookmark Button          AddThis Feed Button
 
 
Print   Email
 
     
 

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

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

 
 
     
   
  Host 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.
 
 
>> Tomcat Clustering
Clustering is technique to improve the performance, high scalability, and failover of web-server or any servers. Number of users increase with increase load on the servers
 
     
 
>> Implementation of tomcat clustering
Simple steps to do tomcat clustering with code
 
     
 
>> Tomcat Installation & Configuration
Tomcat installation on windows, environment system variables setting in Visual mode for easy installation and configuration of web.xml other servlet invoker setting
 
     
 
>> Apache Tomcat integration with mod_jk
Integrate Apache web server with tomcat web server with mod_jk
 
     
 
>> Java Servlet Examples
Learn first servlet by example
 
     
 
>> Apache installation
Apache installation on windows and linux
 
     
 
>> Mysql Installation
Mysql installation on windows, Port setting,installing mysql tools query browser, connecting to database,creating database, creating tables
 
     
 
>> Apache PHP MySql integration
 
 
     
     
     
     
     
 
Powered by Web4Bharat   Privacy Policy