AJAX In Action [219]
All we have left to do is to extract the values from our form submission. The values were sent by our XMLHttpRequest object, which was triggered by the onmouseup event handlers. We need to create our SQL query with this information and update the record in the database to save the new information. We define an UpdateServlet for this purpose, which is shown in listing 11.11. Listing 11.11 UpdateServlet.java (mapped to 'saveSettings.servlet') public class UpdateServlet extends HttpServlet {
protected void doPost(
HttpServletRequest request,
HttpServletResponse response
)throws ServletException, IOException{
String windowId=
request.getParameter("ref"); b
Get unique ID from request
HttpSession session=request.getSession();
PortalWindow window=(PortalWindow)
(session.getAttribute
("window_"+windowId)); c
Get Window object from session
window.setXPos(getIntParam(request,"x"));
Licensed to jonathan zheng 452 CHAPTER 11 The enhanced Ajax web portal window.setYPos(getIntParam(request,"y")); window.setWidth(getIntParam(request,"w")); window.setHeight(getIntParam(request,"h")); DBUtil.savePortalWindow(window); d Save changes Writer writer=response.getWriter(); writer.write("Save Complete"); Return simple text reply writer.flush(); } private int getIntParam(HttpServletRequest request, String param) { String str=request.getParameter(param); int result=Integer.parseInt(str); return result; } } Given the window ID as a request parameter b, we can extract the PortalWindow from session c and update its geometry based on further request parameters. We then call another method on our DBUtil object to save the portal window settings in the database d. Again, the implementation that we’ve provided here in listing 11.12 has been written to be simple and easy to translate to other languages. Listing 11.12 savePortalWindows() method public static void savePortalWindow(PortalWindow window){ Connection conn=getConnection(); int x=window.getXPos(); int y=window.getYPos(); int w=window.getWidth(); int h=window.getHeight(); int id=window.getId(); String sql="UPDATE portal_windows SET xPos="+x +",yPos="+y +",width="+w +",height="+h +" WHERE id="+id; try{ Statement stmt=conn.createStatement(); stmt.execute(sql); stmt.close(); }catch (SQLException sqlex){ } } The code in listing 11.12 is very straightforward. We read the relevant details from the PortalWindow object and construct a SQL update statement accordingly. Rather than returning any JavaScript this time, we issue a simple text acknowledgment. Licensed to jonathan zheng Refactoring 453 To test the new functionality, log into the portal as our test user. Drag the windows around the screen, and resize them so they are in different positions from their defaults. Close the browser to force an end to the session. When we reopen the browser and log back into the portal as that user, we see the windows in the same position. Move the windows to a new position and look at the database table. We are automatically saving the user’s preferences without him even knowing it. We’ve now provided all the basic functionality for a working portal system, including a few things that a classic web application just couldn’t do. There are several other requirements that we could classify as “nice to have,” such as being able to add, remove, and rename windows. Because of limited space, we are not going to discuss them here. The full code for the portal application is available to download and includes the ability to add, delete, rename, and adjust the window’s properties without leaving the single portal page. If you have any questions about the code in this section or need a more thorough understanding, you can always reach us on Manning.com’s Author Online at www.manning.com. Our code so far has been somewhat rough and ready so that we could demonstrate how the individual pieces work. Let’s hand it