Gadgets

Showing posts with label Pass the Values to next jsp page via Hyperlink. Show all posts
Showing posts with label Pass the Values to next jsp page via Hyperlink. Show all posts

Friday, 12 September 2014

Pass the Values via Hyperlink



Pass the Values to next jsp page via Hyperlink:
We can pass the values from one jsp page to other jsp page using Hyperlink
In this process first we get values from database and set that value to hyperlink.
tid=rs.getString(“id”);
<a href="edit.jsp?id=<%out.print(tid);%>">Edit Details</a>
“Id” is database column name, that the id value is stored in “tid” variable.
It shows the value while passing to the next jsp page.
Here I done an application with simple programming, it is very easy to understand, especially for beginners.


   
Code:
Here I done actions registration from, display details, update details, delete details.
Registration form (reg_form.jsp)

<h3>Registration Form</h3>
<form action="registration.jsp"method="post"> 
<table>
<tr><td>Id Number :</td><td><input type="text" name="id" required/></td></tr> 
<tr><td>Name :</td><td><input type="text" name="name" required/></td></tr>         
<tr><td>Gender :</td><td>
<input type="radio" name="gender" value="male"    required/>Male
<input type="radio" name="gender" value="female" required/>Female</td></tr>
<tr><td>Birth Date :</td><td><input type="date" name="date"required/></td></tr>
<tr><td>Phone Number :</td><td><input type="tel" name="phno"
 pattern="[0-9]{10}"required /></td></tr>          
<tr><td>Father Name :</td><td><input type="text"name="fathern"required/>
</td></tr>
<tr><td>Address :</td><td><input type="text" name="add"
placeholder="city/town,area"required/></td></tr> 
<tr><td></td><td><input type="submit" value="Signup" name="Signup" /></td></tr>            
</table>        </form>

Registration JSP Servlet (registration.jsp)

<%
try{
String id,name,dob,gender,phno,fathername,add;
id=request.getParameter("id");
name=request.getParameter("name");
gender=request.getParameter("gender");
dob=request.getParameter("date");
phno=request.getParameter("phno");
fathername=request.getParameter("fathern");
add=request.getParameter("add");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/narendar","user","12345");
String q="insert into student  values('"+id+"','"+name+"','"+gender+"','"+dob+"','"+phno+"','"+fathername+"','"+add+"')";
Statement st=con.createStatement();
int i=st.executeUpdate(q);
if(i>0){
out.print("Registration Successful....");
request.getRequestDispatcher("details.jsp").include(request, response);
}
}
catch(Exception e){
out.println(e);
}
%>

Display Details in table (details.jsp)

<h2>User Details.
</h2>
<h3><a href="reg_form.jsp">Add New User</a></h3>
<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/narendar","user","12345");
String q="select * from student";
Statement stmt=con.createStatement();
ResultSet re=stmt.executeQuery(q);
String id,name,gen,dob,phno,fn,add;
%>
<style>
tr:hover{
background-color:  #ddec01;
}
</style>
<table border="1">
<thead>
<tr>
<th>Id Number</th>
<th>Name</th>
<th>Gender</th>
<th>Date Of Birth</th>
<th>Phone Number</th>
<th>Father Name</th>
<th>Address</th>
<th></th>
<th></th>
</tr>
</thead>
<%
while(re.next()){
id=re.getString("id");
name=re.getString("name");
gen=re.getString("gender");
dob=re.getString("birthdate");
phno=re.getString("phno");
fn=re.getString("fathername");
add=re.getString("address");
%>
<tbody>
<tr>
<td><%out.print(id);%></td>
<td><%out.print(name);%></td>
<td><%out.print(gen);%></td>
<td><%out.print(dob);%></td>
<td><%out.print(phno);%></td>
<td><%out.print(fn);%></td>
<td><%out.print(add);%></td>
<td><a href="delete.jsp?id=<%out.print(id);%>">Delete</a></td>
<td><a href="edit.jsp?id=<%out.print(id);%>">Edit Details</a></td>
</tr>
</tbody><%}%>
</table>
<%
}
catch(Exception e){
out.print(e);
}%>

Edit Details (edit.jsp)

<h3>Update User Details</h3>
<%
try{
String id=request.getParameter("id");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/narendar","user","12345");
String q="select * from student where id='"+id+"'";
Statement stmt=con.createStatement();
ResultSet re=stmt.executeQuery(q);
String sid=null,name=null,gen=null,dob=null,phno=null,fn=null,add=null;
while(re.next()){
sid=re.getString("id");
name=re.getString("name");
gen=re.getString("gender");
dob=re.getString("birthdate");
phno=re.getString("phno");
fn=re.getString("fathername");
add=re.getString("address");
}
%>
<form action="update.jsp"method="post">
<table>
<tr><td>Id Number :</td><td><input type="hidden" name="id" value="<%out.print(sid);%>"required/></td></tr>
<tr><td>Name :</td><td><input type="text" name="name" value="<%out.print(name);%>"required/></td></tr>          
<tr><td>Gender :</td><td><input type="radio" name="gender" value="male" required/>Male
<input type="radio" name="gender" value="female" required/>Female</td></tr>
<tr><td>Birth Date :</td><td><input type="date" name="date"value="<%out.print(dob);%>"required/></td></tr>
<tr><td>Phone Number :</td><td><input type="tel" name="phno"pattern="[0-9]{10}"value="<%out.print(phno);%>"required /></td></tr>          
<tr><td>Father Name :</td><td><input type="text" name="fathern"value="<%out.print(fn);%>"required/></td></tr>
<tr><td>Address :</td><td><input type="text" name="add"value="<%out.print(add);%>"/></td></tr>
<tr><td></td><td><input type="submit" value="Signup" name="Signup" /></td></tr>
</table>
</form>
<% 
}
catch(Exception e){
System.err.print(e);
}
%>

Update Details (update.jsp)

<%
try{
String id,name,dob,gender,phno,fathername,add;
id=request.getParameter("id");
name=request.getParameter("name");
gender=request.getParameter("gender");
dob=request.getParameter("date");
phno=request.getParameter("phno");
fathername=request.getParameter("fathern");
add=request.getParameter("add");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/narendar","user","12345");
String q="update student set name='"+name+"',gender='"+gender+"',birthdate='"+dob+"',phno='"+phno+"',fathername='"+fathername+"',address='"+add+"' where id='"+id+"'";
Statement st=con.createStatement();
int i=st.executeUpdate(q);
if(i>0){
out.print("User details updated successful....");
request.getRequestDispatcher("details.jsp").include(request, response);
}
}
catch(Exception e){
out.println(e);
}
%>

Delete Details (delete.jsp)

<%
try{
String id=request.getParameter("id");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/narendar","user","12345");
String q1="delete from student where id='"+id+"'";
Statement stmt1=con.createStatement();
int i=stmt1.executeUpdate(q1);

System.out.print("User Details Deleted Successful....");
request.getRequestDispatcher("details.jsp").include(request, response);
}
catch(Exception e){
System.err.print(e);
}                
%>