Guest User

Untitled

a guest
Oct 22nd, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.50 KB | None | 0 0
  1. a) Develop a Hibernate Application to store Feedback of Website Visitor.
  2. Code :-
  3. index.jsp –
  4. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  5. <!DOCTYPE html>
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9. <title>JSP Page</title>
  10. </head>
  11. <body style="background-color:#bcd2d0;">
  12. <table style="width: 100%; alignment-adjust: central; border: 0px;">
  13. <tr>
  14. <td>
  15. <table style="width: 100%; border: 0px;">
  16. <tr>
  17. <td style="text-align: left; vertical-align: middle; padding-right:0px; padding-left:0px; padding-bottom:0px; font:24px/30px Georgia; width:228px; color:#786e4e; padding-top:0px; height:37px;">
  18. Enter Your Feedback
  19. </td>
  20. </tr>
  21. </table>
  22. </td>
  23. </tr>
  24. <tr style="text-align: left; vertical-align: top;">
  25. <td style="height: 20px;"><hr /></td>
  26. </tr>
  27. <tr>
  28. <td>
  29. <form action="FeedbackView.jsp" method="post">
  30. <table style="border-spacing: 2px; border: 0px;">
  31.  
  32. <tr>
  33. <td style="text-align: right; font-size:15px; font-family:Arial,Times,serif; font-weight:bold;">
  34. Visitor Name:
  35. </td>
  36. <td>
  37. <input name="guest" maxlength="25" size="50" />
  38. </td>
  39. </tr>
  40. <tr>
  41. <td style="text-align: right; font-size:15px; font-family:Arial,Times,serif; font-weight:bold;">
  42. Message:
  43. </td>
  44. <td>
  45. <textarea rows="5" cols="36" name="message"></textarea>
  46. </td>
  47. </tr>
  48. <tr>
  49. <td colspan="2" style="text-align: right;">
  50. <input type="submit" name="btnSubmit" value="Submit" />
  51. </td>
  52. </tr>
  53. </table>
  54. </form>
  55. </td>
  56. </tr>
  57. </table>
  58. </body>
  59. </html>
  60. FeedbackView.jsp –
  61. <%@page import="java.util.List"%>
  62. <%@page import="myApp.VisitorFeedback"%>
  63. <%@page import="org.hibernate.SessionFactory"%>
  64. <%@page import="java.util.Iterator,org.hibernate.Transaction,org.hibernate.*, org.hibernate.cfg.Configuration "%>
  65. <%@page import="javax.imageio.spi.ServiceRegistry"%>
  66. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  67. <!DOCTYPE html>
  68. <%!
  69. SessionFactory sessionFactory;
  70. ServiceRegistry serviceRegistry;
  71. org.hibernate.Session hibernateSession;
  72. List<VisitorFeedback> visitorfeedback;
  73. %>
  74. <%
  75. Configuration configuration = new Configuration();
  76. configuration.configure("hibernate.cfg.xml");
  77.  
  78. //serviceRegistry = new ServiceRegistry().applySettings(configuration.getProperties()).buildServiceRegistry();
  79. sessionFactory= configuration.buildSessionFactory();
  80. //sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  81. hibernateSession = sessionFactory.openSession();
  82. Transaction transaction = null;
  83.  
  84. String submit = request.getParameter("btnSubmit");
  85. if(submit != null && ("Submit").equals(submit)) {
  86. VisitorFeedback vf= new VisitorFeedback();
  87. try {
  88. transaction = hibernateSession.beginTransaction();
  89.  
  90.  
  91. String guest = request.getParameter("guest");
  92. String message = request.getParameter("message");
  93.  
  94. System.out.println(guest);
  95. vf.setVisitorName(guest);
  96. vf.setMessage(message);
  97.  
  98. hibernateSession.save(vf);
  99. transaction.commit();
  100. } catch (RuntimeException e) {
  101. if(transaction != null) transaction.rollback();
  102. throw e;
  103. }
  104. response.sendRedirect("FeedbackView.jsp");
  105. }
  106.  
  107. try {
  108. hibernateSession.beginTransaction();
  109. visitorfeedback = hibernateSession.createQuery("from VisitorFeedback").list();
  110. } catch (RuntimeException e) {
  111. throw e;
  112. }
  113. hibernateSession.close();
  114. %>
  115.  
  116. <html>
  117. <head>
  118. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  119. <title>JSP Page</title>
  120. </head>
  121. <body style="background-color: #bcd2d0;">
  122. <table style="alignment-adjust: central; width: 100%; border: 0px;">
  123. <tr>
  124. <td>
  125. <table style="width: 100%; border: 0px;">
  126. <tr>
  127. <td style="width: 60%; vertical-align: middle; text-align: left; padding-right:0px; padding-left:0px; padding-bottom:0px; font:24px/30px Georgia; width:228px; color:#786e4e; padding-top:0px; height:37px;">
  128. View the Guest Book
  129. </td>
  130. <td style="vertical-align: bottom; text-align: right; font:12px/16px Georgia, serif; color:#786e4e;">
  131. <b>Click <a href="index.jsp"> here</a> to Enter Your Feedback.</b>
  132. </td>
  133. </tr>
  134. </table>
  135. </td>
  136. </tr>
  137. <tr style="text-align: left; vertical-align: top;">
  138. <td style="height: 20px;"><hr /></td>
  139. </tr>
  140. <tr>
  141. <td>
  142. <table style="text-align: left; width: 100%; border: 0px;">
  143. <%
  144. Iterator iterator = visitorfeedback.iterator();
  145. while (iterator.hasNext()) {
  146. VisitorFeedback objvF = (VisitorFeedback) iterator.next();
  147. %>
  148. <tr>
  149. <td style="font:12px/16px Georgia; color:#786e4e;">
  150. <b><%=objvF.getVisitorName()%>:</b>
  151. <%=objvF.getMessage()%>
  152. <br /><br />
  153. </td>
  154. </tr>
  155. <%
  156. }
  157. %>
  158. </table>
  159. </td>
  160. </tr>
  161. </table>
  162. </body>
  163. </html>
  164.  
  165. VisitorFeeedback.java –
  166. package myApp;
  167.  
  168. import javax.persistence.Column;
  169. import javax.persistence.Entity;
  170. import javax.persistence.Id;
  171. import javax.persistence.Table;
  172. @Entity
  173. @Table(name="VisitorFeedback")
  174. public class VisitorFeedback implements java.io.Serializable {
  175. @Id
  176. @Column(name="VisitorName")
  177. private String visitorName;
  178. @Column(name="Message")
  179. private String message;
  180.  
  181.  
  182. public VisitorFeedback() {
  183. }
  184.  
  185. public VisitorFeedback(String visitorName, String message) {
  186.  
  187. this.visitorName = visitorName;
  188. this.message = message;
  189.  
  190. }
  191.  
  192.  
  193. public String getVisitorName() {
  194. return visitorName;
  195. }
  196. public void setVisitorName(String visitorName) {
  197. this.visitorName = visitorName;
  198. }
  199.  
  200. public String getMessage() {
  201. return message;
  202. }
  203. public void setMessage(String message) {
  204. this.message = message;
  205. }
  206. }
  207.  
  208. hibernate.cfg.xml –
  209. <?xml version="1.0" encoding="UTF-8"?>
  210. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD
  211. <hibernate-configuration>
  212. <session-factory>
  213. <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
  214. <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
  215. <property name="hibernate.connection.url">jdbc:derby://localhost:1527/VisitorFeedback</property>
  216. <property name="hibernate.connection.username">root</property>
  217. <property name="hibernate.connection.password">root</property>
  218. <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
  219. <property name="hibernate.current_session_context_class">thread</property>
  220. <property name="hibernate.show_sql"> true </property>
  221. <mapping class="myApp.VisitorFeedback"/>
  222. </session-factory>
  223. </hibernate-configuration>
  224.  
  225.  
  226.  
  227. a) Develop a simple JSP application values obtained from the use of intrinsic objects of various types.
  228. Code :-
  229. index.jsp -
  230. <html>
  231. <head><title>Index Page</title></head>
  232. <body>
  233. <form action="welcome.jsp">
  234. Enter Username : <input type="text" name="uname"></br>
  235. <input type="submit" value="go">
  236. </form>
  237. </body>
  238. </html>
  239.  
  240. welcome.jsp -
  241. <%@page import="java.io.PrintWriter"%>
  242. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  243. <html>
  244. <head>
  245. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  246. <title>Implicit Objects Example</title>
  247. </head>
  248. <body>
  249. <%-- response object --%>
  250. <%
  251. PrintWriter pw = response.getWriter();
  252. pw.print("<h1><strong>"+" Welcome "+"</strong></h1>");
  253. %><br><br>
  254. <%-- request object --%>
  255. <strong>Request example - User : </strong><%=request.getParameter("uname").toString() %><br><br>
  256. <%-- out object --%>
  257. <strong>Out object prints: </strong><%out.print("This is an example");%><br><br>
  258. <%-- config object --%>
  259. <strong>Config example - servlet name: </strong><%=config.getServletName()%><br><br>
  260. <%-- application object --%>
  261. <strong>Application example - server info: </strong><%=application.getServerInfo()%><br><br>
  262. <%-- page object --%>
  263. <strong>Page example - page name: </strong><%=page.getClass().getName()%><br><br>
  264. <%-- session object --%>
  265. <strong>Session example - creation time: </strong><%=session.getCreationTime()%><br><br>
  266. <%-- pageContext object --%>
  267. <%-- pageContext.setAttribute("Test", "Test Value"); --%>
  268. <strong>PageContext example - class name: </strong><%=pageContext.getClass().getName() %><br><br>
  269. </body>
  270. </html>
  271.  
  272.  
  273.  
  274.  
  275.  
  276. b) Create a servlet for a login page if the username and password are correct then it says message “Hello <username>” else a message “Login Failed”.
  277. Code :-
  278. index.html -
  279. <body>
  280. <form method=”get” action=”hello”>
  281. <legend>Login Form</legend>
  282. Username : <input type="text" name="username"><br/>
  283. Password : <input type="password" name="password"><br/>
  284. <input type="submit" value="Submit" name="submit">
  285. </form>
  286. </body>
  287. hello.java –
  288. out.println("<body>");
  289. String user=request.getParameter("username");
  290. String pass=request.getParameter("password");
  291. if(user.equals("ABC") && pass.equals("abc123"))
  292. out.println("<h1>"+"Hello "+user+" !!"+"</h1>");
  293. else if(user.equals("DEF") && pass.equals("def123"))
  294. out.println("<h1>"+"Hello "+user+" !!"+"</h1>");
  295. else if(user.equals("GHI") && pass.equals("ghi123"))
  296. out.println("<h1>"+"Hello "+user+" !!"+"</h1>");
  297. else
  298. out.println("<h1>"+"Login Failed !"+"</h1>");
  299. out.println("</body>");
Add Comment
Please, Sign In to add comment