Updating the Table


Changing Entity Values

To show how the beans will work when both retrieving a row and changing it using just the code you've developed so far, let's create an HTML page that allows us to query the database for a specific row, display the values from the resulting bean, and change the values. The HTML is found in two files: getLogin.html and changeLogin.html. The code for getLogin.html is:

 <HTML> <BODY> <TITLE>Login</TITLE> <BR> Enter Login name: <form action="http://localhost:8080/entitlements/entitlements.Login- MaintenanceServlet" method="post">   Username: <input name="login"><BR>   <input type="submit" name="submit" value="get"> </form> </BODY> </HTML> 

The code for changeLogin.html is:

      <%@ page language='java' import='entitlements.Login' %> <% ServletContext app = getServletContext();    Login e = (Login)app.getAttribute("login");  %> <head> <body> <form action="http://localhost:8080/entitlements/entitlements.   LoginMaintenanceServlet" method="post">   Username: <input name="login" value = "      <%= e.getLogin() %>"><BR>   Timestamp: <input name="timestamp" value = "       <%= e.getTs() %>"><BR>   Role: <input name="role" value = "<%= e.getRole() %>"><BR>   Description: <input name="description" value = "      <%= e.getDescription() %>"><BR>   Opendate: <input name="opendate" value = "       <%= e.getOpendate() %>"><BR>   Closedate: <input name="closedate" value = "       <%= e.getClosedate() %>"><BR>   <input type="submit" name="submit" value="update"> </form> </body> </head> 

Both of the pages include a <form> that calls a single servlet called LoginMaintenanceServlet.java. The code for this servlet is:

 package entitlements; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.naming.*; import javax.ejb.*; import java.util.*; public class LoginMaintenanceServlet extends HttpServlet {   private LoginHome home = null;   public void init()     throws ServletException {     try {       Context cmp = (Context)         new InitialContext().lookup("java:comp/env/cmp");       home = (LoginHome) cmp.lookup("Login");     } catch (NamingException e) {       throw new ServletException(e);     }   }   public void doGet(HttpServletRequest req, HttpServletResponse res)     throws IOException, ServletException   {     PrintWriter out = res.getWriter();     res.setContentType("text/html");     String submit = req.getParameter("submit");     if (submit.equals)"get")) {       try {         Login e = (Login)home.findByPrimaryKey(req.getParameter("login"));         ServletContext app = getServletContext();         request.setAttribute("login", e);         RequestDispatcher dispatch =           app.getRequestDispatcher("/changeLogin.jsp");         dispatch.forward(req, res);       } catch (FinderException e) {         throw new ServletException(e);       }     } else if (submit.equals("update")) {       try {         Login e = (Login)home.findByPrimaryKey(req.getParameter("login"));         e.setTs(req.getParameter("timestamp")); e.setRole(req.getParameter("role"));         e.setDescription(req.getParameter("description"));         e.setOpendate(req.getParameter("opendate"));         e.changeClosedate(req.getParameter("closedate"));       } catch (FinderException e) {         throw new ServletException(e);       }     }   }   public void doPost(HttpServletRequest req, HttpServletResponse res)     throws IOException, ServletException {     doGet(req, res);   } } 

As you can see, the servlet checks the value of the submit variable to determine what the user is trying to accomplish. If the action is an update, you create an entity bean from the system and set all of the appropriate fields of the row except the primary key. Obviously, you don't want to change the primary key because this could cause an error that your code doesn't handle at the moment.

Let's run through a query. In Figure 6.4, the user is asking to see the login for johnd using the getLogin.html page. Figure 6.5 shows the changeLogin.html page after the user has submitted the request to the LoginMaintenanceServlet code. In Figure 6.6, we've changed the close date to 1-3-2003. Figure 6.7 shows the result of the change after we've used the getLogin.html page to lookup the johnd login a second time. The bean accepted the close date and either cached it or wrote the value to the database. In Figure 6.8, a query of the database shows that the date was stored in the database—without us having to write an INSERT SQL.

click to expand
Figure 6.4: The getLogin.html page for login johnd.

click to expand
Figure 6.5: The result of getting the johnd login.

click to expand
Figure 6.6: The close date has been added to the data.

click to expand
Figure 6.7: The close date has been submitted and stored in a database.

click to expand
Figure 6.8: We queried the database to see the close date.




Mastering Resin
Mastering Resin
ISBN: 0471431036
EAN: 2147483647
Year: 2002
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net