Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- a) Develop a Hibernate Application to store Feedback of Website Visitor.
- Code :-
- index.jsp –
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP Page</title>
- </head>
- <body style="background-color:#bcd2d0;">
- <table style="width: 100%; alignment-adjust: central; border: 0px;">
- <tr>
- <td>
- <table style="width: 100%; border: 0px;">
- <tr>
- <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;">
- Enter Your Feedback
- </td>
- </tr>
- </table>
- </td>
- </tr>
- <tr style="text-align: left; vertical-align: top;">
- <td style="height: 20px;"><hr /></td>
- </tr>
- <tr>
- <td>
- <form action="FeedbackView.jsp" method="post">
- <table style="border-spacing: 2px; border: 0px;">
- <tr>
- <td style="text-align: right; font-size:15px; font-family:Arial,Times,serif; font-weight:bold;">
- Visitor Name:
- </td>
- <td>
- <input name="guest" maxlength="25" size="50" />
- </td>
- </tr>
- <tr>
- <td style="text-align: right; font-size:15px; font-family:Arial,Times,serif; font-weight:bold;">
- Message:
- </td>
- <td>
- <textarea rows="5" cols="36" name="message"></textarea>
- </td>
- </tr>
- <tr>
- <td colspan="2" style="text-align: right;">
- <input type="submit" name="btnSubmit" value="Submit" />
- </td>
- </tr>
- </table>
- </form>
- </td>
- </tr>
- </table>
- </body>
- </html>
- FeedbackView.jsp –
- <%@page import="java.util.List"%>
- <%@page import="myApp.VisitorFeedback"%>
- <%@page import="org.hibernate.SessionFactory"%>
- <%@page import="java.util.Iterator,org.hibernate.Transaction,org.hibernate.*, org.hibernate.cfg.Configuration "%>
- <%@page import="javax.imageio.spi.ServiceRegistry"%>
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <%!
- SessionFactory sessionFactory;
- ServiceRegistry serviceRegistry;
- org.hibernate.Session hibernateSession;
- List<VisitorFeedback> visitorfeedback;
- %>
- <%
- Configuration configuration = new Configuration();
- configuration.configure("hibernate.cfg.xml");
- //serviceRegistry = new ServiceRegistry().applySettings(configuration.getProperties()).buildServiceRegistry();
- sessionFactory= configuration.buildSessionFactory();
- //sessionFactory = configuration.buildSessionFactory(serviceRegistry);
- hibernateSession = sessionFactory.openSession();
- Transaction transaction = null;
- String submit = request.getParameter("btnSubmit");
- if(submit != null && ("Submit").equals(submit)) {
- VisitorFeedback vf= new VisitorFeedback();
- try {
- transaction = hibernateSession.beginTransaction();
- String guest = request.getParameter("guest");
- String message = request.getParameter("message");
- System.out.println(guest);
- vf.setVisitorName(guest);
- vf.setMessage(message);
- hibernateSession.save(vf);
- transaction.commit();
- } catch (RuntimeException e) {
- if(transaction != null) transaction.rollback();
- throw e;
- }
- response.sendRedirect("FeedbackView.jsp");
- }
- try {
- hibernateSession.beginTransaction();
- visitorfeedback = hibernateSession.createQuery("from VisitorFeedback").list();
- } catch (RuntimeException e) {
- throw e;
- }
- hibernateSession.close();
- %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP Page</title>
- </head>
- <body style="background-color: #bcd2d0;">
- <table style="alignment-adjust: central; width: 100%; border: 0px;">
- <tr>
- <td>
- <table style="width: 100%; border: 0px;">
- <tr>
- <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;">
- View the Guest Book
- </td>
- <td style="vertical-align: bottom; text-align: right; font:12px/16px Georgia, serif; color:#786e4e;">
- <b>Click <a href="index.jsp"> here</a> to Enter Your Feedback.</b>
- </td>
- </tr>
- </table>
- </td>
- </tr>
- <tr style="text-align: left; vertical-align: top;">
- <td style="height: 20px;"><hr /></td>
- </tr>
- <tr>
- <td>
- <table style="text-align: left; width: 100%; border: 0px;">
- <%
- Iterator iterator = visitorfeedback.iterator();
- while (iterator.hasNext()) {
- VisitorFeedback objvF = (VisitorFeedback) iterator.next();
- %>
- <tr>
- <td style="font:12px/16px Georgia; color:#786e4e;">
- <b><%=objvF.getVisitorName()%>:</b>
- <%=objvF.getMessage()%>
- <br /><br />
- </td>
- </tr>
- <%
- }
- %>
- </table>
- </td>
- </tr>
- </table>
- </body>
- </html>
- VisitorFeeedback.java –
- package myApp;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.Id;
- import javax.persistence.Table;
- @Entity
- @Table(name="VisitorFeedback")
- public class VisitorFeedback implements java.io.Serializable {
- @Id
- @Column(name="VisitorName")
- private String visitorName;
- @Column(name="Message")
- private String message;
- public VisitorFeedback() {
- }
- public VisitorFeedback(String visitorName, String message) {
- this.visitorName = visitorName;
- this.message = message;
- }
- public String getVisitorName() {
- return visitorName;
- }
- public void setVisitorName(String visitorName) {
- this.visitorName = visitorName;
- }
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- }
- hibernate.cfg.xml –
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
- <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
- <property name="hibernate.connection.url">jdbc:derby://localhost:1527/VisitorFeedback</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">root</property>
- <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
- <property name="hibernate.current_session_context_class">thread</property>
- <property name="hibernate.show_sql"> true </property>
- <mapping class="myApp.VisitorFeedback"/>
- </session-factory>
- </hibernate-configuration>
- a) Develop a simple JSP application values obtained from the use of intrinsic objects of various types.
- Code :-
- index.jsp -
- <html>
- <head><title>Index Page</title></head>
- <body>
- <form action="welcome.jsp">
- Enter Username : <input type="text" name="uname"></br>
- <input type="submit" value="go">
- </form>
- </body>
- </html>
- welcome.jsp -
- <%@page import="java.io.PrintWriter"%>
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Implicit Objects Example</title>
- </head>
- <body>
- <%-- response object --%>
- <%
- PrintWriter pw = response.getWriter();
- pw.print("<h1><strong>"+" Welcome "+"</strong></h1>");
- %><br><br>
- <%-- request object --%>
- <strong>Request example - User : </strong><%=request.getParameter("uname").toString() %><br><br>
- <%-- out object --%>
- <strong>Out object prints: </strong><%out.print("This is an example");%><br><br>
- <%-- config object --%>
- <strong>Config example - servlet name: </strong><%=config.getServletName()%><br><br>
- <%-- application object --%>
- <strong>Application example - server info: </strong><%=application.getServerInfo()%><br><br>
- <%-- page object --%>
- <strong>Page example - page name: </strong><%=page.getClass().getName()%><br><br>
- <%-- session object --%>
- <strong>Session example - creation time: </strong><%=session.getCreationTime()%><br><br>
- <%-- pageContext object --%>
- <%-- pageContext.setAttribute("Test", "Test Value"); --%>
- <strong>PageContext example - class name: </strong><%=pageContext.getClass().getName() %><br><br>
- </body>
- </html>
- 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”.
- Code :-
- index.html -
- <body>
- <form method=”get” action=”hello”>
- <legend>Login Form</legend>
- Username : <input type="text" name="username"><br/>
- Password : <input type="password" name="password"><br/>
- <input type="submit" value="Submit" name="submit">
- </form>
- </body>
- hello.java –
- out.println("<body>");
- String user=request.getParameter("username");
- String pass=request.getParameter("password");
- if(user.equals("ABC") && pass.equals("abc123"))
- out.println("<h1>"+"Hello "+user+" !!"+"</h1>");
- else if(user.equals("DEF") && pass.equals("def123"))
- out.println("<h1>"+"Hello "+user+" !!"+"</h1>");
- else if(user.equals("GHI") && pass.equals("ghi123"))
- out.println("<h1>"+"Hello "+user+" !!"+"</h1>");
- else
- out.println("<h1>"+"Login Failed !"+"</h1>");
- out.println("</body>");
Add Comment
Please, Sign In to add comment