日期:2014-05-17  浏览次数:20848 次

请大家改正servlet小程序 ,很简单的,但是我初手所以不懂
public class showMessage extends HttpServlet {

/**

*/
private static final long serialVersionUID = -102253422187482716L;
/**
* Constructor of the object.
*/
public showMessage() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int result = 0;  
String dbpath = "/WEB-INF/compay.accdb"; 

// --连接字符串 

String url = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+ dbpath; 

Connection conn = null; 

try { 

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

} catch (ClassNotFoundException e) { 

e.printStackTrace(); 



try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {

e.printStackTrace();


Statement statement;
try {


ResultSet rs = statement.executeQuery("select * from worker");
int id = rs.getInt(1);
String name = rs.getString(2);
int phone = rs.getInt(3);
String email = rs.getString(4);
String content = rs.getString(5);

  while(rs.next()){
  statement = conn.createStatement();
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>display messages</title></head>");
out.println("<body>");
out.println("<a href=\"" + request.getContextPath() + "/addMessage.htm\">添加新的留言内容</a><br>");
out.println("留言内容<br><br>");
try {
while (rs.next()) {
out.println("<table width=\"600\" border=\"1\" style=\"table-layout:fixed;word-break:break-all\">");
out.println("<tr><td width=\"50\">编号</td>");
out.println("<td width=\"550\">" + id+ "</td></tr>");
out.println("<tr><td>姓名</td><td>" + name + "</td></tr>");
out.println("<tr><td>电话</td><td>" + phone + "</td></tr>");
out.println("<tr><td>email</td><td>" + email + "</td></tr>");
out.println("<tr><td valign=\"top\">主题</td><td>" + rs.getString("title") + " </td></tr>");
out.println("<tr><td valign=\"top\">内容</td>");
out.println("<td>" +content + "</td></tr>");
  out.println(" </td></tr>");
out.println("</table><br>")