Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. ----------------------
  2. FILE INDEX.HTML
  3. ----------------------
  4. <!DOCTYPE html>
  5. <html lang="it">
  6. <head>
  7. <meta charset="UTF-8">
  8. <title>Cookies</title>
  9. </head>
  10. <body>
  11. <form action="login.jsp" method="post">
  12.  
  13. <div>
  14. <label for="user">Nome Utente</label>
  15. <input type="text" id="user" name="username" required="required">
  16. </div>
  17.  
  18. <!-- inserisco due <div> per far si che i due elementi siano l'uno sopra l'altro -->
  19.  
  20. <div>
  21. <label for="pass">Password</label>
  22. <input type="password" id="pass" name= "password" required="required">
  23. </div>
  24.  
  25. <div>
  26. <input type="submit" value="login">
  27. </div>
  28.  
  29. </form>
  30. </body>
  31. </html>
  32. -----------------
  33. LOGIN.JSP
  34. -----------------
  35. <%@ page session="true"
  36. import="it.cefi.User" %> <!-- per poter utlizzare dati di sessione all'interno della pagina(di defaut e' true ) -->
  37.  
  38. <!DOCTYPE html>
  39. <html>
  40. <head>
  41. <meta charset="UTF-8">
  42. <title>Sessions</title>
  43. </head>
  44. <body>
  45. <jsp:useBean id="user" class="it.cefi.User" />
  46. <jsp:setProperty property="*" name="user"/>
  47.  
  48. <%
  49. if(user.getUsername().equals("danilo")
  50. && user.getPassword().equals("1234")) {
  51.  
  52. session.setAttribute("username", user.getUsername());
  53. response.sendRedirect("page1.jsp");
  54. }
  55. %>
  56.  
  57.  
  58. <h3>Nome Utene o Password non corretti</h3>
  59. <a href="index.html">Torna Indietro</a>
  60. </body>
  61. </html>
  62. ----------------
  63. LOGOUT.JSP
  64. ----------------
  65. <%@ page session="true"%>
  66. <%
  67. if(session.getAttribute("username") != null) {
  68. session.invalidate();
  69. }
  70.  
  71. response.sendRedirect("Index.html");
  72.  
  73. %>
  74. ----------------
  75. PAGE1.JSP
  76. ----------------
  77. <%@ page session="true"%>
  78. <!DOCTYPE html>
  79. <html>
  80. <head>
  81. <meta charset="UTF-8">
  82. <title>Sessions</title>
  83. </head>
  84. <body>
  85. <%
  86. if(session.getAttribute("username") != null) {
  87. %>
  88. <h3>Benvenuto, <%= session.getAttribute("username") %></h3>
  89. <h3>ID di sessione: <%= session.getId() %></h3>
  90.  
  91. <a href="logout.jsp">Disconnetti</a>
  92.  
  93. <% } else { %>
  94. <h3>Accesso Non Consentito</h3>
  95. <a href="index.html">Torna Indietro</a>
  96. <% } %>
  97.  
  98. </body>
  99. </html>
  100. ----------------
  101. USER.JAVA
  102. ----------------
  103. package it.cefi;
  104.  
  105. import java.io.Serializable;
  106.  
  107. public class User implements Serializable {
  108.  
  109. private static final long serialVersionUID = 1L;
  110.  
  111. private String username; /*dovranno avere lo stesso nome del campo a cui fanno riferimento nella parte web*/
  112. private String password;
  113.  
  114. public User() {
  115. super();
  116. }
  117.  
  118. public String getUsername() {
  119. return username;
  120. }
  121.  
  122. public void setUsername(String username) {
  123. this.username = username;
  124. }
  125.  
  126. public String getPassword() {
  127. return password;
  128. }
  129.  
  130. public void setPassword(String password) {
  131. this.password = password;
  132. }
  133.  
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement