📄 listnotesservlet.java
字号:
package sessions;
import java.util.*;
import java.io.*;
import java.sql.*;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class ListNotesServlet extends NotePadServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String email = request.getParameter("email");
HttpSession session = request.getSession();
String currentEmail = (String) session.getAttribute("email");
if(currentEmail != null) {
if(!currentEmail.equals(email)) {
session.invalidate();
session = request.getSession();
}
}
session.setAttribute("email", email);
Map noteList = (Map) session.getAttribute("noteList");
Connection connection = null;
PreparedStatement statement = null;
if(noteList == null) {
try {
String sql = "SELECT NOTE_ID, NOTE_TITLE FROM NOTES WHERE EMAIL = ?";
connection = getConnection();
statement = connection.prepareStatement(sql);
statement.setString(1, email);
ResultSet rs = statement.executeQuery();
noteList = new HashMap();
while(rs.next()) {
noteList.put(new Integer(rs.getInt(1)), rs.getString(2));
}
} 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) {}
}
session.setAttribute("noteList", noteList);
}
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>Notes</h3>");
if(noteList.size() == 0) {
writer.println("<p>You do not have any notes.</p>");
} else {
writer.println("<p>Click on the note to edit.</p><ul>");
Iterator iterator = noteList.keySet().iterator();
while(iterator.hasNext()) {
Integer noteId = (Integer) iterator.next();
String noteTitle = (String) noteList.get(noteId);
writer.println("<li><a href='/notepad/edit?noteId=" +
noteId.toString() + "'>" + noteTitle + "</a></li>");
}
writer.println("</ul>");
}
writer.println("<p><a href='/notepad/edit'>Add a New Note</a></p>");
writer.println("<p><a href='/notepad/'>Change User</a></p>");
writer.println("</body></html>");
writer.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -