📄 zipdbservlet.java
字号:
/*** * Excerpted from "Pragmatic Ajax" * We make no guarantees that this code is fit for any purpose. * Visit http://www.pragmaticprogrammer.com/titles/ajax for more book information.***/package ajaxian.book.crm.servlet;
import ajaxian.book.crm.model.ZipCityState;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletConfig;
import java.io.PrintWriter;
import java.io.IOException;
import java.sql.*;
public class ZipDbServlet extends HttpServlet {
private boolean debug = false;
public void init(ServletConfig servletConfig) {
if ("true".equals(servletConfig.getInitParameter("DEBUG"))) {
debug = true;
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String zip = request.getParameter("zip");
ZipCityState zcs = null;
if (zip != null) {
try {
zcs = getZipCityState(zip);
} catch (SQLException e) {
debug(e.toString());
zcs = new ZipCityState(zip, "Durham", "NC");
}
} else {
zcs = new ZipCityState(zip, "No City", "XX");
}
String type = request.getParameter("type");
if ("html".equalsIgnoreCase(type)) {
debug("Returning HTML result for zip: " + zip);
out.println("<th>City:</th><td><input id='city' type='text' name='city' value='" + zcs.getCity() + "'/></td><th>State:</th><td><input id='state' type='text' name='state' size='2' maxlength='2' value='" + zcs.getState() + "'/></td>");
/*
<tr id="rewrite">
<th>City:</th>
<td><input id="city" type="text" name="city"/></td>
<th>State:</th>
<td><input id="state" type="text" name="state"/></td>
</tr>
*/
} else if ("eval".equalsIgnoreCase(type)) {
debug("Returning eval result for zip: " + zip);
out.println("document.getElementById('city').value = '" + zcs.getCity() + "';document.getElementById('state').value = '" + zcs.getState() + "';document.getElementById('zipError').innerHTML = '';");
/*
document.getElementById('city').value = 'Boulder';
document.getElementById('state').value = 'CO';
document.getElementById('zipError').innerHTML = '';
*/
} else if ("evalproto".equalsIgnoreCase(type)) {
debug("Returning prototype style eval result for zip: " + zip);
out.println("$('city').value = '" + zcs.getCity() + "';$('state').value = '" + zcs.getState() + "';$('zipError').innerHTML = '';");
/*
$('city').value = 'Boulder';
$('state').value = 'CO';
$('zipError').innerHTML = '';
*/
} else {
debug("Returning 'City,State' result for zip: " + zip);
if(zcs != null) {
out.println(zcs.getCity() + "," + zcs.getState());
} else {
out.println("Durham,NC");
}
}
}
private static ZipCityState getZipCityState(String zip) throws SQLException {
PreparedStatement ps = getConnection().prepareStatement("SELECT city, state FROM zips WHERE zip=?");
ps.setString(1, zip);
ResultSet rs = ps.executeQuery();
rs.next();
return new ZipCityState(zip, rs.getString("city"), rs.getString("state"));
}
private static Connection getConnection() throws SQLException {
try {
Class.forName("org.hsqldb.jdbcDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return DriverManager.getConnection("jdbc:hsqldb:hsql://localhost:8585/crm", "sa", "");
}
/**
* Cheesy logging
* @param info
*/
private void debug(String info) {
if (debug) System.out.println(info);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -