Advertisement
Guest User

Untitled

a guest
Apr 25th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <%@ taglib prefix="s" uri="/struts-tags"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8. <title>Welcome to Popular Movies</title>
  9.  
  10. <style type="text/css">
  11. .errors {
  12. background-color:#FFCCCC;
  13. border:1px solid #CC0000;
  14. width:400px;
  15. margin-bottom:8px;
  16. }
  17. .errors li{
  18. list-style: none;
  19. }
  20. </style>
  21.  
  22. </head>
  23. <body>
  24.  
  25. <h2>Please Log in</h2>
  26. <div id="errorMessage" >
  27. <s:if test="hasActionErrors()">
  28. <div class="errors" >
  29. <s:actionerror/>
  30. </div>
  31. </s:if>
  32. </div>
  33. <s:form action="login" method="post" name="myForm" onSubmit="return preValidate(errorMessage, userName, userPassword)" >
  34. <s:textfield name="userName" label="Name" size="20" id="userName" />
  35. <s:password name="password" label="Password" size="20" id="userPassword" />
  36. <s:hidden name="registration" value="false" />
  37. <s:submit value="Submit" align="center" />
  38. </s:form>
  39.  
  40. <hr>
  41.  
  42. <a href="/PopularMovies/registration.jsp">Sign up free</a>
  43. </body>
  44.  
  45. <script type="text/javascript" src="validation.js">
  46.  
  47. </script>
  48. </html>
  49.  
  50. package com.esi.actions;
  51.  
  52. import com.opensymphony.xwork2.ActionSupport;
  53.  
  54. import java.util.List;
  55.  
  56. import org.hibernate.Session;
  57. import org.hibernate.SessionFactory;
  58. import org.hibernate.cfg.Configuration;
  59.  
  60. import com.sans.model.Movie;
  61. import com.sans.model.Movies;
  62. import com.sans.model.user_account;
  63. import org.hibernate.HibernateException;
  64. import org.hibernate.Query;
  65.  
  66. @SuppressWarnings("serial")
  67. public class Login extends ActionSupport {
  68. private String password;
  69. private String userName;
  70. private String email;
  71. private String firstName;
  72. private String lastName;
  73. private String registration;
  74. private static SessionFactory sessionFactory;
  75.  
  76. @SuppressWarnings("deprecation")
  77. public String execute() {
  78. boolean isRegistration = Boolean.parseBoolean(registration);
  79.  
  80. System.out.println("Action called from struts.xml");
  81. try {
  82. //Setting up Hibernate configuration
  83. System.out.println("Attempting Database connection...");
  84. sessionFactory = new Configuration().configure().buildSessionFactory();
  85. }
  86. catch(Exception ex){
  87. System.out.println("Failed to create sessionFactory object. " + ex.toString());
  88. return INPUT;
  89. }
  90.  
  91. // If Registration flag is set to true then it means user is trying to register
  92. // else authenticate user
  93. if(isRegistration) {
  94. if(this.addUser()) {
  95. return SUCCESS;
  96. }
  97. else {
  98. return INPUT;
  99. }
  100. }
  101. else {
  102. if(this.authenticateUser(this.getUserName())) {
  103. return SUCCESS;
  104. }
  105. else {
  106. return INPUT;
  107. }
  108. }
  109. }
  110.  
  111. public boolean addUser() {
  112. boolean result = false;
  113. user_account user = new user_account();
  114. user.setUser_Name(this.getUserName());
  115. user.setFirstName(this.getFirstName());
  116. user.setLastName(this.getLastName());
  117. user.setEmail(this.getEmail());
  118. user.setUser_Password(this.getPassword());
  119.  
  120. Session session = sessionFactory.openSession();
  121. try{
  122. session.beginTransaction();
  123. session.save(user);
  124. session.getTransaction().commit();
  125. result = true;
  126. addActionMessage("Welcome " + user.getUser_Name());
  127. }
  128. catch(HibernateException e){
  129. if(session.getTransaction() != null)
  130. session.getTransaction().rollback();
  131. System.out.println("Error trying to insert user to database.. " + e.getMessage() + "nStack Trace: ");
  132. e.printStackTrace();
  133. }
  134.  
  135. finally {
  136. session.close();
  137. }
  138. return result;
  139. }
  140.  
  141. public boolean authenticateUser(String userName) {
  142. boolean result = false;
  143.  
  144. Session session = sessionFactory.openSession();
  145. try{
  146. session.beginTransaction();
  147. String hql = "FROM user_account U WHERE U.User_Name = :userName";
  148. Query query = session.createQuery(hql);
  149. query.setParameter("userName", userName);
  150. List results = query.list();
  151.  
  152. //If the query result size is 0, then it means user does not exist in database
  153. if(results.size() != 0) {
  154. user_account user = (user_account)results.get(0);
  155.  
  156. if(this.getUserName().equals(user.getUser_Name()) && this.getPassword().equals(user.getUser_Password())) {
  157. addActionMessage("Welcome " + user.getUser_Name());
  158. result = true;
  159. }
  160. else {
  161. addActionError("Invalid User");
  162. result = false;
  163. }
  164. }
  165. else {
  166. addActionError("Invalid User");
  167. result = false;
  168. }
  169. session.getTransaction().commit();
  170. }
  171. catch(HibernateException e){
  172. if(session.getTransaction() != null)
  173. session.getTransaction().rollback();
  174. System.out.println("Error trying to insert user to database.. " + e.toString());
  175. }
  176. catch(IndexOutOfBoundsException e) {
  177.  
  178. }
  179. catch(Exception ex) {
  180. System.out.println("Something went wrong: " + ex.toString());
  181. }
  182.  
  183. finally {
  184. session.close();
  185. }
  186. return result;
  187. }
  188.  
  189.  
  190. public String getPassword() {
  191. return password;
  192. }
  193.  
  194. public void setPassword(String password) {
  195. this.password = password;
  196. }
  197. public String getUserName() {
  198. return userName;
  199. }
  200.  
  201. public void setUserName(String userName) {
  202. this.userName = userName;
  203. }
  204.  
  205. public String getEmail() {
  206. return email;
  207. }
  208.  
  209. public void setEmail(String email) {
  210. this.email = email;
  211. }
  212.  
  213. public String getFirstName() {
  214. return firstName;
  215. }
  216.  
  217. public void setFirstName(String firstName) {
  218. this.firstName = firstName;
  219. }
  220.  
  221. public String getLastName() {
  222. return lastName;
  223. }
  224.  
  225. public void setLastName(String lastName) {
  226. this.lastName = lastName;
  227. }
  228. public String getRegistration() {
  229. return registration;
  230. }
  231.  
  232. public void setRegistration(String registration) {
  233. this.registration = registration;
  234. }
  235.  
  236.  
  237.  
  238. }
  239.  
  240. <?xml version="1.0" encoding="UTF-8"?>
  241. <!DOCTYPE struts PUBLIC
  242. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  243. "http://struts.apache.org/dtds/struts-2.0.dtd">
  244.  
  245. <struts>
  246.  
  247.  
  248. </struts>
  249.  
  250. <?xml version="1.0" encoding="UTF-8"?>
  251. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  252. <display-name>PopularMovies</display-name>
  253. <welcome-file-list>
  254. <welcome-file>index.html</welcome-file>
  255. <welcome-file>index.htm</welcome-file>
  256. <welcome-file>index.jsp</welcome-file>
  257. <welcome-file>default.html</welcome-file>
  258. <welcome-file>default.htm</welcome-file>
  259. <welcome-file>default.jsp</welcome-file>
  260. </welcome-file-list>
  261.  
  262.  
  263. <filter>
  264. <filter-name>struts2</filter-name>
  265. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  266. </filter>
  267.  
  268. <filter-mapping>
  269. <filter-name>struts2</filter-name>
  270. <url-pattern>/*</url-pattern>
  271. </filter-mapping>
  272.  
  273. </web-app>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement