Advertisement
Guest User

Untitled

a guest
Jan 1st, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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_2_5.xsd" id="WebApp_ID" version="2.5">
  3. <display-name>quiz_mcq</display-name>
  4. <welcome-file-list>
  5. <welcome-file>welcome.htm</welcome-file>
  6. </welcome-file-list>
  7.  
  8. <servlet>
  9. <servlet-name>spring-dispatcher</servlet-name>
  10. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  11. </servlet>
  12.  
  13. <servlet-mapping>
  14. <servlet-name>spring-dispatcher</servlet-name>
  15. <url-pattern>/</url-pattern>
  16. </servlet-mapping>
  17.  
  18. </web-app>
  19.  
  20. <?xml version="1.0" encoding="UTF-8"?>
  21. <beans xmlns="http://www.springframework.org/schema/beans"
  22. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  23. xmlns:mvc="http://www.springframework.org/schema/mvc"
  24. xmlns:tx="http://www.springframework.org/schema/tx"
  25. xmlns:context="http://www.springframework.org/schema/context"
  26. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  27. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  28. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  29. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  30.  
  31. <context:component-scan base-package="com.quiz_mcq.controller" />
  32.  
  33. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  34. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  35. <property name="url" value="jdbc:mysql://localhost:3306/quiz_mcq" />
  36. <property name="username" value="root" />
  37. <property name="password" value="" />
  38. </bean>
  39.  
  40. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  41. <property name="dataSource" ref="dataSource"></property>
  42. <property name="packagesToScan" value="com.quiz_mcq.bean"></property>
  43. <property name="hibernateProperties">
  44. <props>
  45. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  46. <prop key="hibernate.hbm2ddl.auto">update</prop>
  47. <prop key="hibernate.show_sql">true</prop>
  48. </props>
  49. </property>
  50.  
  51. </bean>
  52.  
  53.  
  54.  
  55. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  56. <property name="prefix" value="/" />
  57. <property name="suffix" value=".jsp" />
  58. </bean>
  59.  
  60.  
  61. </beans>
  62.  
  63. @Controller
  64. public class QuizMcqController {
  65.  
  66. UserService userService;
  67.  
  68. @RequestMapping(value="/welcome.htm")
  69. public ModelAndView redirectToLoginPage(){
  70.  
  71. ModelAndView modelAndView = new ModelAndView("login");
  72. return modelAndView;
  73.  
  74. }
  75.  
  76. @RequestMapping(value="/AuthenticateUser.htm", method = RequestMethod.POST)
  77. public ModelAndView authenticateUser(@RequestParam("username") String username, @RequestParam("password")String password){
  78.  
  79. userService = new UserService();
  80. boolean flag = userService.authenticate(username,password);
  81. if(flag){
  82. ModelAndView modelAndView = new ModelAndView("login");
  83. return modelAndView;
  84. }
  85. else{
  86. ModelAndView modelAndView = new ModelAndView("wrong");
  87. return modelAndView;
  88. }
  89.  
  90.  
  91. }
  92.  
  93.  
  94. }
  95.  
  96. public class UserService {
  97.  
  98. User user;
  99. UserDao dao;
  100.  
  101. public boolean authenticate(String username, String password) {
  102. user = new User();
  103. user.setUsername(username);
  104. user.setPassword(password.toCharArray());
  105.  
  106. if(dao.authenticateUser(user))
  107. {
  108. return true;
  109. }
  110.  
  111. return false;
  112.  
  113. }
  114. }
  115.  
  116. @Repository
  117. public class UserDao implements IUser {
  118.  
  119. @Autowired
  120. SessionFactory sessionFactory;
  121.  
  122. public SessionFactory getSessionFactory() {
  123. return sessionFactory;
  124. }
  125.  
  126. public void setSessionFactory(SessionFactory sessionFactory) {
  127. this.sessionFactory = sessionFactory;
  128. }
  129.  
  130. @Override
  131. public boolean authenticateUser(User user) {
  132. String username = user.getUsername();
  133. char[] password = user.getPassword();
  134. System.out.println(username +" <----> "+password);
  135. String hql = "from User where username='username' and password ='password'";
  136. Query query = getSessionFactory().openSession().createQuery(hql);
  137. List list = new ArrayList();
  138. list = query.list();
  139. if (list.size() > 0 && list != null) {
  140. return true;
  141. }
  142.  
  143. return false;
  144. }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement