Updating the User Profile

The updateprofile.jsp page, used if the user makes new project selections in the main page and clicks Update Profile, is also invoked through the POST method. It follows the same access-control approach as the storemsg.jsp page and is shown in Example 13-6. But what’s more interesting with this page is that it shows how to replace multivalue bean and database data, and is an instance of when you need to care about database transactions.

Example 13-6. Updating multiple database rows (updateprofile.jsp)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
  
<%-- Verify that the user is logged in --%>
<c:if test="${validUser == null}">
  <jsp:forward page="login.jsp">
    <jsp:param name="origURL" value="${pageContext.request.requestURL}" />
    <jsp:param name="errorMsg" value="Please log in first." />
  </jsp:forward> 
</c:if>
  
<%-- Verify that it's a POST method --%>
<c:if test="${pageContext.request.method != 'POST'}">
  <c:redirect url="main.jsp" />
</c:if>
  
<%-- Update the project list in the bean --%>
<c:set target="${validUser}" property="projects" value="${paramValues.projects}" /> <sql:transaction> <%-- Delete the old project (if any) and insert the new ones --%> <sql:update> DELETE FROM EmployeeProjects WHERE UserName = ? <sql:param value="${validUser.userName}" /> </sql:update> <c:forEach items="${validUser.projects}" var="project"> <sql:update> INSERT INTO EmployeeProjects (UserName, ProjectName) ...

Get JavaServer Pages, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.