One of our clients had an external contacts form on their website that they wanted to populate the database with. So Here’s the code. FIrst of all I created a nice JSF page then in the INIT code I called an enterprise Java bean with the save routine.
Since this is not straightforwards I though I’d share it…
In the init() phase of the Java server page.
HttpServletRequest request = (HttpServletRequest)
FacesContext.getCurrentInstance().
getExternalContext().getRequest();
String sResult="";
Map MapToSend=request.getParameterMap();
sResult=pHJBean.saveContactMap(MapToSend);
msgRESULT.setText(sResult);
if (!sResult.equals("Save Complete")) {
hypRETURN.setText("Click Here to go back and try again");
hypRETURN.setUrl("javascript:history.back()");
}
SOme notes here:-
I had a static text box called msgRESULT which displays the result of calling the “saveContactMap” routine in the bean.
Also I wanted to make sure that if there was a problem I would change a Hyperlink to return the user to the completed page so they could fix it before hitting send again…don’t worry it will make sense below.
In the bean first a simple bit of code to get the Field & Value of a key from one that has been submitted, note that the first element is of a string.
The second bit of code actually does some comparing and saves the contact to the database, but only if it’s passed the verification (since thier web designer didn’t do any!).
/**
* Get a field from a request
* @param request
* @param sField
* @return the value as a string
*/
private String sGetField(Map<String,String[]> request, String sField) {
for(String key: request.keySet()) {
if (key.equals(sField)) return request.get(key)[0];
}
return "";
}
/**
* Saves a contact detail from a web form to the database
* @param Params the Map <String,String[]> of the request
* @return a string result
*/
public String saveContactMap(Map Params) {
String sResult="Save Complete";
String firstname = sGetField(Params,"firstname");
String lastname = sGetField(Params,"lastname");
String email = sGetField(Params,"email");
String telephone = sGetField(Params,"bro_telephone");
String address1=sGetField(Params,"ADDRESS 1");
String address2=sGetField(Params,"ADDRESS 2");
String town=sGetField(Params,"TOWN");
String county=sGetField(Params,"COUNTY");
String postcode=sGetField(Params,"POSTCODE");
if (firstname.equals("")) {
return "Please enter your first name";
}
if (lastname.equals("")) {
return "Please enter your surname";
}
if (address1.equals("") ||
county.equals("") ||
town.equals("") ||
postcode.equals("")) {
return "Please enter your full address";
}
if (telephone.equals("")) {
return "Please enter your phone number";
}
Contacts thisContact = new PHJ.Contacts();
thisContact.setForename(firstname);
thisContact.setSurname(lastname);
thisContact.setEmail(email);
thisContact.setTelno(telephone);
thisContact.setEnquiryorstudent("web enquiry");
thisContact.setAddress1(address1);
thisContact.setAddress2(address2);
thisContact.setTown(town);
thisContact.setCounty(county);
thisContact.setPostcode(postcode);
thisContact.setLastcontact(new Date());
thisContact.setLastenquirydate(new Date());
thisContact.setBeautytherapyqualificationsheld("");
em.persist(thisContact);
em.flush();
return sResult;
}