Servlet code :
/*
* Created on 2005/3/1--下午 02:10:35 by jack
*/
package crm.action;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import crm.form.CandidateForm;
import crm.form.DocnoForm;
import crm.form.ElectionForm;
import crm.form.ResolveForm;
import crm.form.ShmmosForm;
import crm.lang.Access;
/**
* @author jack
* @version 1.0, 2005/3/1
*/
public class SocketServlet extends HttpServlet {
private static final long serialVersionUID = 3257288011091031095L;
private final static Logger log = Logger.getLogger(SocketServlet.class);
private Connection con = null;
private final DocnoForm docno = new DocnoForm();
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
log.debug("method=" + method);
if (method == null) {
message(response, "method is null");
return;
}
if (method.equalsIgnoreCase("SHMMOS")) {
resultShmmos(request, response);
} else if (method.equalsIgnoreCase("DOCNO")) {
resultDocno(request, response);
} else if (method.equalsIgnoreCase("RESOLVE")) {
resultResolve(request, response);
} else if (method.equalsIgnoreCase("ELECTION")) {
modifyElection(request, response);
} else if (method.equalsIgnoreCase("CANDIDATE")) {
resultCandidate(request, response);
} else if (method.equalsIgnoreCase("SELECT_ELECTION")) {
resultElection(request, response);
} else if (method.equalsIgnoreCase("UPDATE_ELECTION")) {
updateElection(request, response);
} else {
message(response, "Not match method");
return;
}
}
private synchronized void updateElection(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// FIXME delete election
String candidate = request.getParameter("candidate");
if ((candidate == null) || (candidate.equals(""))) {
message(response, "key 值為空白");
return;
}
try {
int sn = Integer.parseInt(candidate);
String sql = "delete from election where candidate=? ";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, sn);
pstmt.execute();
pstmt.close();
} catch (SQLException e) {
log.error(e);
message(response, e.toString());
return;
}
modifyElection(request, response);
}
private synchronized void modifyElection(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String[] lst = request.getParameterValues("election");
String category = request.getParameter("category");
log.debug("category=" + category);
ElectionForm elect = null;
try {
con.setAutoCommit(true);
String sql = "select pg_insert_election(?, ?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(sql);
for (int i = 0; i < lst.length; i++) {
elect = ElectionForm.load(lst[i]);
pstmt.setInt(1, elect.getCandidate());
pstmt.setInt(2, elect.getPsntid());
pstmt.setDouble(3, elect.getVoteqty());
pstmt.setDouble(4, elect.getBalance());
log.debug("elect.getBalance()="+elect.getBalance());
pstmt.executeQuery();
}
con.setAutoCommit(false);
pstmt.close();
pstmt = null;
} catch (SQLException e) {
log.error(e);
message(response, e.toString());
return;
}
message(response, "success");
}
private void resultCandidate(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
CandidateForm candidate = new CandidateForm();
ArrayList lst = new ArrayList();
try {
String sql = "select * from candidate where 1=? order by sn ";
Object[] obj = { new Integer(1) };
lst = (ArrayList) Access.executeQuery(con, sql, obj, candidate);
} catch (SQLException e) {
log.error(e);
message(response, e.toString());
return;
}
ObjectOutputStream out = new ObjectOutputStream(response
.getOutputStream());
out.writeObject(lst);
out.flush();
out.close();
}
private void resultResolve(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String[] lst = request.getParameterValues("resolve");
try {
String sql = "insert into resolve values(?,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
ResolveForm resolve = null;
for (int i = 0; i < lst.length; i++) {
resolve = ResolveForm.load(lst[i]);
log.debug(resolve.toString());
pstmt.setInt(1, resolve.getBill());
pstmt.setInt(2, resolve.getPsntid());
pstmt.setString(3, resolve.getChoose());
pstmt.addBatch();
}
con.setAutoCommit(true);
int[] cnt = pstmt.executeBatch();
} catch (SQLException e) {
log.error(e);
message(response, e.toString());
return;
}
message(response, "success");
}
private void message(HttpServletResponse response, String e)
throws ServletException, IOException {
log.info("send message");
DataOutputStream out = new DataOutputStream(response.getOutputStream());
out.writeBytes(e);
out.flush();
out.close();
}
private void resultDocno(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
DocnoForm docno = new DocnoForm();
try {
String sql = "select * from docno where 1=? ";
Object[] obj = { new Integer(1) };
ArrayList lst = (ArrayList) Access.executeQuery(con, sql, obj,
docno);
if ((lst != null) && (!lst.isEmpty())) {
docno = (DocnoForm) lst.get(0);
}
} catch (SQLException e) {
log.error(e);
message(response, e.toString());
return;
}
ObjectOutputStream out = new ObjectOutputStream(response
.getOutputStream());
out.writeObject(docno);
out.flush();
out.close();
}
private void resultShmmos(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String psntid = request.getParameter("psntid");
if (psntid == null) {
return;
}
ShmmosForm shmmos = new ShmmosForm();
try {
String sql = "select * from shmmos where psntid=? ";
Object[] obj = { new Integer(psntid) };
ArrayList lst = (ArrayList) Access.executeQuery(con, sql, obj,
shmmos);
if ((lst != null) && (!lst.isEmpty())) {
shmmos = (ShmmosForm) lst.get(0);
}
} catch (SQLException e) {
log.error(e);
message(response, e.toString());
return;
}
ObjectOutputStream out = new ObjectOutputStream(response
.getOutputStream());
out.writeObject(shmmos);
out.flush();
out.close();
}
private void resultElection(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String candidate = request.getParameter("candidate");
if (candidate == null) {
return;
}
ElectionForm elect = new ElectionForm();
ArrayList lst = null;
try {
String sql = "select oid, * from election where candidate=? ";
Object[] obj = { new Integer(candidate) };
lst = (ArrayList) Access.executeQuery(con, sql, obj, elect);
} catch (SQLException e) {
log.error(e);
message(response, e.toString());
return;
}
if ((lst != null) && (!lst.isEmpty())) {
ObjectOutputStream out = new ObjectOutputStream(response
.getOutputStream());
out.writeObject(lst);
out.flush();
out.close();
}
}
public void destroy() {
if (con != null) {
try {
con.close();
con = null;
} catch (SQLException e) {
}
}
}
public void init(ServletConfig config) throws ServletException {
String url = config.getInitParameter("url");
String driver = config.getInitParameter("driver");
String user = config.getInitParameter("user");
String password = config.getInitParameter("password");
try {
Class.forName(driver);
try {
con = DriverManager.getConnection(url, user, password);
log.info(SocketServlet.class.getName() + " is inited");
String sql = "select * from docno ";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
docno.setDirector(rs.getInt("director"));
docno.setSupervisor(rs.getInt("supervisor"));
}
log.debug("docno.getDirector()=" + docno.getDirector());
rs.close();
ps.close();
} catch (SQLException e) {
log.error(e);
}
} catch (ClassNotFoundException e) {
log.error(e);
}
}
}
* Created on 2005/3/1--下午 02:10:35 by jack
*/
package crm.action;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import crm.form.CandidateForm;
import crm.form.DocnoForm;
import crm.form.ElectionForm;
import crm.form.ResolveForm;
import crm.form.ShmmosForm;
import crm.lang.Access;
/**
* @author jack
* @version 1.0, 2005/3/1
*/
public class SocketServlet extends HttpServlet {
private static final long serialVersionUID = 3257288011091031095L;
private final static Logger log = Logger.getLogger(SocketServlet.class);
private Connection con = null;
private final DocnoForm docno = new DocnoForm();
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
log.debug("method=" + method);
if (method == null) {
message(response, "method is null");
return;
}
if (method.equalsIgnoreCase("SHMMOS")) {
resultShmmos(request, response);
} else if (method.equalsIgnoreCase("DOCNO")) {
resultDocno(request, response);
} else if (method.equalsIgnoreCase("RESOLVE")) {
resultResolve(request, response);
} else if (method.equalsIgnoreCase("ELECTION")) {
modifyElection(request, response);
} else if (method.equalsIgnoreCase("CANDIDATE")) {
resultCandidate(request, response);
} else if (method.equalsIgnoreCase("SELECT_ELECTION")) {
resultElection(request, response);
} else if (method.equalsIgnoreCase("UPDATE_ELECTION")) {
updateElection(request, response);
} else {
message(response, "Not match method");
return;
}
}
private synchronized void updateElection(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// FIXME delete election
String candidate = request.getParameter("candidate");
if ((candidate == null) || (candidate.equals(""))) {
message(response, "key 值為空白");
return;
}
try {
int sn = Integer.parseInt(candidate);
String sql = "delete from election where candidate=? ";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, sn);
pstmt.execute();
pstmt.close();
} catch (SQLException e) {
log.error(e);
message(response, e.toString());
return;
}
modifyElection(request, response);
}
private synchronized void modifyElection(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String[] lst = request.getParameterValues("election");
String category = request.getParameter("category");
log.debug("category=" + category);
ElectionForm elect = null;
try {
con.setAutoCommit(true);
String sql = "select pg_insert_election(?, ?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(sql);
for (int i = 0; i < lst.length; i++) {
elect = ElectionForm.load(lst[i]);
pstmt.setInt(1, elect.getCandidate());
pstmt.setInt(2, elect.getPsntid());
pstmt.setDouble(3, elect.getVoteqty());
pstmt.setDouble(4, elect.getBalance());
log.debug("elect.getBalance()="+elect.getBalance());
pstmt.executeQuery();
}
con.setAutoCommit(false);
pstmt.close();
pstmt = null;
} catch (SQLException e) {
log.error(e);
message(response, e.toString());
return;
}
message(response, "success");
}
private void resultCandidate(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
CandidateForm candidate = new CandidateForm();
ArrayList lst = new ArrayList();
try {
String sql = "select * from candidate where 1=? order by sn ";
Object[] obj = { new Integer(1) };
lst = (ArrayList) Access.executeQuery(con, sql, obj, candidate);
} catch (SQLException e) {
log.error(e);
message(response, e.toString());
return;
}
ObjectOutputStream out = new ObjectOutputStream(response
.getOutputStream());
out.writeObject(lst);
out.flush();
out.close();
}
private void resultResolve(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String[] lst = request.getParameterValues("resolve");
try {
String sql = "insert into resolve values(?,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
ResolveForm resolve = null;
for (int i = 0; i < lst.length; i++) {
resolve = ResolveForm.load(lst[i]);
log.debug(resolve.toString());
pstmt.setInt(1, resolve.getBill());
pstmt.setInt(2, resolve.getPsntid());
pstmt.setString(3, resolve.getChoose());
pstmt.addBatch();
}
con.setAutoCommit(true);
int[] cnt = pstmt.executeBatch();
} catch (SQLException e) {
log.error(e);
message(response, e.toString());
return;
}
message(response, "success");
}
private void message(HttpServletResponse response, String e)
throws ServletException, IOException {
log.info("send message");
DataOutputStream out = new DataOutputStream(response.getOutputStream());
out.writeBytes(e);
out.flush();
out.close();
}
private void resultDocno(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
DocnoForm docno = new DocnoForm();
try {
String sql = "select * from docno where 1=? ";
Object[] obj = { new Integer(1) };
ArrayList lst = (ArrayList) Access.executeQuery(con, sql, obj,
docno);
if ((lst != null) && (!lst.isEmpty())) {
docno = (DocnoForm) lst.get(0);
}
} catch (SQLException e) {
log.error(e);
message(response, e.toString());
return;
}
ObjectOutputStream out = new ObjectOutputStream(response
.getOutputStream());
out.writeObject(docno);
out.flush();
out.close();
}
private void resultShmmos(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String psntid = request.getParameter("psntid");
if (psntid == null) {
return;
}
ShmmosForm shmmos = new ShmmosForm();
try {
String sql = "select * from shmmos where psntid=? ";
Object[] obj = { new Integer(psntid) };
ArrayList lst = (ArrayList) Access.executeQuery(con, sql, obj,
shmmos);
if ((lst != null) && (!lst.isEmpty())) {
shmmos = (ShmmosForm) lst.get(0);
}
} catch (SQLException e) {
log.error(e);
message(response, e.toString());
return;
}
ObjectOutputStream out = new ObjectOutputStream(response
.getOutputStream());
out.writeObject(shmmos);
out.flush();
out.close();
}
private void resultElection(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String candidate = request.getParameter("candidate");
if (candidate == null) {
return;
}
ElectionForm elect = new ElectionForm();
ArrayList lst = null;
try {
String sql = "select oid, * from election where candidate=? ";
Object[] obj = { new Integer(candidate) };
lst = (ArrayList) Access.executeQuery(con, sql, obj, elect);
} catch (SQLException e) {
log.error(e);
message(response, e.toString());
return;
}
if ((lst != null) && (!lst.isEmpty())) {
ObjectOutputStream out = new ObjectOutputStream(response
.getOutputStream());
out.writeObject(lst);
out.flush();
out.close();
}
}
public void destroy() {
if (con != null) {
try {
con.close();
con = null;
} catch (SQLException e) {
}
}
}
public void init(ServletConfig config) throws ServletException {
String url = config.getInitParameter("url");
String driver = config.getInitParameter("driver");
String user = config.getInitParameter("user");
String password = config.getInitParameter("password");
try {
Class.forName(driver);
try {
con = DriverManager.getConnection(url, user, password);
log.info(SocketServlet.class.getName() + " is inited");
String sql = "select * from docno ";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
docno.setDirector(rs.getInt("director"));
docno.setSupervisor(rs.getInt("supervisor"));
}
log.debug("docno.getDirector()=" + docno.getDirector());
rs.close();
ps.close();
} catch (SQLException e) {
log.error(e);
}
} catch (ClassNotFoundException e) {
log.error(e);
}
}
}