-
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.
finish the basic funcitons of a chat room
use servlet to bulid a char room
- Loading branch information
0 parents
commit b076c96
Showing
23 changed files
with
379 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,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> | ||
<classpathentry kind="lib" path="D:/Program Files/Apache Software Foundation/Tomcat 9.0/lib/servlet-api.jar"/> | ||
<classpathentry kind="lib" path="D:/Program Files/Apache Software Foundation/Tomcat 9.0/lib/jstl.jar"/> | ||
<classpathentry kind="lib" path="D:/Program Files/Apache Software Foundation/Tomcat 9.0/lib/standard.jar"/> | ||
<classpathentry kind="lib" path="D:/Program Files/Apache Software Foundation/Tomcat 9.0/lib/mysql-connector-java-5.0.8-bin.jar"/> | ||
<classpathentry kind="output" path="web/WEB-INF/classes"/> | ||
</classpath> |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>chat room</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
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,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.8 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.8 |
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,17 @@ | ||
package bean; | ||
|
||
public class Content { | ||
private int id; | ||
private String username; | ||
private String stime; | ||
private String content; | ||
|
||
public int getId() { return id; } | ||
public String getUsername() { return username; } | ||
public String getStime() { return stime; } | ||
public String getContent() { return content; } | ||
public void setId(int id) { this.id = id; } | ||
public void setUsername(String name) { this.username = name; } | ||
public void setStime(String stime) { this.stime = stime; } | ||
public void setContent(String content) { this.content = content; } | ||
} |
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,25 @@ | ||
package bean; | ||
|
||
public class User { | ||
private int id; | ||
private String name; | ||
private String password; | ||
public int getId() { | ||
return id; | ||
} | ||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
public String getName() { | ||
return name; | ||
} | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
public String getPassword() { | ||
return password; | ||
} | ||
public void setPassword(String password) { | ||
this.password = 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,72 @@ | ||
package dao; | ||
import java.util.List; | ||
import java.util.LinkedList; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
import bean.Content; | ||
|
||
public class ContentDAO { | ||
|
||
public static void main(String[] args) { | ||
Content p = new Content(); | ||
p.setUsername("jane"); | ||
p.setStime("03/23/2016"); | ||
p.setContent("this is added by eclipse"); | ||
new ContentDAO().addContent(p); | ||
} | ||
|
||
public void addContent(Content p) { | ||
try { | ||
Class.forName("com.mysql.jdbc.Driver"); | ||
Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/chatroom?characterEncoding=UTF-8", | ||
"root", "password"); | ||
String sql = "INSERT INTO record values(null, ?, ?, ?);"; | ||
PreparedStatement ps = c.prepareStatement(sql); | ||
ps.setString(1, p.getUsername()); | ||
ps.setString(2, p.getStime()); | ||
ps.setString(3, p.getContent()); | ||
ps.executeUpdate(); | ||
|
||
ps.close(); | ||
c.close(); | ||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public List<Content> ListContent() { | ||
List<Content> list = new LinkedList<>(); | ||
try { | ||
Class.forName("com.mysql.jdbc.Driver"); | ||
Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/chatroom?characterEncoding=UTF-8", | ||
"root", "password"); | ||
String sql = "select * from record"; | ||
PreparedStatement ps = c.prepareStatement(sql); | ||
ResultSet rs = ps.executeQuery(); | ||
|
||
while (rs.next()) { | ||
Content content = new Content(); | ||
content.setId(rs.getInt(1)); | ||
content.setUsername(rs.getString(2)); | ||
content.setContent(rs.getString(4)); | ||
content.setStime(rs.getString(3)); | ||
|
||
list.add(content); | ||
} | ||
|
||
ps.close(); | ||
c.close(); | ||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
return list; | ||
} | ||
} |
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,50 @@ | ||
package dao; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
import bean.User; | ||
|
||
public class UserDAO { | ||
|
||
public static void main(String[] args) { | ||
User p = new UserDAO().getUser("tom", "123"); | ||
if (p == null) System.out.println(false); | ||
else System.out.println(true); | ||
} | ||
|
||
public User getUser(String name, String password) { | ||
User r = null; | ||
try { | ||
Class.forName("com.mysql.jdbc.Driver"); | ||
Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/chatroom?characterEncoding=UTF-8", | ||
"root", "password"); | ||
String sql = "select * from user where name = ? and password = ?"; | ||
PreparedStatement ps = c.prepareStatement(sql); | ||
ps.setString(1, name); | ||
ps.setString(2, password); | ||
ResultSet rs = ps.executeQuery(); | ||
|
||
if (rs.next()) { | ||
r = new User(); | ||
r.setName(name); | ||
r.setPassword(password); | ||
r.setId(rs.getInt(1)); | ||
} | ||
|
||
ps.close(); | ||
c.close(); | ||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
return r; | ||
} | ||
} |
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 @@ | ||
package servlet; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import dao.ContentDAO; | ||
import bean.Content; | ||
|
||
public class GetContentServlet extends HttpServlet{ | ||
protected void service(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
List<Content> list = new ContentDAO().ListContent(); | ||
request.setAttribute("contents", list); | ||
request.getRequestDispatcher("interface.jsp").forward(request, response); | ||
} | ||
|
||
} |
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,5 @@ | ||
package servlet; | ||
|
||
public class StorageServlet { | ||
|
||
} |
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 servlet; | ||
|
||
import java.io.IOException; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.util.Date; | ||
|
||
import java.util.Enumeration; | ||
|
||
import dao.ContentDAO; | ||
import bean.Content; | ||
import bean.User; | ||
|
||
public class SubmitServlet extends HttpServlet { | ||
protected void service(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
|
||
/*Enumeration paramNames = request.getSession().getAttributeNames(); | ||
while (paramNames.hasMoreElements()) { | ||
String paraName = (String)paramNames.nextElement(); | ||
System.out.println(paraName); | ||
}*/ | ||
String text = request.getParameter("text"); | ||
|
||
User user = (User) request.getSession().getAttribute("user"); | ||
|
||
Content p = new Content(); | ||
p.setContent(text); | ||
p.setUsername(user.getName()); | ||
p.setStime(new Date().toLocaleString()); | ||
|
||
new ContentDAO().addContent(p); | ||
new GetContentServlet().service(request, response); | ||
} | ||
} |
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,29 @@ | ||
package servlet; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import bean.User; | ||
import dao.UserDAO; | ||
|
||
public class UserLoginServlet extends HttpServlet{ | ||
protected void service(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
String name = request.getParameter("user"); | ||
String password = request.getParameter("password"); | ||
System.out.println(name); | ||
System.out.println(password); | ||
User p = new UserDAO().getUser(name, password); | ||
if (p != null) { | ||
request.getSession().setAttribute("user", p); | ||
response.sendRedirect("/content"); | ||
} | ||
else { | ||
response.sendRedirect("/login.jsp"); | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<web-app> | ||
|
||
<servlet> | ||
<servlet-name>GetContentServlet</servlet-name> | ||
<servlet-class>servlet.GetContentServlet</servlet-class> | ||
</servlet> | ||
|
||
<servlet-mapping> | ||
<servlet-name>GetContentServlet</servlet-name> | ||
<url-pattern>/content</url-pattern> | ||
</servlet-mapping> | ||
|
||
<servlet> | ||
<servlet-name>Login</servlet-name> | ||
<servlet-class>servlet.UserLoginServlet</servlet-class> | ||
</servlet> | ||
|
||
<servlet-mapping> | ||
<servlet-name>Login</servlet-name> | ||
<url-pattern>/login</url-pattern> | ||
</servlet-mapping> | ||
|
||
<servlet> | ||
<servlet-name>submit_content</servlet-name> | ||
<servlet-class>servlet.SubmitServlet</servlet-class> | ||
</servlet> | ||
|
||
<servlet-mapping> | ||
<servlet-name>submit_content</servlet-name> | ||
<url-pattern>/submit_content</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,25 @@ | ||
<%@ page language="java" contentType="text/html; charset=UTF-8" | ||
pageEncoding="UTF-8" import="java.util.*"%> | ||
<!DOCTYPE html> | ||
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> | ||
|
||
<c:if test="${!empty user}" > | ||
<div align="center"> | ||
当前用户: ${user.name} <br> | ||
</div> | ||
</c:if> | ||
|
||
|
||
<%@ include file="record.jsp" %> | ||
|
||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
|
||
|
||
<form action="submit_content" method="post"> | ||
输入聊天内容: <input type = "text" name = "text" /> | ||
<!--style = "height:300px;width:500px;" /> --> | ||
<br> | ||
<input type = "submit" value = "submit" /> | ||
</form> | ||
|
||
<!-- <div>留 言<br> <textarea name="textfield3" cols="30" rows="3"></textarea> --> |
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,12 @@ | ||
<%@ page language="java" contentType="text/html; charset=UTF-8" | ||
pageEncoding="UTF-8" import="java.util.*"%> | ||
|
||
<!DOCTYPE html> | ||
|
||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
|
||
<form action="login" method="post"> | ||
账号: <input type = "text" name = "user" /> <br> | ||
密码: <input type = "password" name = "password" /> <br> | ||
<input type="submit" value = "登录"> | ||
</form> |
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,12 @@ | ||
<%@ page language="java" contentType="text/html; charset=UTF-8" | ||
pageEncoding="UTF-8" import="java.util.*"%> | ||
|
||
|
||
|
||
<c:forEach items="${contents}" var="content" varStatus="st"> | ||
<font color="#FF0000"> ${content.stime} 12333</font> <br> | ||
<Strong> <font color="#000079"> ${content.username}: </font> </Strong> | ||
${content.content} <br> <br> | ||
</c:forEach> | ||
|
||
|