jsp页面:
<%@ page language="java" contentType="text/html; charset=gbk"%>
<%@page import="java.util.ArrayList"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for IndexForm form</title>
</head>
<body>
<center>
<h1>很2B的网络群聊天室</h1>
<br> <hr><font color="red">
<%
ArrayList list = new ArrayList();
list = (ArrayList)request.getAttribute("content");
if(list!=null){
for(int i=0;i<list.size();i++){
out.println(list.get(i)+"<br>");
}
}
%>
</font>
<hr/>
<html:form action="/index">
请输入 : <html:text property="content"/><html:errors property="content"/><br/>
<html:submit value="提交"/><html:reset value="重置"/>
</html:form>
</center>
</body>
</html>
ActionForm页面:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class IndexForm extends ActionForm {
private String content;
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
action页面:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.yourcompany.struts.dao.JdbcUtil;
import com.yourcompany.struts.form.IndexForm;
public class IndexAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
try {
request.setCharacterEncoding("gbk");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
IndexForm indexForm = (IndexForm) form;
ArrayList list = new ArrayList();
String content = indexForm.getContent();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Statement stmt = JdbcUtil.conn().createStatement();
stmt.executeUpdate("insert into online(content,time) values('"+content+"','"+sdf.format(new Date())+"')");
ResultSet rs = stmt.executeQuery("select content,time from online order by ID desc limit 10");
while(rs.next()){
list.add(rs.getString("time"));
list.add(rs.getString("content"));
}
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("content", list);
return mapping.findForward("ok");
}
}
DAO页面:
package com.yourcompany.struts.dao;
import java.sql.Connection;
import java.sql.DriverManager;
public class JdbcUtil {
public static Connection conn(){
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root", "PING");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
}
过滤器Filter页面:
package com.yourcompany.struts.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharactorEncodingFilter implements Filter{
private FilterConfig config;
private String encode;
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
arg0.setCharacterEncoding(encode);
arg1.setCharacterEncoding(encode);
arg2.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
encode = arg0.getInitParameter("encode");
}
}