-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
servlet #219
Open
alexpol11
wants to merge
4
commits into
tehnotrack:master
Choose a base branch
from
alexpol11:alexpo11-servlet
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
servlet #219
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,38 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8"> | ||
<output url="file://$MODULE_DIR$/target/classes" /> | ||
<output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
<excludeFolder url="file://$MODULE_DIR$/target" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.37" level="project" /> | ||
<orderEntry type="library" name="Maven: javax.servlet:jstl:1.2" level="project" /> | ||
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-server:9.4.0.RC2" level="project" /> | ||
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-http:9.4.0.RC2" level="project" /> | ||
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-io:9.4.0.RC2" level="project" /> | ||
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-webapp:9.4.0.RC2" level="project" /> | ||
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-xml:9.4.0.RC2" level="project" /> | ||
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-servlet:9.4.0.RC2" level="project" /> | ||
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-security:9.4.0.RC2" level="project" /> | ||
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-proxy:9.4.0.RC2" level="project" /> | ||
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-util:9.4.0.RC2" level="project" /> | ||
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-client:9.4.0.RC2" level="project" /> | ||
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-mapper-asl:1.9.13" level="project" /> | ||
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-core-asl:1.9.13" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" /> | ||
<orderEntry type="library" name="Maven: commons-io:commons-io:2.5" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.4" level="project" /> | ||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.6.3" level="project" /> | ||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.6.3" level="project" /> | ||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.6.3" level="project" /> | ||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-log4j12:1.7.5" level="project" /> | ||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.5" level="project" /> | ||
<orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" /> | ||
<orderEntry type="library" name="Maven: javax.servlet:javax.servlet-api:4.0.0" level="project" /> | ||
</component> | ||
</module> |
188 changes: 188 additions & 0 deletions
188
L10_servlet/src/main/java/track/servlet/container/ConversationService.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,188 @@ | ||
package track.servlet.container; | ||
|
||
import java.sql.*; | ||
import java.util.*; | ||
|
||
/** | ||
* | ||
*/ | ||
public class ConversationService { | ||
|
||
protected Map<Integer, Connection> connectionsMap; | ||
|
||
ConversationService(){ | ||
connectionsMap = new HashMap<>(); | ||
connect(); | ||
} | ||
|
||
private void connect() { | ||
String baseName = null; | ||
for (int i = 0; i < 3; i++){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
baseName = "tdb-"+ i +".trail5.net:"; | ||
try { | ||
DriverManager.registerDriver((Driver) Class.forName("com.mysql.jdbc.Driver").newInstance()); | ||
StringBuilder url = new StringBuilder(); | ||
url. | ||
append("jdbc:mysql://"). //db type | ||
append(baseName). //host name | ||
append("3306/"). //port | ||
append("track17?"). //db name | ||
append("user=track_student&"). //login | ||
append("password=7EsH.H6x"); //password | ||
connectionsMap.put(i, DriverManager.getConnection(url.toString())); | ||
} catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
private Connection returnDbconnection(String username) throws IllegalArgumentException{ | ||
char firstLetter = username.charAt(0); | ||
String baseName = null; | ||
if (('a' <= firstLetter) && (firstLetter <= 'j')){ | ||
return connectionsMap.get(0); | ||
}else if (('k' <= firstLetter) && (firstLetter <= 't')){ | ||
return connectionsMap.get(1); | ||
}else if (('u' <= firstLetter) && (firstLetter <= 'z')){ | ||
return connectionsMap.get(2); | ||
}else { | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
public long store(Message msg) { | ||
PreparedStatement statement = null; | ||
ResultSet generatedKeys = null; | ||
try { | ||
Connection connection = returnDbconnection(msg.ownerLogin); | ||
statement = connection.prepareStatement | ||
("INSERT INTO messages(user_name, text, ts) VALUE (?, ?, now())", | ||
Statement.RETURN_GENERATED_KEYS); | ||
statement.setString(1, msg.ownerLogin); | ||
statement.setString(2, msg.text); | ||
// statement.setTimestamp(3, new Timestamp( msg.ts)); | ||
|
||
statement.execute(); | ||
|
||
generatedKeys = statement.getGeneratedKeys(); | ||
generatedKeys.next(); | ||
return generatedKeys.getLong(1); | ||
|
||
|
||
}catch (SQLException e) { | ||
e.printStackTrace(); | ||
}catch (IllegalArgumentException e){ | ||
System.out.println("Username must starts with english character!"); | ||
e.printStackTrace(); | ||
}finally{ | ||
try { | ||
if (statement != null) | ||
statement.close(); | ||
}catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
try{ | ||
if (generatedKeys != null) | ||
generatedKeys.close(); | ||
}catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
List<Message> getHistory(long from, long to, long limit){ | ||
|
||
try { | ||
PreparedStatement[] statements = new PreparedStatement[3]; | ||
List<Message> msgList = new ArrayList<>(); | ||
ResultSet resultSet = null; | ||
for(int i = 0; i < 3; i++){ | ||
try { | ||
statements[i] = connectionsMap.get(i).prepareStatement | ||
("SELECT * FROM messages WHERE ts BETWEEN ? AND now() ORDER BY ts ASC LIMIT ?", | ||
Statement.RETURN_GENERATED_KEYS); | ||
statements[i].setTimestamp(1, new Timestamp(from - 1)); | ||
// statements[i].setTimestamp(2, new Timestamp(from + 1)); | ||
statements[i].setLong(2, limit); | ||
|
||
resultSet = statements[i].executeQuery(); | ||
msgList.addAll(toMsgList(resultSet)); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
}finally { | ||
try { | ||
if (statements[i] != null) | ||
statements[i].close(); | ||
}catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
try{ | ||
if (resultSet != null) | ||
resultSet.close(); | ||
}catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
msgList.sort(new Comparator<Message>() { | ||
@Override | ||
public int compare(Message o1, Message o2) { | ||
return o1.ts > o2.ts ? 1 : o1.ts == o2.ts ? 0 : -1; | ||
} | ||
}); | ||
return msgList.subList(0, limit < msgList.size() ? (int) limit : msgList.size()); | ||
}catch (IllegalArgumentException e){ | ||
System.out.println("Username must starts with english character!"); | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
private List<Message> toMsgList(ResultSet resultSet) throws SQLException{ | ||
List<Message> list = new ArrayList<>(); | ||
while (resultSet.next()) { | ||
Message msg = new Message(); | ||
msg.ownerLogin = resultSet.getString("user_name"); | ||
msg.text = resultSet.getString("text"); | ||
msg.ts = resultSet.getLong("ts"); | ||
|
||
list.add(msg); | ||
} | ||
return list; | ||
} | ||
|
||
List<Message> getByUser(String username, long limit){ | ||
PreparedStatement statement = null; | ||
ResultSet resultSet = null; | ||
try{ | ||
Connection connection = returnDbconnection(username); | ||
statement = connection.prepareStatement | ||
("SELECT * FROM messages WHERE user_name=? LIMIT ?", | ||
Statement.RETURN_GENERATED_KEYS); | ||
statement.setString(1, username); | ||
statement.setLong(2, limit); | ||
|
||
resultSet = statement.executeQuery(); | ||
|
||
return toMsgList(resultSet); | ||
|
||
}catch (IllegalArgumentException e){ | ||
System.out.println("Username must starts with english character!"); | ||
e.printStackTrace(); | ||
}catch (SQLException e){ | ||
e.printStackTrace(); | ||
}finally { | ||
try { | ||
if (statement != null) | ||
statement.close(); | ||
}catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
L10_servlet/src/main/java/track/servlet/container/History.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 track.servlet.container; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class History extends HttpServlet { | ||
private String param = "limit"; | ||
@Override | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
Map<String, String[]> params = req.getParameterMap(); | ||
|
||
if (params.containsKey(param)){ | ||
ConversationService conv = new ConversationService(); | ||
|
||
List<Message> msgHistory = conv.getHistory(0, System.currentTimeMillis(),Long.parseLong(req.getParameter("limit"))); | ||
req.setAttribute("msgList", msgHistory); | ||
req.getRequestDispatcher("/history.jsp").forward(req, resp); | ||
}else { | ||
resp.setContentType("text/html"); | ||
resp.setStatus(HttpServletResponse.SC_OK); | ||
resp.getWriter().println("<h1>Sorry, but u haven't entered limit field(</h1>"); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
L10_servlet/src/main/java/track/servlet/container/Message.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,40 @@ | ||
package track.servlet.container; | ||
|
||
public class Message { | ||
String ownerLogin; | ||
String text; | ||
long ts; | ||
|
||
public String getOwnerLogin() { | ||
return ownerLogin; | ||
} | ||
|
||
public void setOwnerLogin(String ownerLogin) { | ||
this.ownerLogin = ownerLogin; | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
public void setText(String text) { | ||
this.text = text; | ||
} | ||
|
||
public long getTs() { | ||
return ts; | ||
} | ||
|
||
public void setTs(long ts) { | ||
this.ts = ts; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "track.servlet.container.Message{" + | ||
"ownerLogin='" + ownerLogin + '\'' + | ||
", text='" + text + '\'' + | ||
", ts='" + ts + '\'' + | ||
'}'; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
L10_servlet/src/main/java/track/servlet/container/SendToBase.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,38 @@ | ||
package track.servlet.container; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.lang.reflect.Array; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
|
||
public class SendToBase extends HttpServlet { | ||
private String[] param = {"username", "text"}; | ||
@Override | ||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
|
||
Map<String, String[]> params = req.getParameterMap(); | ||
|
||
if (params.containsKey(param[0]) && params.containsKey(param[1])) { | ||
if (!req.getParameter(param[0]).equals("")) { | ||
ConversationService conv = new ConversationService(); | ||
|
||
Message msg = new Message(); | ||
msg.ownerLogin = req.getParameter(param[0]); | ||
msg.text = req.getParameter(param[1]); | ||
conv.store(msg); | ||
}else { | ||
resp.setContentType("text/html"); | ||
resp.setStatus(HttpServletResponse.SC_OK); | ||
resp.getWriter().println("<h1>Username must contains at least 1 char</h1>"); | ||
} | ||
}else { | ||
resp.setContentType("text/html"); | ||
resp.setStatus(HttpServletResponse.SC_OK); | ||
resp.getWriter().println("<h1>Sorry, but u haven't entered username or text field(</h1>"); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
L10_servlet/src/main/java/track/servlet/container/UserHistory.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 track.servlet.container; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class UserHistory extends HttpServlet { | ||
private String[] param = {"limit", "username"}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ключи параметров лучше вынести в константы |
||
@Override | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
Map<String, String[]> params = req.getParameterMap(); | ||
|
||
if (params.containsKey(param[0]) && params.containsKey(param[1])){ | ||
ConversationService conv = new ConversationService(); | ||
|
||
List<Message> msgUserHistory = conv.getByUser(req.getParameter(param[1]), Long.parseLong(req.getParameter(param[0]))); | ||
req.setAttribute("msgList", msgUserHistory); | ||
req.getRequestDispatcher("/history.jsp").forward(req, resp); | ||
}else { | ||
resp.setContentType("text/html"); | ||
resp.setStatus(HttpServletResponse.SC_OK); | ||
resp.getWriter().println("<h1>Sorry, but u haven't entered limit or username field(</h1>"); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ну как так? В конце семестра пулл-реквест снова забит бинарниками и левыми файлами? =(