Advertisement
stronk_8s

java jstl/el

Feb 24th, 2025 (edited)
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 2.45 KB | Source Code | 0 0
  1. //web.xml
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <web-app version="4.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
  4.     <session-config>
  5.         <session-timeout>
  6.             30
  7.         </session-timeout>
  8.     </session-config>
  9.     <resource-ref>
  10.         <res-ref-name>jdbc/ass1</res-ref-name>
  11.         <res-type>javax.sql.DataSource</res-type>
  12.         <res-auth>Container</res-auth>
  13.         <res-sharing-scope>Shareable</res-sharing-scope>
  14.     </resource-ref>
  15. </web-app>
  16.  
  17.  
  18.  
  19.  
  20. //pom.xml
  21. <dependencies>
  22.         <dependency>
  23.             <groupId>com.mysql</groupId>
  24.             <artifactId>mysql-connector-j</artifactId>
  25.             <version>8.0.33</version>
  26.         </dependency>
  27. </dependencies>
  28.  
  29.  
  30. //index.jsp
  31.  
  32. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  33. <%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
  34. <sql:setDataSource dataSource="jdbc/ass1"/>
  35.  
  36.             <sql:query var="result">
  37.                 SELECT * FROM Student
  38.             </sql:query>
  39.  
  40. <c:forEach var="sql" items="${result.rowsByIndex}">
  41.             <table border="2">
  42.                 <tr>
  43.                     <td>${sql[0]}</td>
  44.                     <td>${sql[1]}</td>
  45.                     <td>${sql[2]}</td>
  46.                 </tr>
  47.             </table>
  48.         </c:forEach>
  49.  
  50.  
  51.  
  52. //registration.jsp
  53.  
  54. <sql:setDataSource dataSource="jdbc/urvil"/>
  55.  
  56.         <c:if test="${not empty param.name and not empty param.age}">
  57.             <!-- Insert Data into Database -->
  58.             <sql:update var="result">
  59.                 INSERT INTO student(id, name, age)
  60.                 VALUES
  61.                 (null, '${param.name}', '${param.age}')
  62.             </sql:update>
  63.  
  64.             <!-- Redirect to newjsp.jsp if the registration is successful -->
  65.             <c:if test="${result != null}">
  66.                 <p>Registration successful! Redirecting...</p>
  67.                 <c:redirect url="newjsp.jsp"/>
  68.             </c:if>
  69.         </c:if>
  70.  
  71.         <form action="registration.jsp" method="post">
  72.             <label for="name">Name:</label><br>
  73.             <input type="text" id="name" name="name" required><br><br>
  74.  
  75.             <label for="age">Age:</label><br>
  76.             <input type="number" id="age" name="age" required><br><br>
  77.  
  78.             <input type="submit" value="Register">
  79.         </form>
Tags: Java
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement