-
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.
- Loading branch information
0 parents
commit c60f117
Showing
10 changed files
with
236 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target | ||
*.class |
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,51 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>me.nknj</groupId> | ||
<artifactId>mvcDemo</artifactId> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>mvcDemo Maven Webapp</name> | ||
<url>http://maven.apache.org</url> | ||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>3.1.0</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<finalName>mvcDemo</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-dependency-plugin</artifactId> | ||
<version>2.8</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>copy</goal> | ||
</goals> | ||
<configuration> | ||
<artifactItems> | ||
<artifactItem> | ||
<groupId>org.eclipse.jetty</groupId> | ||
<artifactId>jetty-runner</artifactId> | ||
<version>9.1.0.RC1</version> | ||
<destFileName>jetty-runner.jar</destFileName> | ||
</artifactItem> | ||
</artifactItems> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
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,19 @@ | ||
package controller; | ||
|
||
import java.io.IOException; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.RequestDispatcher; | ||
|
||
public class DashboardServlet extends HttpServlet { | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
throws ServletException, IOException { | ||
RequestDispatcher rd = req.getRequestDispatcher("dashboard.jsp"); | ||
rd.forward(req, resp); | ||
} | ||
|
||
} |
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,33 @@ | ||
package controller; | ||
|
||
import java.io.IOException; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.RequestDispatcher; | ||
|
||
import model.entity.User; | ||
import model.UserManager; | ||
|
||
public class SignUpServlet extends HttpServlet { | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
throws ServletException, IOException { | ||
RequestDispatcher rd = req.getRequestDispatcher("signup.html"); | ||
rd.forward(req, resp); | ||
} | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) | ||
throws ServletException, IOException { | ||
String username = req.getParameter("username"); | ||
String password = req.getParameter("password"); | ||
UserManager.addUser(username, password); | ||
|
||
resp.sendRedirect("/dashboard"); | ||
} | ||
|
||
|
||
} |
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,43 @@ | ||
package model; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
import model.entity.User; | ||
|
||
public class UserManager { | ||
|
||
private static final String USER_DATA_FILE = "users.txt"; | ||
|
||
public static void addUser(String username, String password) { | ||
|
||
try { | ||
FileWriter writer = new FileWriter(USER_DATA_FILE); | ||
writer.write(username + " " + password); | ||
writer.close(); | ||
} catch (IOException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
|
||
public static User getUser() { | ||
|
||
try { | ||
BufferedReader br = new BufferedReader(new FileReader(USER_DATA_FILE)); | ||
String content = br.readLine(); | ||
if (content != null) { | ||
String[] tokens = content.split(" "); | ||
String username = tokens[0]; | ||
String password = tokens[1]; | ||
return new User(username, password); | ||
} | ||
} catch (IOException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
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,19 @@ | ||
package model.entity; | ||
|
||
public class User { | ||
private String username; | ||
private String password; | ||
|
||
public User(String username, String password) { | ||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
} |
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,10 @@ | ||
<!DOCTYPE html> | ||
|
||
<html> | ||
<head> | ||
<title>User Not Found</title> | ||
</head> | ||
<body> | ||
<h3>Sorry, User Not Found</h3> | ||
</body> | ||
</html> |
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,23 @@ | ||
<!DOCTYPE web-app PUBLIC | ||
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" | ||
"http://java.sun.com/dtd/web-app_2_3.dtd" > | ||
|
||
<web-app> | ||
<display-name>Archetype Created Web Application</display-name> | ||
<servlet> | ||
<servlet-name>SignUp</servlet-name> | ||
<servlet-class>controller.SignUpServlet</servlet-class> | ||
</servlet> | ||
<servlet-mapping> | ||
<servlet-name>SignUp</servlet-name> | ||
<url-pattern>/signup</url-pattern> | ||
</servlet-mapping> | ||
<servlet> | ||
<servlet-name>Dashboard</servlet-name> | ||
<servlet-class>controller.DashboardServlet</servlet-class> | ||
</servlet> | ||
<servlet-mapping> | ||
<servlet-name>Dashboard</servlet-name> | ||
<url-pattern>/dashboard</url-pattern> | ||
</servlet-mapping> | ||
</web-app> |
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,22 @@ | ||
<%@ page import="model.entity.User, model.UserManager" %> | ||
|
||
<!DOCTYPE html> | ||
|
||
<html> | ||
<head> | ||
<title>Dashboard</title> | ||
</head> | ||
<body> | ||
<% | ||
User u = UserManager.getUser(); | ||
if (u == null) { | ||
response.sendRedirect("404.html"); | ||
return; | ||
} | ||
%> | ||
<h2> | ||
Hello, <%=u.getUsername()%> | ||
</h2> | ||
Your password is <%=u.getPassword()%>, oops! | ||
</body> | ||
</html> |
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,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Signup</title> | ||
</head> | ||
<body> | ||
<h3>Sign Up</h3> | ||
<form method="post" action="/signup"> | ||
<input type="text" name="username" placeholder="Username"></br> | ||
<input type="password" name="password" placeholder="Password"></br> | ||
<input type="submit" value="Submit"> | ||
</form> | ||
</body> | ||
</html> |