Last active
April 15, 2017 18:28
-
-
Save 13andrew13/9ce85987f78c93dea1f1da67d14a3afa to your computer and use it in GitHub Desktop.
Создать проект «Анкета». Сделать возможностьввода пользователем его имени, фамилии,возраста и ответов на 2-3 вопроса. Данныедолжны отправляться на сервер, который в ответдолжен вернуть статистику по ответам в виде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
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
/** | |
* Created by andrew on 13.04.17. | |
*/ | |
public class AdminServlet extends HttpServlet { | |
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { | |
resp.getWriter().println(String.format("<html><head></head><body><p>%s</p></body></html>",Users.newUsers().toString())); | |
} | |
} |
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
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.http.HttpSession; | |
import java.io.IOException; | |
/** | |
* Created by andrew on 11.04.17. | |
*/ | |
public class LoginServlet extends HttpServlet { | |
public static String name; | |
public static String surname; | |
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { | |
} | |
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { | |
name = req.getParameter("name"); | |
surname = req.getParameter("surname"); | |
if(name.equals("admin")){ | |
resp.sendRedirect("/admin"); | |
}else{ | |
Users.newUsers().add(name+' '+ surname); | |
resp.sendRedirect("test.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
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
/** | |
* Created by andrew on 13.04.17. | |
*/ | |
public class AdminServlet extends HttpServlet { | |
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { | |
resp.getWriter().println(String.format("<html><head></head><body><p>%s</p></body></html>",Users.newUsers().toString())); | |
} | |
} |
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
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.http.HttpSession; | |
import java.io.IOException; | |
/** | |
* Created by andrew on 11.04.17. | |
*/ | |
public class LoginServlet extends HttpServlet { | |
public static String name; | |
public static String surname; | |
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { | |
} | |
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { | |
name = req.getParameter("name"); | |
surname = req.getParameter("surname"); | |
if(name.equals("admin")){ | |
resp.sendRedirect("/admin"); | |
}else{ | |
Users.newUsers().add(name+' '+ surname); | |
resp.sendRedirect("test.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
/** | |
* Created by andrew on 11.04.17. | |
* | |
*/ | |
import javax.servlet.*; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
public class StatisticServlet extends HttpServlet { | |
public static int x=0; | |
public static int y=0; | |
public static int w=0; | |
public static int z=0; | |
public static final String TEMPLATE ="<html><head></head><body>"; | |
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { | |
String answer1 = req.getParameter("first"); | |
String answer2 = req.getParameter("second"); | |
if("yes".equals(answer1)){ | |
x++; | |
} | |
else { | |
y++; | |
} | |
if("yes".equals(answer2)){ | |
w++; | |
} | |
else { | |
z++; | |
} | |
StringBuilder statistic= new StringBuilder(); | |
statistic.append("%s %s" + | |
"<p>Liking java: yes=%s no=%s</p>"); | |
statistic.append("<p>Liking java: yes=%s no=%s</p></body></html>"); | |
resp.setContentType("text/html"); | |
resp.getWriter().println(String.format(TEMPLATE+statistic.toString(),LoginServlet.name,LoginServlet.surname,x,y,w,z)); | |
} | |
} | |
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
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by andrew on 13.04.17. | |
*/ | |
public class Users { | |
private List<String> list = new ArrayList<>(); | |
private static Users instance = new Users(); | |
private Users(){} | |
public static Users newUsers(){ | |
return instance; | |
} | |
public List<String> getList() { | |
return list; | |
} | |
@Override | |
public String toString() { | |
return "Users{" + | |
"list=" + list + | |
'}'; | |
} | |
public void add(String s){ | |
list.add(s); | |
} | |
} |
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
/** | |
* Created by andrew on 11.04.17. | |
* | |
*/ | |
import javax.servlet.*; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
public class StatisticServlet extends HttpServlet { | |
public static int x=0; | |
public static int y=0; | |
public static int w=0; | |
public static int z=0; | |
public static final String TEMPLATE ="<html><head></head><body>"; | |
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { | |
String answer1 = req.getParameter("first"); | |
String answer2 = req.getParameter("second"); | |
if("yes".equals(answer1)){ | |
x++; | |
} | |
else { | |
y++; | |
} | |
if("yes".equals(answer2)){ | |
w++; | |
} | |
else { | |
z++; | |
} | |
StringBuilder statistic= new StringBuilder(); | |
statistic.append("%s %s" + | |
"<p>Liking java: yes=%s no=%s</p>"); | |
statistic.append("<p>Liking java: yes=%s no=%s</p></body></html>"); | |
resp.setContentType("text/html"); | |
resp.getWriter().println(String.format(TEMPLATE+statistic.toString(),LoginServlet.name,LoginServlet.surname,x,y,w,z)); | |
} | |
} | |
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
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by andrew on 13.04.17. | |
*/ | |
public class Users { | |
private List<String> list = new ArrayList<>(); | |
private static Users instance = new Users(); | |
private Users(){} | |
public static Users newUsers(){ | |
return instance; | |
} | |
public List<String> getList() { | |
return list; | |
} | |
@Override | |
public String toString() { | |
return "Users{" + | |
"list=" + list + | |
'}'; | |
} | |
public void add(String s){ | |
list.add(s); | |
} | |
} |
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" | |
"http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<title>Title</title> | |
</head> | |
<body> | |
<form action="/login" method="post"> | |
Name<input type="text" name="name"> | |
Surname<input type="text" name = "surname"> | |
<input type="submit" value="Submit"> | |
</form> | |
</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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" | |
"http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<title>Form</title> | |
</head> | |
<body> | |
<form action="/answer" method="get"> | |
Do you like Java? | |
<br> | |
Yes<input type="radio" name="first" value="yes"> | |
No<input type="radio" name="first" value="no"> | |
<br> | |
Do you like C#? | |
<br> | |
Yes<input type="radio" name="second" value="yes" > | |
No<input type="radio" name="second" value="no"> | |
<br> | |
<input type="submit" name="submit"> | |
</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
<?xml version="1.0" encoding="UTF-8"?> | |
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" | |
version="3.1"> | |
<servlet> | |
<servlet-name>StatisticServlet</servlet-name> | |
<servlet-class>StatisticServlet</servlet-class> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>StatisticServlet</servlet-name> | |
<url-pattern>/answer</url-pattern> | |
</servlet-mapping> | |
<servlet> | |
<servlet-name>loginServlet</servlet-name> | |
<servlet-class>LoginServlet</servlet-class> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>loginServlet</servlet-name> | |
<url-pattern>/login</url-pattern> | |
</servlet-mapping> | |
<servlet> | |
<servlet-name>adminServlet</servlet-name> | |
<servlet-class>AdminServlet</servlet-class> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>adminServlet</servlet-name> | |
<url-pattern>/admin</url-pattern> | |
</servlet-mapping> | |
<welcome-file-list> | |
<welcome-file>index.html</welcome-file> | |
</welcome-file-list> | |
</web-app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment