Online Book Reader

Home Category

AJAX In Action [214]

By Root 4012 0
want for any of the users in the database table users. You can see in figure 11.10 that we have added three DHTML windows for user 1.

In figure 11.11, the three DHTML window parameters give us the information needed to create three windows on the screen with different dimensions and positions. The three windows in this table display three different websites: JavaRanch, Google, and Eric’s Ajax Blog. Now that the database table has been built, we have to get this information to the user when he logs into the portal. You’ll see how straightforward this is in the next section.

Figure 11.11 Data entered for the user with the id of 1

Licensed to jonathan zheng

Implementing DHTML windows

441

11.4.2 The portal window’s server-side code

Let’s assume that our login request has made it through the security filter. The next step is to retrieve the list of portal windows for our authenticated user and send back the JavaScript telling the browser what to display. We define a PortalWindow object that represents a row of data in the database, as shown in listing 11.7.

Listing 11.7 PortalWindow.java

public class PortalWindow {

private int id=-1;

private User user=null;

private int xPos=0;

private int yPos=0;

private int width=0;

private int height=0;

private String url=null;

private String title=null;

public PortalWindow(

int id, User user, int xPos, int yPos,

int width,int height,

String url, String title

) {

this.id = id;

this.user = user;

this.xPos = xPos;

this.yPos = yPos;

this.width = width;

this.height = height;

this.url = url;

this.title = title;

}

public int getHeight() {return height;}

public void setHeight(int height) {this.height = height;}

public int getId() {return id;}

public void setId(int id) {this.id = id;}

public String getTitle() {return title;}

public void setTitle(String title) {this.title = title;}

public String getUrl() {return url;}

public void setUrl(String url) {this.url = url;}

public User getUser() {return user;}

public void setUser(User user) {this.user = user;}

public int getWidth() {return width;}

public void setWidth(int width) {this.width = width;}

public int getXPos() {return xPos;}

public void setXPos(int pos) {xPos = pos;}

Licensed to jonathan zheng

442

CHAPTER 11

The enhanced Ajax web portal

public int getYPos() {return yPos;}

public void setYPos(int pos) {yPos = pos;}

}

Again, this object is pretty much a straightforward mapping of the database structure. In production, we’d probably use an ORM system such as Hibernate or iBATIS

to help us out, but we want to keep things fairly simple and platform-agnostic for now. Note that we provide setter methods as well as getters for this object, because we’ll want to update these objects dynamically in response to user events. The URL that we requested on login, portalLogin.servlet, is mapped to a servlet that retrieves all the portal windows for that user and sends back JavaScript instructions. Listing 11.8 shows the main servlet.

Listing 11.8 SelectServlet.java (mapped to 'portalLogin.servlet')

public class SelectServlet extends HttpServlet {

protected void doPost(

HttpServletRequest request,

HttpServletResponse response

) throws ServletException, IOException {

HttpSession session=request.getSession();

User user=(User)

(session.getAttribute("user")); b

Check session

StringBuffer jsBuf=new StringBuffer();

if (user==null){

jsBuf.append(JSUtil.logout());

}else{

c Define object

List windows=DBUtil

.getPortalWindows(user);

jsBuf.append(JSUtil.initUI());

d Utilize the JSUtil object

for (Iterator iter=windows.iterator();iter.hasNext();){

PortalWindow window=(PortalWindow)(iter.next());

session.setAttribute("window_"+window.getId(),window); jsBuf.append

(JSUtil.initWindow(window)); e

Declare portal window

}

}

Writer writer=response.getWriter();

writer.write(jsBuf.toString()); f

Write to output stream

writer.flush();

}

}

Again, we use the DBUtil object to abstract out the database interactions and the JSUtil to generate JavaScript code. DBUtil

Return Main Page Previous Page Next Page

®Online Book Reader