Problem with jsp
I have a form (with a few fields)..basically a singup form. and i have the username field which should be chosen to be unique by the user. For this i have to connect to database,chec开发者_StackOverflow社区k whether such a username exists and then display an appropriate message in the same form. All this must happen when i click the "check Availabilty" button. How should i handle this
If you want to do it all in one form (not recommended), you can write a single JSP using JSTL <sql>
tags.
A better approach would be to break the problem up into parts. Start with a Credential class:
package model;
public class Credential
{
private String username;
private String password;
// Add the rest.
}
Have a DAO interface that lets you get a Credential out of a database:
package persistence;
public interface CredentialDao
{
List<Credential> find();
Credential find(String username);
}
Write a servlet that fields requests from your JSP with the form, validates and binds the input, uses a CredentialDao to search for the Credential given a username, and then decides what to send as the response to the JSP by examining what comes back.
- get username
- check if it exists using "select username from..."
- if it exists, redirect user to an error page telling them that user already exists (or use Ajax to tell them the same thing on one page)
- if it doesn't exist, register a new user using "insert username to..." or whatever mechanism you're using and then tell user registration was sucessfull
精彩评论