Har problem med kod. När jag skriver in "Password" och "rPassword" skrivs det ut att jag ej angivet "Password" i input-rutan:
'jspsidan
<%@ page import="ValidatePersonalBean"%>
<jsp:useBean id="form" class="ValidatePersonalBean" scope="request">
<jsp:setProperty name="form" property="errorMessages" value='<%= errorMap %>'/>
</jsp:useBean>
<%
// If process is true, attempt to validate and process the form
if ("true".equals(request.getParameter("process"))) {
%>
<jsp:setProperty name="form" property="*" />
<%
// Attempt to process the form
if (form.process()) {
// Go to success page
response.sendRedirect("formDone.jsp");
return;
}
}
%>
<html>
<head><title>A Simple Form</title></head>
<body>
<%-- When submitting the form, resubmit to this page --%>
<form action='<%= request.getRequestURI() %>' method="POST">
Logon Settings:<br>
<font color=red><%= form.getErrorMessage("email") %></font><br>
Email: <input type="TEXT" name="email" style="font-size : 8pt" value='<%= form.getEmail() %>'>
<br>
<font color=red><%= form.getErrorMessage("Password") %></font><br>
Password: <input type="TEXT" name="Password" value='<%= form.getPassword() %>'>
<br>
<font color=red><%= form.getErrorMessage("rPassword") %></font><br>
Repeat Password: <input type="TEXT" name="rPassword" value='<%= form.getrPassword() %>'>
<p>
Personal Information:<br>
<font color=red><%= form.getErrorMessage("zipcode") %></font><br>
Name: <input type="TEXT" name="name" value='<%= form.getZipcode() %>'>
<br>
<font color=red><%= form.getErrorMessage("zipcode") %></font><br>
Day of birth:
<select name="in_day_birth"><option value="">Day</option><option value="01">01</option><option value="02">02</option></select>*
<select name="in_month_birth"><option value="">Month</option><option value="01">01</option></select>*
<select name="in_year_birth"><option value="">Year</option><option value="2002"></select>*
<br>
<font color=red><%= form.getErrorMessage("zipcode") %></font><br>
City: <input type="TEXT" name="city" value='<%= form.getZipcode() %>'>
<br>
<font color=red><%= form.getErrorMessage("Country") %></font><br>
Country: <select name="country" style="width:250px;">
<option value="" selected>Select your Country</option>
</select>
<br>
<font color=red><%= form.getErrorMessage("Timezone") %></font><br>
Timezone: <SELECT name="timezone" style="width:250px;">
<option value="" selected>Select your (GMT)</option>
<option value="-12">(GMT -12) Eniwetok, Kwajalein</option>
</select>
<br>
Optional Settings<br>
Cellphone: <SELECT NAME="gsmcc" CLASS="inp">
<OPTION VALUE="1">+1
<OPTION VALUE="1340">+1340
</SELECT>
<INPUT TYPE="text" NAME="gsm">
<input type="SUBMIT" value="OK">
<input type="HIDDEN" name="process" value="true">
</form>
</body>
</html>
<%!
// Define error messages
java.util.Map errorMap = new java.util.HashMap();
public void jspInit() {
errorMap.put(ValidatePersonalBean.ERR_EMAIL_ENTER, "Please enter an email address");
errorMap.put(ValidatePersonalBean.ERR_EMAIL_INVALID, "The email address is not valid");
errorMap.put(ValidatePersonalBean.ERR_ZIPCODE_ENTER, "Please enter a zipcode");
errorMap.put(ValidatePersonalBean.ERR_ZIPCODE_INVALID, "The zipcode must be 5 digits");
errorMap.put(ValidatePersonalBean.ERR_ZIPCODE_NUM_ONLY, "The zipcode must contain only digits");
errorMap.put(ValidatePersonalBean.ERR_RPASSWORD_ENTER, "Password and Repeat Password must be equals.");
errorMap.put(ValidatePersonalBean.ERR_PASSWORD_INVALID, "Password contain illegal characters");
errorMap.put(ValidatePersonalBean.ERR_PASSWORD_LENGTH, "Password must be at least 4 character");
}
%>
'bönan
import java.util.*;
import java.util.regex.*;
import java.sql.*;
public class ValidatePersonalBean {
/* The properties */
String email = "";
String zipcode = "";
String Password = "";
String rPassword = "";
String fname = "";
String lname = "";
String dob = "";
String Country = "";
String Timezone = "";
String Mobilephone = "";
String Icqnumber = "";
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email.trim();
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode.trim();
}
public String getPassword() {
return Password;
}
public void setPassword(String Password) {
this.Password = Password.trim();
}
public String getrPassword() {
return rPassword;
}
public void setrPassword(String rPassword) {
this.rPassword = rPassword.trim();
}
public String getCountry() {
return Country;
}
public void setCountry(String Country) {
this.Country = Country.trim();
}
public String getTimezone() {
return Timezone;
}
public void setTimezone(String Timezone) {
this.Timezone = Timezone.trim();
}
public String getMobilephone() {
return Mobilephone;
}
public void setMobilephone(String Mobilephone) {
this.Mobilephone = Mobilephone.trim();
}
public String getIcqnumber() {
return email;
}
public void setIcqnumber(String Icqnumber) {
this.Icqnumber = Icqnumber.trim();
}
/* Errors */
public static final Integer ERR_EMAIL_ENTER = new Integer(1);
public static final Integer ERR_EMAIL_INVALID = new Integer(2);
public static final Integer ERR_ZIPCODE_ENTER = new Integer(3);
public static final Integer ERR_ZIPCODE_INVALID = new Integer(4);
public static final Integer ERR_ZIPCODE_NUM_ONLY = new Integer(5);
public static final Integer ERR_RPASSWORD_ENTER = new Integer(6);
public static final Integer ERR_PASSWORD_INVALID = new Integer(7);
public static final Integer ERR_PASSWORD_LENGTH = new Integer(8);
// Holds error messages for the properties
Map errorCodes = new HashMap();
// Maps error codes to textual messages.
// This map must be supplied by the object that instantiated this bean.
Map msgMap;
public void setErrorMessages(Map msgMap) {
this.msgMap = msgMap;
}
public String getErrorMessage(String propName) {
Integer code = (Integer)(errorCodes.get(propName));
if (code == null) {
return "";
} else if (msgMap != null) {
String msg = (String)msgMap.get(code);
if (msg != null) {
return msg;
}
}
return "Error";
}
/* Form validation and processing */
public boolean isValid() {
// Clear all errors
errorCodes.clear();
// Validate email
try{
String driverName="sun.jdbc.odbc.JdbcOdbcDriver";
String dbUrl="jdbc:odbc:login";
Class.forName(driverName);
Connection db =DriverManager.getConnection(dbUrl,"","");
Statement stmt = db.createStatement();
String query = "SELECT * FROM [login] WHERE [account] = '"+ email +"'";
ResultSet rs = stmt.executeQuery(query);
if(rs.next()){
errorCodes.put("email", ERR_EMAIL_INVALID);
}
rs.close();
stmt.close();
db.close();
}
catch(Exception e){
System.out.println(e);
}
Pattern pe = Pattern.compile("[^A-Za-z0-9\\.\\@_\\-~#]+");
Matcher me = pe.matcher(email);
if (me.find() || email.length() < 6 || email.indexOf("@") == -1 || email.indexOf(".") == -1) {
errorCodes.put("email", ERR_EMAIL_INVALID);
}
if (zipcode.length() == 0) {
errorCodes.put("zipcode", ERR_ZIPCODE_ENTER);
} else if (zipcode.length() != 5) {
errorCodes.put("zipcode", ERR_ZIPCODE_INVALID);
} else {
try {
int i = Integer.parseInt(zipcode);
} catch (NumberFormatException e) {
errorCodes.put("zipcode", ERR_ZIPCODE_NUM_ONLY);
}
}
// Validate Password(s)
Pattern pp = Pattern.compile("[A-Za-z0-9\\.\\@_\\-~#]+");
Matcher mp = pp.matcher(Password);
if (mp.find()) {
errorCodes.put("Password", ERR_PASSWORD_INVALID);
} else if (Password.length() < 4) {
errorCodes.put("Password", ERR_PASSWORD_LENGTH);
}
if (!Password.equals(rPassword)) {
errorCodes.put("rPassword", ERR_RPASSWORD_ENTER);
}
// Validate Country
if (Country.length() != 2) {
errorCodes.put("Country", ERR_EMAIL_ENTER);
} else if (!Country.matches("[a-z]")) {
errorCodes.put("Country", ERR_EMAIL_INVALID);
}
// Validate Timezone
try {
int tz = Integer.parseInt(Timezone);
} catch (NumberFormatException e) {
errorCodes.put("Timezone", ERR_ZIPCODE_NUM_ONLY);
}
// Validate Mobilephone
try {
int pn = Integer.parseInt(Mobilephone);
} catch (NumberFormatException e) {
errorCodes.put("Mobilephone", ERR_ZIPCODE_NUM_ONLY);
}
// Validate Icqnumber
try {
int in = Integer.parseInt(Icqnumber);
} catch (NumberFormatException e) {
errorCodes.put("Icqnumber", ERR_ZIPCODE_NUM_ONLY);
}
// If no errors, form is valid
return errorCodes.size() == 0;
}
public boolean process() {
if (!isValid()) {
return false;
}
// Process form...
// Clear the form
email = "";
zipcode = "";
Password = "";
rPassword = "";
fname = "";
lname = "";
dob = "";
Country = "";
Timezone = "";
Mobilephone = "";
Icqnumber = "";
errorCodes.clear();
return true;
}
}
Andreas