So knowing the little pieces is something different and putting them together is something different.
Here is the code where I try to follow good oop practices and 3 - layered structure.
Any review is greatly appreciated:
The form is located at registirationform.jsp.
The view layer:
public class RegistirationServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws ServletException, IOException {
httpServletRequest.setCharacterEncoding("utf-8");
httpServletResponse.setContentType("text/html;charset=utf-8");
try {
UserRegistirationService userRegistirationService
= new UserRegistirationService(new AppUserAccessObject(new DatabaseConnectionImpl()));
String username = httpServletRequest.getParameter("username");
String password = httpServletRequest.getParameter("password");
String email = httpServletRequest.getParameter("email");
if(username.equals("")
|| password.equals("")
|| email.equals("")){
List<String> errors = new ArrayList<String>();
errors.add("Can you make sure you fill all the fields?");
httpServletRequest.setAttribute("errors",errors);
if(!username.equals("")){
httpServletRequest.setAttribute("username",username);
}
if(!email.equals("")){
httpServletRequest.setAttribute("email",email);
}
httpServletRequest.getRequestDispatcher("/registirationform.jsp").forward(httpServletRequest, httpServletResponse);
}
else {
userRegistirationService.registerAppUser(username, password, email);
}
} catch (SQLException e) {
e.printStackTrace();
List<String> errors = new ArrayList<String>();
errors.add("Registiration was not successful. Maybe username/email was already taken?");
httpServletRequest.setAttribute("errors",errors);
httpServletRequest.getRequestDispatcher("/registirationform.jsp").forward(httpServletRequest, httpServletResponse);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
and the service layer:
public class UserRegistirationService {
private AppUserAccessObject appUserAccessObject;
public UserRegistirationService(AppUserAccessObject appUserAccessObject) {
this.appUserAccessObject = appUserAccessObject;
}
public void registerAppUser(String username, String password, String email) throws SQLException {
appUserAccessObject.insertUser(username, password, email);
}
}
and the db layer:
public class AppUserAccessObject extends DataAccessObjectImpl implements DataAccessObject {
public AppUserAccessObject(DatabaseConnectionImpl databaseConnection)
throws IOException, SQLException, ClassNotFoundException {
super(databaseConnection);
setTableName("app_user");
}
public void insertUser(String username, String email, String password) throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO " + tableName + "(username,email,password) VALUES(?,?,?)");
preparedStatement.setString(1, username);
preparedStatement.setString(2, email);
preparedStatement.setString(3, password);
preparedStatement.execute();
}
}
Is all the code at correct layers?
new DatabaseConnectionImpl()
andcatch (SQLException e)
in your servlet class, therefore your web layer is not separated from data access layer. \$\endgroup\$ServletContextListener
in the web.xml. You create your services, factories etc in itscontextInitialized
method, and stick them into theServletContext
withsetAttribute("some.prefix.xyzService", xyzService)
etc. You can then get them back in the servlet withXyzService xyzService = (XyzService)(getServletContext().getAttribute("some.prefix.xyzService"));
\$\endgroup\$