<<!DOCTYPE
HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
<head>
<h1
align="center"
style="color:red;"><u>
Registration form </u></h1>
<script
type="text/javascript">
function
validateForm()
{
var
p=document.forms["myForm"]["id"].value;
if
(p==null
|| p=="")
{
alert("Id
must be filled out");
document.myForm.id.focus();
return
false;
}
var
x=document.forms["myForm"]["firstname"].value;
if
(x==null
|| x=="")
{
alert("First
name must be filled out");
document.myForm.firstname.focus();
return
false;
}
var
y=document.forms["myForm"]["fathersname"].value;
if
(y==null
|| y=="")
{
alert("Fathers
name must be filled out");
document.myForm.fathersname.focus();
return
false;
}
var
a=document.forms["myForm"]["age"].value;
if
(a==null
|| a=="")
{
alert("age
must be filled out");
document.myForm.age.focus();
return
false;
}
var
c=document.forms["myForm"]["email"].value;
var
atpos=c.indexOf("@");
var
dotpos=c.lastIndexOf(".");
if
(atpos<1 || dotpos<atpos+2 || dotpos+2>=c.length)
{
alert("Not
a valid e-mail address");
document.myForm.email.focus();
return
false;
}
var
d=document.forms["myForm"]["phonenum"].value;
if
(d==null
|| d=="")
{
alert("phone
number must be filled out");
document.myForm.phonenum.focus();
return
false;
}
var
f=document.forms["myForm"]["qualification"].value;
if
(f==null
|| f=="")
{
alert("Language
must be filled out");
document.myForm.qualification.focus();
return
false;
}
}
</script>
</head>
<body
style="background-color:skyblue;">
<form
name="myForm"
onsubmit="return validateForm()"
action=RegistrationServlet method="post">
<table
align="center">
<tr><td>
Id:</td><td><input type="text"
name="id"
/><br /></td></tr>
<tr><td>
First name:</td><td><input type="text"
name="firstname"
/><br /></td></tr>
<tr><td>
Fathersname:</td><td> <input type="text"
name="fathersname"
/><br/></td></tr>
<tr><td>
Age:</td><td><input type="text"
name="age"
/></br></td></tr>
<tr><td>
Gender:</td><td></br>
<input
type="radio"
name="sex"
value="male"
/> Male<br />
<input
type="radio"
name="sex"
value="female"
/> Female</br></td></tr>
<tr><td>
Emailid:</td><td><input type="text"
name="email"/><br
/></td></tr>
<tr><td>
Phone
Number:</td><td><input type="text"
name="phonenum"/><br
/></td></tr>
<tr><td>
Qualification:</td><td> <select name="qualification"/>
<option
value="Btech">Btech</option>
<option
value="Mtech">Mtech</option>
<option
value="MCA">MCA</option>
<option
value="MBA">MBA</option>
</select></br></td></tr>
<tr><td>
<input type="Submit"
value="submit"
name="s1"
>
<input type="reset"
value="reset"
/></td>
<td> <input
type="Submit"
value="insert"
name="s1">
<input
type="Submit"
value="update"
name="s1"></td>
<td> <input
type="Submit"
value="delete"
name="s1">
</td></tr>
</form>
</body>
</html>
RegistrationServlet.java
package
com;
import
java.io.*;
import
java.net.*;
import
java.sql.*;
import
javax.servlet.*;
import
javax.servlet.http.*;
/**
*
* @author
mkarthik
* @version
*/
public
class
RegistrationServlet extends
HttpServlet {
protected
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException{
response.setContentType("text/html;charset=UTF-8");
try
{
PrintWriter out =
response.getWriter();
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con =
DriverManager.getConnection("jdbc:sqlserver://sql:1433;databaseName=karthik","karthik","karthik");
int
id=Integer.parseInt(request.getParameter("id"));
String
firstname=request.getParameter("firstname");
String
fathersname=request.getParameter("fathersname");
int
age=Integer.parseInt(request.getParameter("age"));
String
sex=request.getParameter("sex");
String
email=request.getParameter("email");
String
phonenum=request.getParameter("phonenum");
String
qualification=request.getParameter("qualification");
String
s2=request.getParameter("s1");
if(s2.equals("insert")){
PreparedStatement
pstmt = con.prepareStatement("insert
into register values(?,?,?,?,?,?,?,?)");
pstmt.setInt(1,id);
pstmt.setString(2,firstname);
pstmt.setString(3,fathersname);
pstmt.setInt(4,age);
pstmt.setString(5,sex);
pstmt.setString(6,email);
pstmt.setString(7,phonenum);
pstmt.setString(8,qualification);
int
a=pstmt.executeUpdate();
if(a>0)
{
out.println("Values
Are Inserted");
} else
{
out.println("Values
Are not Inserted");
}
} else
if(s2.equals("delete")){
PreparedStatement
pst = con.prepareStatement("delete
from register where id =?");
pst.setInt(1,id);
pst.executeUpdate();
out.println("<body><h3>deleted
with id " + id +
"</h3></body></html>");
} else
if(s2.equals("update")){
String query =
"update register set
id='"+id+"',firstname='"+firstname+"',fathersname='"+fathersname+"',age='"+age+"',sex='"+sex+"',email='"+email+"',phonenum='"+phonenum+"',qualification='"+qualification+"'
where id="+id;
Statement stmt =
con.createStatement();
int
i = stmt.executeUpdate(query);
//out.println("query"
+ query);
out.println("update
successfully");
}
out.println("<html>");
out.println("<head>");
out.println("<title>UserInfo</title>");
out.println("</head>");
out.println("<body>");
ResultSet rs;
out.println("<form>");
out.println("<table
border='1' cellspacing='3' cellpadding='2'>");
out.println("<tr><td>
ID </td> <td>FIRSTNAME</td>
<td>FATHERSNAME</td><td>AGE</td><td>GENDER</td><td>EMAILID</td><td>PHONENUM</td><td>QUALIFICATION</td></tr>");
PreparedStatement
pstmt = con.prepareStatement("select
* from register");
ResultSet rs1 =
pstmt.executeQuery();
while
(rs1.next()) {
out.println("<tr><td>"
+ rs1.getString(1) + "</td><td>"
+ rs1.getString(2) + "</td><td>"
+ rs1.getString(3) + "</td><td>"
+ rs1.getString(4) + "</td><td>"
+ rs1.getString(5) + "</td><td>"
+ rs1.getString(6) + "</td><td>"
+ rs1.getString(7) + "</td><td>"
+ rs1.getString(8) + "</td></tr>");
}
out.println("</table>");
out.println("</form>");
out.println("<form
name='f2' method='post' action=RegistrationServlet >");
out.println("<br><br><br>");
out.println("<br><br><br>");
out.println("<br><br>");
out.println("<form
name='s' action='RegistrationServlet' method=post>");
out.println("<br>Enter
ID : <input type='text' value=''name='search1'/> ");
out.println("<input
type='hidden' value='" + id +
"'name='id'/> ");
out.println("<input
type='hidden' value='" + age +
"'name='age'/> ");
out.println("<br><br>");
String s3 =
request.getParameter("search1");
out.println("Click
Here For Delete: <input type ='submit' value='Delete'
name='s1'/>");
out.println("Click
Here For Delete: <input type ='submit' value='Update1'
name='s1'/>");
out.println("<br>");
out.println("Click
Here For Search Details :<input type ='submit' value='Search
Details' name='s1'/>");
out.println("</form>");
if
(s2.equals("Delete"))
{
PreparedStatement
pst2 = con.prepareStatement("delete
from register where ID =?");
pst2.setString(1,
s3);
pst2.executeUpdate();
} //
out.println("Click Here For Search Details :<input type
='submit' value='Search Details' name='search'/>");
else
if
(s2.equals("Search Details"))
{
//out.println("Click
Here For Search Details :<input type ='submit' value='Search
Details' name='search'/>");
out.println("<table
border='1' cellspacing='3' cellpadding='2'>");
String s23 =
request.getParameter("search1");
out.println("<tr><td>
ID </td> <td>FIRSTNAME</td>
<td>FATHERSNAME</td><td>AGE</td><td>GENDER</td><td>EMAILID</td><td>PHONENUM</td><td>QUALIFICATION</td></tr>");
PreparedStatement
pst1 = con.prepareStatement("select
* from register where ID=? ");
pst1.setString(1,
s23);
//System.out.println("hello");
rs =
pst1.executeQuery();
while
(rs.next()) {
out.println("<tr><td>"
+ rs.getString(1) + "</td><td>"
+ rs.getString(2) + "</td><td>"
+ rs.getString(3) + "</td><td>"
+ rs.getString(4) + "</td><td>"
+ rs.getString(5) + "</td><td>"
+ rs.getString(6) + "</td><td>"
+ rs.getString(7) + "</td><td>"
+ rs.getString(8) + "</td></tr>");
}
} else
if
(s2.equals("Update1"))
{
PreparedStatement
pst2 = con.prepareStatement("select
* from register where ID =?");
pst2.setString(1,
s3);
rs1 =
pst2.executeQuery();
if
(rs1.next()) {
out.println("
<form name=myForm method=post action=RegistrationServlet>");
out.println("<table
bgcolor=skyblue align=center cellspacing=1 cellpadding=0 >");
out.println("
<tr><td> ID </td><td><input type=text
name=id value='" + rs1.getInt(1) +
"' readonly /><br
/></td></tr>");
out.println("<tr><td>
First name:</td><td> <input type=text name=firstname
value='" + rs1.getString(2) +
"'/><br /></td></tr>");
out.println("<tr><td>
Fathersname:</td><td> <input type=text
name=fathersname value='" +
rs1.getString(3) + "'/><br/></td></tr>");
out.println("<tr><td>
Age:</td><td> <input type=text name=age value='"
+ rs1.getString(4) + "'/></br></td></tr>");
out.println("<tr><td>
Gender:</td><td>");
if(rs1.getString(5).equals("male"))
{
out.println("<input
type=radio name=sex value='male' checked /> Male");
out.println("
<input type=radio name=sex value='female' />
Female</td></tr>");
} else{
out.println("<input
type=radio name=sex value='male' /> Male");
out.println("
<input type=radio name=sex value='female' checked/>
Female</td></tr>");
}
out.println("<tr><td>
Emailid: </td><td><input type=text name=email
value='" + rs1.getString(6) +
"'/><br /></td></tr>");
out.println("
<tr><td>Phone Number:</td><td> <input
type=text name=phonenum value='" +
rs1.getString(7) + "'/><br
/></td></tr>");
out.println("
<tr><td> Qualification :</td><td>");
if(rs1.getString(8).equals("Btech"))
{
out.println("
<select name=qualification><option
value=-1/>Btech</option><option value='Btech'
select/>Btech</option> ");
out.println("
<option value='Mtech'>Mtech</option> ");
out.println("
<option value='MCA'>MCA</option> ");
out.println("
<option value='MBA'>MBA</option></select>");
} else
if(rs1.getString(8).equals("Mtech"))
{
out.println("<select
name=qualification><option value=-1/>Mtech</option><option
value='Mtech' select/>Mtech</option> ");
out.println("
<option value='Btech' checked/>Mtech</option>");
out.println("
<option value='MCA'>MCA</option> ");
out.println("
<option value='MBA'>MBA</option></select>");
} else
if(rs1.getString(8).equals("MCA"))
{
out.println("
<select name=qualification><option
value=-1/>MCA</option><option value='MCA'
select/>MCA</option> ");
out.println("
<option value='Mtech' checked/>Mtech</option> ");
out.println("
<option value='Btech'>Btech</option> ");
out.println("
<option value='MBA'>MBA</option></select>");
} else
if(rs1.getString(8).equals("MBA"))
{
out.println("
<select name=qualification><option
value=-1/>MBA</option><option value='MBA'
select/>MBA</option> ");
out.println("
<option value='Mtech' checked/>Mtech</option> ");
out.println("
<option value='MCA'>MCA</option> ");
out.println("
<option value='Btech'>Btech</option></select>");
}
//out.println("</select></br></td></tr>");
out.println("<tr><td><br></td><td<br></td></tr>");
out.println("<tr><td></td><td>");
out.println("<input
type=reset value=Reset />");
out.println("
<input type=Submit value=insert name=s1>");
out.println("<input
type=Submit value=update name=s1>");
out.println("<input
type=Submit value=delete name=s1> </td></tr></table>");
}
}
out.println("</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
} catch
(Exception ex) {
ex.printStackTrace();
}
}
protected
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
doPost(request, response);
}
}
where is database content?
ReplyDeleteIn Servlet we given Please go through the code or copy paste and run you will get the proper result
Deleteit's perfect run ....1. create jsp page and copy code
Delete2. create new servlet and copy code .....you are done ...but check on your servlet page doget method which 2 time come and you get error...so carefully copy content to content
And Create Database File also And Change username password
DeleteI'm getting blank page after clicking on insert button...can you please tell me?
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi... i get error HTTP Status 404 - /regjs/html/RegistrationServlet while run above code..
ReplyDeletemy file structure is html(index.html) jsp WEB-INF((classes(RegistrationServlet.java
))(lib)web.xml)
what i have done wrong...help me to resolve this issue..
Thanks in advance
error
DeleteThis code really helps
DeleteIt was very encouraging to see this kind of content. Thank you for sharing.
ReplyDeleteJava training institutes in chennai
java courses
Everything worked fine but I'm not able to search details and delete and update1 it show blank page and in servlet console it says null what I'm missing I don't I gone 2 to 3 times comparing ur code and mine but everything is fine but error is showing as same as null
ReplyDeleteits showing a blank page
ReplyDelete