Sample Code
Sample Code
Registration.jsp
<%
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<!--
-->
<script language="JavaScript" src="<%=request.getContextPath()+"/js/gen_validatorv31.js"%>"
type="text/javascript"></script>
<script>
function goOn() {
var port=document.register.port.value;
var host=document.register.host.value;
var userName=document.register.userName.value;
window.location.href='http://'+host+':'+port+'/OnlineCollegeMagazine/CheckUserAction?userName='+us
erName+"&path=./registration.jsp";
;
</script>
<body>
<jsp:include page="header.jsp"></jsp:include>
<br />
<center>
<h3>
</h3>
</center>
onSubmit="return validate()">
<!--<table border="1"><tr><td></td></tr></table>-->
<br />
Account Details
</th>
<tr></tr>
<tr></tr>
<tr>
<td align='right'>
User Name :
</td>
<td width="303">
value="<%
if(request.getParameter("userName")!=null)
out.print(request.getParameter("userName")); %>"
<td align='left'>
test="${'requestScope.status1'!='null'}">
<c:out
value="${param.status1}"></c:out>
</c:if> </font>
</b>
</td>
<td></td><td></td>
<td></td>
</tr>
<tr>
<td align='right'>
PassWord :
</td>
<td>
onkeyup="testPassword(document.forms.register.password.value);"
onchange="Encrypt(document.forms.register.password.value);" />
</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td align='right'>
</td>
<td>
<tr>
<td height=15 bgcolor=red></td>
</tr>
</table></center>
</td>
<td></td>
</tr>
<tr>
<td align='right'>
Confirm :
</td>
<td>
onBlur="checkconformpassword()" />
</td>
</td>
</tr>
<tr>
<td align='right'>
User Type :
</td>
<td width="276">
</option>
<option value="student">
<font size="3"
face="Verdana">Student</font>
</option>
<option value="faculty">
<font size="3"
face="Verdana">Faculty</font>
</option>
<option value="moderator">
<font size="3"
face="Verdana">Moderator</font>
</option>
</select>
</td>
<tr>
<td align='right'>
SecurityQuestion :
</td>
<td>
<select name="squest">
</option>
<option value="What is your favorite
pastime?">
pastime?</font>
</option>
</option>
first school?</font>
</option>
spouse?</font>
</option>
sports team?</font>
</option>
name?</font>
</option>
mascot?</font>
</option>
car or bike?</font>
</option>
</option>
</select>
</td>
</tr>
<tr>
onClick="check(register)" />
Own Question :
</td>
</tr>
<tr>
<td>
<td>
</td>
</tr>
<tr>
<td align='right'>
Security Answer :
</td>
<td>
</td>
</tr>
<tr></tr>
<tr></tr>
Personal Details
</th>
<tr></tr>
<tr></tr>
<tr>
<td align='right'>
First Name :
</td>
<td width="276">
<td align='right'>
Gender :
</td>
<td>
<select name="gender">
</option>
<option value="Male">
<font size="3"
face="Verdana">Male</font>
</option>
<option value="Female">
<font size="3"
face="Verdana">Female</font>
</option>
</select>
</td>
</tr>
<tr>
<td align='right'>
Last Name :
</td>
<td width="276">
</td>
<td align='right'>
Email :
</td>
<td>
</td>
</tr>
<tr>
<td align='right'>
Birth Date :
</td>
<td>
readonly="readonly" />
<a
href="javascript:show_calendar('document.register.birthdate', document.register.birthdate.value);">
<img src="<%=request.getContextPath()+"/images/cal.gif"%>" alt="a" width="18" height="18"
border="0"/></a>
</td>
<td align='right'>
Fax No :
</td>
<td>
</td>
</tr>
<tr>
<td align='right'>
Browse Photo :
</td>
<td>
onChange="preview(this)" />
</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr></tr>
<tr></tr>
<center>
Contact Details
</center>
</th>
<tr></tr>
<tr></tr>
<tr>
<td align='right'>
Address Type :
</td>
<td width="276">
</option>
<option value="home">
<font size="3"
face="Verdana">Home</font>
</option>
<option value="office">
<font size="3"
face="Verdana">Office</font>
</option>
<option value="personal">
<font size="3"
face="Verdana">Personal</font>
</option>
</select>
</td>
City :
</td>
<td width="273">
</td>
</tr>
<tr>
<td align='right'>
House No :
</td>
<td>
</td>
</tr>
<tr>
<td align='right'>
Street :
</td>
<td>
</td>
<td align='right'>
State :
</td>
<td>
</td>
</tr>
<tr>
<td align='right'>
Country :
</td>
<td>
</td>
</tr>
<tr>
<td align='right'>
Phone No :
</td>
<td>
onBlur="ValidateForm()" />
</td>
<td align='right'>
Pin :
</td>
<td>
onChange="showStatus()" />
</td>
</tr>
<th colspan="5">
</th>
<tr></tr>
<tr></tr>
<tr>
<td></td>
<td align="right">
name="register" value="Register"
/> </font>
</td>
<td align="left">
</td>
<td></td>
</tr>
</table>
<p>
<br />
<br />
<br />
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
<br />
</p>
</form>
//You should create the validator only after the definition of the HTML form
frmvalidator.addValidation("gender","dontselect=0");
frmvalidator.addValidation("email","maxlen=50");
frmvalidator.addValidation("email","req");
frmvalidator.addValidation("email","email");
frmvalidator.addValidation("addressType","dontselect=0");
frmvalidator.addValidation("phoneType","dontselect=0");
frmvalidator.addValidation("phoneNo","req");
frmvalidator.addValidation("phoneNo","maxlen=50");
frmvalidator.addValidation("phoneNo","numeric");
frmvalidator.addValidation("phoneNo","Phone");
frmvalidator.addValidation("squest","dontselect=0");
</script>
<br />
<br />
<jsp:include page="footer.jsp"></jsp:include>
</body>
</html>
Profilebean.java
package com.collegemagazine.bean;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import com.collegemagazine.formbeans.profileformbean;
import com.sun.org.apache.commons.beanutils.BeanUtils;
public ProfileTo() {
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
RegisterDAOImpl.java
package com.collegemagazine.daoImpl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.Date;
import java.util.Properties;
import oracle.jdbc.driver.OracleCallableStatement;
import oracle.jdbc.driver.OracleTypes;
import com.collegemagazine.daoI.ProfileDAOI;
import com.collegemagazine.dbutil.DBConnectionFactory;
import com.collegemagazine.formbeans.profileformbean;
import com.collegemagazine.util.Corehash;
import com.collegemagazine.util.DateWrapper;
import com.collegemagazine.util.LoggerManager;
public class ProfileDAOImpl implements ProfileDAOI {
public Connection con;
private boolean flag;
/** Creates a new instance of ProfileDAO */
public ProfileDAOImpl(){
con=DBConnectionFactory.getConnection();
}
//User Registration
public boolean changePass(profileformbean pro) {
// TODO Auto-generated method stub
return false;
}
public boolean changeQuestion(profileformbean pro) {
// TODO Auto-generated method stub
return false;
}
public String checkUser(String userName) {
String user=null;
System.out.println("username"+userName);
try
{
con.setAutoCommit(true);
CallableStatement cstmt=con.prepareCall("{ call loginidavailablity(?,?) }");
cstmt.setString(1, userName);
cstmt.registerOutParameter(2,Types.VARCHAR);
cstmt.execute();
user=cstmt.getString(2);
}
catch (SQLException ex)
{ex.printStackTrace();
LoggerManager.writeLogSevere(ex);
user=null;
}
catch (Exception e)
{
LoggerManager.writeLogSevere(e);
user=null;
}
finally
{
try
{
con.close();
}
catch(SQLException se)
{
LoggerManager.writeLogWarning(se);
}
catch(Exception e)
{
LoggerManager.writeLogWarning(e);
}
}
return user;
}
public boolean insertNewUser(profileformbean pro) throws FileNotFoundException{
boolean flag=false;
String firstname = pro.getFirstName();
System.out.println("fname:"+firstname);
String lastname = pro.getLastName();
System.out.println("lname:"+lastname);
String birthdate = DateWrapper.parseDate(pro.getBirthdate());
System.out.println("dob:"+birthdate);
String squest = pro.getSquest();
System.out.println("question:"+squest);
if(squest.equals("")){
squest = pro.getOwnquest();
System.out.println(squest);}
String sqansw = pro.getSecrete();
System.out.println("answer:"+sqansw);
String fax = pro.getFax();
System.out.println("fax:"+fax);
String email = pro.getEmail();
System.out.println("email:"+email);
String gender = pro.getGender();
System.out.println("gender:"+gender);
String addresstype = pro.getAddressType();
System.out.println("addresstype is:"+addresstype);
String houseno = pro.getHouseNo();
System.out.println("house no :"+houseno);
String street = pro.getStreet();
System.out.println("street:"+street);
String city = pro.getCity();
System.out.println("city :"+city);
String state=pro.getState();
System.out.println("state:"+state);
String country=pro.getCountry();
System.out.println("country:"+country);
String pin = pro.getPin();
System.out.println("pin:"+pin);
String phoneno = pro.getPhoneNo();
System.out.println("phone is"+phoneno);
String logintype = pro.getLoginType();
System.out.println("usertype is :"+logintype);
String username = pro.getUserName();
System.out.println("user is :"+username);
String password = pro.getPassword();
System.out.println("password is:"+password);
String photo= pro.getPhoto();
try {
System.out.println("photo=" + photo);
File f = new File(photo);
FileInputStream fis = new FileInputStream(f);
System.out.println("fole=" + f.length());
CallableStatement cstmt = con
.prepareCall("{call
insertprocedure(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
cstmt.setBinaryStream(1, fis, (int) f.length());
cstmt.setString(2, firstname);
cstmt.setString(3, lastname);
cstmt.setString(4, birthdate);
cstmt.setString(5, squest);
cstmt.setString(6, sqansw);
cstmt.setString(7, email);
cstmt.setString(8, fax);
cstmt.setString(9, gender);
cstmt.setString(10, username);
cstmt.setString(11, password);
cstmt.setString(12, logintype);
cstmt.setString(13, addresstype);
cstmt.setString(14, houseno);
cstmt.setString(15, street);
cstmt.setString(16, city);
cstmt.setString(17, state);
cstmt.setString(18, country);
cstmt.setString(19, pin);
cstmt.setString(20, phoneno);
int i = cstmt.executeUpdate();
if (i >= 0) {
flag = true;
} else {
flag = false;
}
con.close();
} catch (SQLException e) {
System.out.println(e.toString());
if (e
.toString()
.equalsIgnoreCase(
"java.sql.SQLException: [Microsoft][ODBC driver for
Oracle][Oracle]ORA-12571: TNS:packet writer failure")) {
flag = true;
System.out.println("n===" + flag);
}
System.out.println(e);
} catch (Exception e) {
e.printStackTrace();
flag = false;
try {
con.rollback();
} catch (SQLException se) {
se.printStackTrace();
}
}
return flag;
}
//Getting profile
public profileformbean getProfile(String loginname,String path)
{
profileformbean rb=new profileformbean();
try
{
CallableStatement cs=con.prepareCall("{call showprofile(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
cs.setString(1,loginname);
cs.registerOutParameter(2,Types.VARCHAR);
cs.registerOutParameter(3,Types.VARCHAR);
cs.registerOutParameter(4,Types.VARCHAR);
cs.registerOutParameter(5,Types.VARCHAR);
cs.registerOutParameter(6,Types.VARCHAR);
cs.registerOutParameter(7,Types.VARCHAR);
cs.registerOutParameter(8,Types.VARCHAR);
cs.registerOutParameter(9,Types.VARCHAR);
cs.registerOutParameter(10,Types.VARCHAR);
cs.registerOutParameter(11,Types.VARCHAR);
cs.registerOutParameter(12,Types.VARCHAR);
cs.registerOutParameter(13,Types.VARCHAR);
cs.registerOutParameter(14,Types.VARCHAR);
cs.registerOutParameter(15,Types.VARCHAR);
cs.registerOutParameter(16,Types.BLOB);
cs.execute();
rb.setPassword(cs.getString(2));
rb.setFirstName(cs.getString(3));
rb.setLastName(cs.getString(4));
rb.setBirthdate(cs.getString(5));
//rb.setPhoto(cs.getString());
rb.setEmail(cs.getString(6));
rb.setFax(cs.getString(7));
//rb.setHome(cs.getString(8));
rb.setHouseNo(cs.getString(9));
rb.setStreet(cs.getString(10));
rb.setCity(cs.getString(11));
rb.setState(cs.getString(12));
//rb.setPin(cs.getString(13));
rb.setCountry(cs.getString(13));
rb.setPin(cs.getString(14));
rb.setPhoneNo(cs.getString(15));
Blob b =cs.getBlob(16);
byte b1[]=b.getBytes(1,(int)b.length());
OutputStream fout=new FileOutputStream(path+"/"+loginname+"+.jpg");
fout.write(b1);
}
catch(Exception e)
{e.printStackTrace();
LoggerManager.writeLogSevere(e);
}
finally
{
try{
con.close();
}catch(Exception e)
{
LoggerManager.writeLogSevere(e);
}
}
return rb;
}
}
RegisterAction.java
package com.collegemagazine.action;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.org.apache.commons.beanutils.BeanUtils;
import com.collegemagazine.delegate.ProfileMgrDelegate;
import com.collegemagazine.formbeans.profileformbean;
import com.collegemagazine.util.UtilConstants;
/**
*/
/**
* This method is called when a form has its tag value method equals to
* post.
* @param request
* @param response
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
RequestDispatcher rd = null;
try {
BeanUtils.populate(pf, map);
e1.printStackTrace();
e1.printStackTrace();
try {
if (flag) {
path = UtilConstants._STATUS;
request.setAttribute("status",
UtilConstants._REGISTERED_SUCCESS);
} else {
path = UtilConstants._STATUS;
request.setAttribute("status", UtilConstants._REGISTERED_FAIL);
} catch (Exception e) {
e.printStackTrace();
path = UtilConstants._STATUS;
request.setAttribute("status", UtilConstants._INVALID_ENTRIES);
rd = request.getRequestDispatcher(path);
rd.forward(request, response);