Last active
December 11, 2015 08:48
-
-
Save pikanji/4575405 to your computer and use it in GitHub Desktop.
Sample code for blog post: http://kurotofu.sytes.net/kanji/fool/?p=950
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RequestMapping("/signup") | |
@SessionAttributes("user") | |
@Controller | |
public class SignUpFormController { | |
private static final String DB_DRIVER_NAME = "com.mysql.jdbc.Driver"; | |
private static final String DB_URL = "jdbc:mysql://localhost/db_name?useUnicode=true&characterEncoding=UTF-8"; | |
private static final String DB_USER = "db_user"; | |
private static final String DB_PSWD = "db_password"; | |
private static final Logger logger = LoggerFactory | |
.getLogger(SignUpFormController.class); | |
/** | |
* This is called when the signup page is loaded. | |
*/ | |
@RequestMapping(method = RequestMethod.GET) | |
public String showform(Model model) { | |
model.addAttribute("user", new User()); | |
return "signup"; | |
} | |
/** | |
* This is called when submit button of the form is pressed and the page is reloaded. | |
*/ | |
@RequestMapping(method = RequestMethod.POST) | |
public String submitform(@ModelAttribute("user") User user, BindingResult result, Model model) { | |
try { | |
Class.forName(DB_DRIVER_NAME); // Load driver | |
Connection con = DriverManager.getConnection(DB_URL, DB_USER, DB_PSWD); | |
Statement state = con.createStatement(); | |
StandardPasswordEncoder encoder = new StandardPasswordEncoder(); | |
String encodedPass = encoder.encode(user.getPassword()); | |
String sql = "INSERT INTO `users` (`user_name`, `password`, `enabled`) VALUES ('" + | |
user.getUserName() + "', '" + | |
encodedPass + "', '" + | |
1 + "')"; | |
state.execute(sql); | |
con.close(); | |
} catch (ClassNotFoundException e) { | |
logger.error(e.getMessage()); | |
} catch (SQLException e) { | |
logger.error(e.getMessage()); | |
} catch (Exception e) { | |
logger.error(e.getMessage()); | |
} | |
model.addAttribute("username", user.getUserName()); | |
model.addAttribute("password", user.getPassword()); | |
return "success"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class User { | |
// Basic Details | |
private int userId; | |
private String userName; | |
private String password; | |
// Login Details | |
private int enabled = 1; | |
private String userAuthority = "ROLE_USER"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> | |
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> | |
<html> | |
<head> | |
<title>Sign up</title> | |
</head> | |
<body> | |
<h1>Sign up</h1> | |
<p> | |
<form:form method="POST" commandName="user" modelAttribute="user" id="signup"> | |
<form:label path="userName">User Name</form:label> | |
<form:input path="userName" required="true" /><br/> | |
<form:label path="password">Password</form:label> | |
<form:input type="password" path="password" required="true" /><br/> | |
<input type="submit" value="SignUp"/> | |
</form:form> | |
</p> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> | |
<%@ page session="false" %> | |
<html> | |
<head> | |
<title>Success</title> | |
</head> | |
<body> | |
<h1>Success</h1> | |
<P>Successfully registerd</P> | |
<p>User Name: ${username}</p> | |
<p>Password: ${password}</p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment