⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 editnoteservlet.java

📁 JAVA Servlet2.3外文书籍源码
💻 JAVA
字号:
package sessions;

import java.util.Map;
import java.io.*;
import java.sql.*;
import javax.naming.NamingException;
import javax.servlet.*;
import javax.servlet.http.*;

public class EditNoteServlet extends NotePadServlet {
  protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException {
    String noteId = (String) request.getParameter("noteId");
    String note = "";
    String title = "";
    boolean isEdit = false;
    Connection connection = null;
    PreparedStatement statement = null;

    if(noteId != null) {
      try {
        String sql = "SELECT NOTE_TITLE, NOTE FROM NOTES WHERE NOTE_ID = ?";
        connection = getConnection();
        statement = connection.prepareStatement(sql);
        statement.setInt(1, Integer.parseInt(noteId));
        ResultSet rs = statement.executeQuery();
        rs.next();
        title = rs.getString(1);
        note = rs.getString(2);
        isEdit = true;
      } catch(SQLException sqle) {
        throw new ServletException("SQL Exception", sqle);
      } finally {
        try {
          if(statement != null) {
            statement.close();
          }
        } catch(SQLException ignore) {}
        try {
          if(connection != null) {
            connection.close();
          }
        } catch(SQLException ignore) {}
      }
    }
    response.setContentType("text/html");
    PrintWriter writer = response.getWriter();
    writer.println("<html><head>");
    writer.println("<title>NotePad</title>");
    writer.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"style/global.css\" />");
    writer.println("</head><body>");
    writer.println("<h3>Add/Edit a Note</h3>");
    if(isEdit) {
      writer.println("<form action='/notepad/edit?noteId=" + noteId + "' method='POST'>");
    } else {
      writer.println("<form action='/notepad/edit' method='POST'>");
    }
    writer.println("<p>Title: <input type='text' name='title' size='40' value='" + title + "'></p>");
    writer.println("<p><textarea name='note' cols='50' rows='15'>");
    writer.println(note);
    writer.println("</textarea></p>");
    writer.println("<p><input type='Submit' name='submit' value='Save Note'></p>");
    writer.println("</form></body></html>");
    writer.close();
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException {
    String noteId = (String) request.getParameter("noteId");
    HttpSession session = request.getSession();
    String email = (String) session.getAttribute("email");
    Map noteList = (Map) session.getAttribute("noteList");
    String title = request.getParameter("title");
    String note = request.getParameter("note");
    Connection connection = null;
    PreparedStatement statement = null;
    try {
      if(noteId == null) {
        String sql = "INSERT INTO NOTES (EMAIL, NOTE_TITLE, NOTE, LAST_MODIFIED)"
                     + "VALUES(?, ?, ?, ?)";
        connection = getConnection();
        statement = connection.prepareStatement(sql);
        statement.setString(1, email);
        statement.setString(2, title);
        statement.setString(3, note);
        statement.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
        statement.executeUpdate();
        
        // Retrieve the automatically inserted NOTE_ID
        sql = "SELECT LAST_INSERT_ID()";
        statement = connection.prepareStatement(sql);
        ResultSet rs = statement.executeQuery(sql);
        int id = 0;
        while(rs.next ()) {
          id = rs.getInt(1);
        }
        noteList.put(new Integer(id), title);
      } else {
        String sql = "UPDATE NOTES SET NOTE_TITLE = ?, NOTE = ?, " + 
                     "LAST_MODIFIED = ? WHERE NOTE_ID = ?";
        connection = getConnection();
        statement = connection.prepareStatement(sql);
        statement.setString(1, title);
        statement.setString(2, note);
        statement.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
        statement.setInt(4, Integer.parseInt(noteId));
        statement.executeUpdate();

        noteList.put(new Integer(noteId), title);
      }
    } catch(SQLException sqle) {
      throw new ServletException("SQL Exception", sqle);
    } finally {
      try {
        if(statement != null) {
          statement.close();
        }
      } catch(SQLException ignore) {}
      try {
        if(connection != null) {
          connection.close();
        }
      } catch(SQLException ignore) {}
    }
    RequestDispatcher rd = request.getRequestDispatcher("/list?email=" + email);
    rd.forward(request, response);
  }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -