You are on page 1of 4

//mvc example1

files required
---------------
1)register.html
2)RegServlet.java
3)RegModel.java
4)DBUtil.java
5)result.jsp
6)web.xml
7)student table in mysql

1)register.html
---------------
<html>
<body bgcolor="silver">
<center>
<h1>Reg is here </h1>
<h2>Student Registration</h2>
<form action="/change/as/per/ur/req/reg.do" method="post">
<table bgcolor="red">
<tr>
<td>First Name</td>
<td><input type="text" name="fn"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="ln"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="em"></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="ph"></td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="un"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pw"></td>
</tr>
<tr>
<td colspan=2 align="center"><input type="submit" value=" Register "></td>
</tr>
</table>
</form>
</body>
</html>
2)RegServlet.java
-------------------
package pkg;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RegServlet extends HttpServlet
{
String fn,ln,em,ph,un,pw;
public void doPost(HttpServletRequest req,HttpServletResponse res)throws IOExcep
tion,ServletException
{
//collect data
fn=req.getParameter("fn");
ln=req.getParameter("ln");
em=req.getParameter("em");
ph=req.getParameter("ph");
un=req.getParameter("un");
pw=req.getParameter("pw");
//call model
RegModel rm=new RegModel();
int x=rm.insertStudent(fn,ln,em,ph,un,pw);
String msg=null;
if(x==1)
{
msg="Registration completed Successfully";
}
else
{
msg="Registration not completed ";
}
req.setAttribute("msg",msg);
req.setAttribute("fn",fn);
req.setAttribute("ln",ln);
RequestDispatcher rd=req.getRequestDispatcher("/result.jsp");
rd.forward(req,res);
}
}

3)RegModel.java
----------------
package com.guru;
import java.sql.*;
public class RegModel
{
PreparedStatement ps=null;
Connection con=null;
public int insertStudent(String fn,String ln,String em,String ph,String un,Strin
g pw)
{
int x=0;
try{
con=DBUtil.getDBConnection();
ps=con.prepareStatement("insert into student values(?,?,?,?,?,?)");
ps.setString(1,fn);
ps.setString(2,ln);
ps.setString(3,em);
ps.setString(4,ph);
ps.setString(5,un);
ps.setString(6,pw);
x=ps.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
DBUtil.cleanup(con,ps);
}
return x;
}
}
4)DBUtil.java
--------------
package com.guru;
import java.sql.*;
public class DBUtil
{
public static Connection getDBConnection()
{
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/b8db","root","password")
;
}catch(Exception e)
{
e.printStackTrace();
}
return con;
}
public static void cleanup(Connection con,Statement s)
{
try{
if(con!=null)
{
con.close();
}
if(s!=null)
{
s.close();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
4)result.jsp
-------------
<html>
<body bgcolor="silver">
<center>
<h1>type as u like</h1>
<h2>Student Registration</h2>
<font size="6" color="red">
<%
String msg=request.getAttribute("msg").toString();
String fn=request.getAttribute("fn").toString();
String ln=request.getAttribute("ln").toString();
out.println("hello !.."+fn + "."+ln+<br>);
out.println(msg);
%>
</body>
</html>
5)web.xml
---------
<servlet>
<servlet-name>reg</servlet-name>
<servlet-class>com.javasree.student.RegServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>reg</servlet-name>
<url-pattern>/reg.do</url-pattern>
</servlet-mapping>

6)student table in mysql


------------------------
drop table student;
create table student
(
fname char(15),
lname char(15),
email char(15),
phone char(15),
uname char(15),
pword char(15));

You might also like