-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
167 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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
31 changes: 31 additions & 0 deletions
31
web-module/src/main/java/cz/literak/sandbox/so/register/Controller.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cz.literak.sandbox.so.register; | ||
|
||
import javax.annotation.Resource; | ||
import javax.servlet.RequestDispatcher; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.sql.DataSource; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Date: 5. 4. 2014 | ||
*/ | ||
public class Controller extends HttpServlet { | ||
@Override | ||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||
String name = request.getParameter("name"); | ||
String phone = request.getParameter("phone"); | ||
// todo check parameters | ||
|
||
User user = new User(name, phone); | ||
DAO dao = new DAO(); | ||
dao.saveUser(user); | ||
request.setAttribute("USER", user);// consider using session | ||
// TODO handle errors | ||
|
||
RequestDispatcher dispatcher = request.getRequestDispatcher("register/index.jsp"); | ||
dispatcher.forward(request, response); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
web-module/src/main/java/cz/literak/sandbox/so/register/DAO.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cz.literak.sandbox.so.register; | ||
|
||
import javax.servlet.ServletException; | ||
import java.sql.*; | ||
|
||
/** | ||
* Date: 5. 4. 2014 | ||
*/ | ||
public class DAO { | ||
static { | ||
try { | ||
Class.forName("org.mariadb.jdbc.Driver"); | ||
// Class.forName("com.mysql.jdbc.Driver"); | ||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public User findUser(String phone) { | ||
// todo | ||
return null; | ||
} | ||
|
||
// DDL: CREATE TABLE user (name VARCHAR(100), phone VARCHAR(100)) | ||
public boolean saveUser(User user) throws ServletException { | ||
try ( | ||
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "secret"); | ||
PreparedStatement ps = con.prepareStatement("INSERT INTO user VALUES(?,?)")) | ||
{ | ||
ps.setString(1, user.getName()); | ||
ps.setString(2, user.getPhone()); | ||
return ps.execute(); | ||
} catch (SQLException e) { | ||
throw new ServletException("DB operation failed", e); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
web-module/src/main/java/cz/literak/sandbox/so/register/User.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cz.literak.sandbox.so.register; | ||
|
||
/** | ||
* Date: 5. 4. 2014 | ||
*/ | ||
public class User { | ||
String name; | ||
String phone; | ||
|
||
public User(String name, String phone) { | ||
this.name = name; | ||
this.phone = phone; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getPhone() { | ||
return phone; | ||
} | ||
|
||
public void setPhone(String phone) { | ||
this.phone = phone; | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<%-- | ||
Date: 5. 4. 2014 | ||
This code is intented as show case for stackoverflow user having trouble | ||
--%> | ||
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> | ||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> | ||
<html> | ||
<head> | ||
<title>Registration sample</title> | ||
</head> | ||
<body> | ||
|
||
<%--@elvariable id="USER" type="cz.literak.sandbox.so.register.User"--%> | ||
<c:choose> | ||
<c:when test="${USER == null}"> | ||
<form action="${pageContext.request.contextPath}/RegistrationController" method="post"> | ||
<table> | ||
<tr> | ||
<td>User name</td> | ||
<td><input type="text" name="name" ></td> | ||
</tr> | ||
<tr> | ||
<td>Phone</td> | ||
<td><input type="text" name="phone"></td> | ||
</tr> | ||
<tr> | ||
<td></td> | ||
<td><input type="submit" value="submit"></td> | ||
</tr> | ||
</table> | ||
</form> | ||
</c:when> | ||
<c:otherwise> | ||
Welcome <c:out value="${USER.name}"/> | ||
</c:otherwise> | ||
</c:choose> | ||
|
||
</body> | ||
</html> |