Ok, first things first. I'm extremely new to JSP & Servlets.
I'm working on a project but just messing around with jsp & servlets a bit at the moment.
What I have at the moment is a JSP page with a form, when I click submit, it sends the user name to the servlet. The servlet checks the username to see if it's already in a list of usernames. The servlet the sends a response back to the jsp page indicating whether or not the user can be added. If the user is ok a new page will be called, however if the username already exists, a message will be displayed.
Here's the very bare servlet class:
Sorry if the code is all over the place. Again I'm very new to this, so all what I've said could be complete BS. Thanks for the help
//package brightspark.services;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
//import brightspark.exceptions.*;
//Based on example in book:
//Web Development with Java Server Pages - Fields et.al 2002.
public class AddUserServlet extends HttpServlet
{
List users;
String testString;
public AddUserServlet() {
}
public void init( ServletConfig config ) throws ServletException {
super.init( config );
users = new ArrayList();
testString = "";
}
public void service( HttpServletRequest request, HttpServletResponse response ) throws
ServletException, IOException {
String fName = request.getParameter("firstName");
boolean userExists = false;
//Search for user
int found = Collections.binarySearch( users, fName );
//Code to add user to list if username is unique.
if (found == -1 ){
users.add(fName);
testString = "Already there";
userExists = true;
}
//Code to add param to request if user is not unique.
else{
userExists = false;
testString = "User Added";
}
//Code to call the add userpage.
//Send response back???
}
}
And the jsp page:
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//w3c//DTS XHTML 1.0 Strict//EN" "http://www.w3.org/R/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Add User</title>
<script type="text/javascript">
function validForm(passForm) {
var warningColor = "#FF2828"
<!--Check for null entries in form-->
if (passForm.firstName.value == ""){
passForm.firstName.style.backgroundColor = warningColor
passForm.firstName.focus()
return false
}
if (passForm.lastName.value == ""){
passForm.lastName.style.backgroundColor = warningColor
passForm.lastName.focus()
return false
}
if (passForm.userName.value == ""){
passForm.userName.style.backgroundColor = warningColor
passForm.userName.focus()
return false
}
if (passForm.email.value == ""){
passForm.email.style.backgroundColor = warningColor
passForm.email.focus()
return false
}
if (passForm.address.value == ""){
passForm.address.style.backgroundColor = warningColor
passForm.address.focus()
return false
}
if (passForm.phone.value == ""){
passForm.phone.style.backgroundColor = warningColor
passForm.phone.focus()
return false
}
if (passForm.password.value == ""){
passForm.password.style.backgroundColor = warningColor
passForm.password.focus()
return false
}
if (passForm.confirmPassword.value == ""){
passForm.confirmPassword.style.backgroundColor = warningColor
passForm.confirmPassword.focus()
return false
}
<!--Check to see if passwords are same -->
if (passForm.password.value != passForm.confirmPassword.value){
alert("Passwords do not match")
passForm.password.value = ""
passForm.password.style.backgroundColor = warningColor
passForm.confirmPassword.value = ""
passForm.password.focus()
return false
}
if (!checkMail(passForm)){
return false
}
return true
}
<!--Check entered email address for correct syntax-->
function checkMail(passForm){
var strEmail = passForm.email.value;
<!-- Explanation of RegEx check -->
<!-- Scan the entire value from the beginning (^) to ($): -->
<!-- User Name part, i.e. before the @, should only include alphanumerical (letters or numbers), -->
<!-- underscores, dots or dashes values - ([a-zA-Z0-9_\.\-])+ -->
<!-- next comes the @ symbol - \@ -->
<!-- After that, the domain name, which may include several sub-domains (com.cit). -->
<!-- Several series of alphanumerical characters and dashes, followed by a dot will be allowed. -->
<!-- (([a-zA-Z0-9\-])+\.)+ -->
<!-- Finally the top level(.com, .co.uk, .ie) domain, once again we check for
<!-- alphanumerical characters, but now without the dash. Must be between 2 and 4 characters. -->
<!-- ([a-zA-Z0-9]{2,4})+$/; -->
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(strEmail)){
passForm.confirmPassword.value = ""
passForm.password.style.backgroundColor = warningColor
passForm.email.focus()
return false
}
}
<!-- clear all the form fields -->
function clearForm (clsForm) {
clsForm.reset()
}
</script>
</head>
<body>
<%
String hopethisworks = (request.getParameter("testString")==null)?"":request.getParameter("testString");
String fName = (request.getParameter("firstName")==null)?"":request.getParameter("firstName");
String lName = (request.getParameter("lastName")==null)?"":request.getParameter("lastName");;
String uName = (request.getParameter("userName")==null)?"":request.getParameter("userName");;
String emailAddress = (request.getParameter("email")==null)?"":request.getParameter("email");;
String userAddress = (request.getParameter("address")==null)?"":request.getParameter("address");;
String phoneNum = (request.getParameter("phone")==null)?"":request.getParameter("phone");;
String pword = (request.getParameter("password")==null)?"":request.getParameter("password");;
if (fName != "") {
%>
<h3>
TestString: <%= hopethisworks %> <br />
First Name: <%= fName %> <br />
Last Name: <%= lName %> <br />
User Name: <%= uName %> <br />
Email: <%= emailAddress %> <br />
Address: <%= userAddress %> <br />
Phone: <%= phoneNum %> <br />
Password: <%= pword %> <br />
</h3>
<%
}
else {
TestString: <%= hopethisworks %> <br />
%>
<table style="height:100%; width:100%;" border="1">
<tr style="height=15%;">
<td colspan="2">
</td>
</tr>
<tr style="height:75%;">
<td width="15%">
</td>
<td style="width: auto;">
<form method="post" action="AddUserServlet"> <!--onSubmit="return validForm(this)"-->
<!--action="/maincontroller?cmd=addUser"-->
<table style="height:100%; width: auto;" align="center" border="1" cellpadding="2"
cellspacing="0">
<tr>
<td align="right" valign="top">
First Name:
</td>
<td align="left" valign="top">
<input type="text" size="40" id="firstName" value="<%=fName%>">
</td>
<td align="left" valign="top">
<label for="firstName">test <%=userAddress%></label>
</td>
</tr>
<tr style="height:10%;">
<td>
</td>
<td>
<font size="2" color="#ff0000">First Name required</font>
</td>
</tr>
<tr>
<td align="right" valign="top">
Last Name:
</td>
<td align="left" valign="top">
<input type="text" size="40" name="lastName" value="<%=lName%>">
</td>
</tr>
<tr>
<td align="right" valign="top">
User Name:
</td>
<td align="left" valign="top">
<input type="text" size="40" name="userName" value="<%=uName%>">
</td>
</tr>
<tr>
<td align="right" valign="top">
Email:
</td>
<td align="left" valign="top">
<input type="text" size="40" name="email" value="<%=emailAddress%>">
</td>
</tr>
<tr>
<td align="right" valign="top">
Address:
</td>
<td align="left" valign="top">
<textarea name="address" cols="29" rows="4"><%=userAddress%></textarea>
</td>
</tr>
<tr>
<td align="right" valign="top">
Phone:
</td>
<td align="left" valign="top">
<input type="text" size="40" name="phone" value="<%=phoneNum%>">
</td>
</tr>
<tr>
<td align="right" valign="top">
Password:
</td>
<td align="left" valign="top">
<input type="password" size="40" name="password">
</td>
</tr>
<tr>
<td align="right" valign="top">
Confirm Password:
</td>
<td align="left" valign="top">
<input type="password" size="40" name="confirmPassword">
</td>
</tr>
<tr valign="top">
<td colspan="2" align="center">
<input type="submit" value="Submit" onclick="validForm(passForm)">
<input type="reset" value="Clear" onclick="clearForm(passForm)" />
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<%
}
%>
</body>
</html>