Registration form in Servlet
STEPS WRITTEN BY ITSELF FOR PRACTICAL FOR EASY UNDERSTANDING OF SERVLET
1) install tomcat 6.0
2) install sql server 10g
3)make database (product) in sql server and create table student
4) insert records in student table
5) set environment variables in my computer
variable name=>classpath
variable value=>C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.JAR;.;
5) set environment variables in my computer
variable name=>classpath
variable value=>C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.JAR;.;
6)set environment variables in my computer for tomcat
variable name=>JAVA_HOME
variable_value=>C:\Program Files (x86)\Java\jdk1.6.0_18
7)create folder in tomcat6.0 =>webapps=>registeruser=>WEB-INF and reg.html =>classes , lib and reg.xml
8)paste ojdbc.jar file in lib and paste reg.java , reg.class file after compile in classes folder
9)open url
TYPE THE
===> Localhost:9999/registeruser/
THE REGISTRATION PAGE WILL OPEN
In this example you will see how to develop a registration form in Servlet. To develop a registration form you will need to connect your servlet application with database. Here we are using SQL SERVER 10 G database.
Create a Table in your Database
create table Student ( name varchar2(60), email varchar2(60), pass varchar2(100),
country varchar2(50) )
index.html
<html>
<body bgcolor="green">
<form action="Register" method="get">
<center>
<table>
<tr align="center" ><td colspan="2">Registration Form....</td>.
</tr>
<tr><td>Name:</td><td><input type="text" name="name"/><td>
</tr>
<tr><td>Email:</td><td><input type="text" name="email"/><td>
</tr>
<tr><td>Pass:</td><td><input type="text" name="pass"/><td>
</tr>
<tr><td>Address:</td>
<td><input type="text" name="address"/><td></tr>
<tr><td>Country:</td>
<td>
<select name="country"/>
<option>India</option>
<option>Austrilia</option>
<option>Newzland</option>
<option>Pakistan</option>
<option>Brazil</option>
<option>Neywork</option>
</select></td>
</tr>
<br/>
<br/>
<tr align="center"><td colspan="2">
<input type="submit" value="register"/></td>
</tr>
</table>
</center>
</form>
</body>
</html>
Register.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.sql.*;
public class Reg extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String n=req.getParameter("name");
String e=req.getParameter("email");
String p=req.getParameter("pass");
String a=req.getParameter("address");
String c=req.getParameter("country");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","vision");
PreparedStatement ps=con.prepareStatement("insert into STUDENT values(?,?,?,?,?)");
ps.setString(1,n);
ps.setString(2,e);
ps.setString(3,p);
ps.setString(4,a);
ps.setString(5,c);
int i=ps.executeUpdate();
if(i>0)
{
out.println("You are successfully registered");
}
}
catch (Exception e2) {System.out.println(e2);
}
}
}
web.xml
<web-app> <servlet> <servlet-name>register</servlet-name> <servlet-class>Reg</servlet-class> </servlet> <servlet-mapping> <servlet-name>register</servlet-name> <url-pattern>/Register</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>register.html</welcome-file> </welcome-file-list> </web-app>
No comments:
Post a Comment